org.json.JSON 异常:字符 0 处的输入结束

Posted

技术标签:

【中文标题】org.json.JSON 异常:字符 0 处的输入结束【英文标题】:org.json.JSON Exception : End of input at character 0 【发布时间】:2012-01-02 09:00:53 【问题描述】:

我正在尝试从 android 解析 json,但我得到了这个奇怪的异常。我的json数据是

"id":"1","owner":"1","name":"gravitas","description":"is a fest","start_time":"0000-00-00 00: 00:00","end_time":"0000-00-00 00:00:00","venue":"vellore","radius":"10","lat":"11","lng": "11","type":"type","ownername":"dilip","noofpolls":0,"noofquizes":0,"peopleattending":0,"result":true

在android中我会这样做

JSONObject j =new JSONObject(response);
Event pst = gson.fromJson(j.toString(),  Event.class);

我明白了:

org.json.JSONException: end of input at character 0 of

它有什么问题?这是代码...

RestClient client = new RestClient("http://192.168.1.3/services/events/"+eve.getName());         
        try 
             Log.i("MY INFO", "calling boston");
            client.Execute(RequestMethod.POST);
         catch (Exception e) 
            e.printStackTrace();
        

        String response = client.getResponse();
         Log.i("MY INFO", response);
         GsonBuilder gsonb = new GsonBuilder();
         Gson gson = gsonb.create();
         Event pst = null;

        try 
            JSONObject j =new JSONObject(response);
            pst = gson.fromJson(j.toString(),  Event.class);

         catch (JSONException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        

【问题讨论】:

【参考方案1】:

哎呀!我的错,我应该使用 GET 方法。那个 url 不响应 POST 请求,所以我得到了 org.json.JSON 异常:字符 0.its 的输入结束,因为这个我得到了产生该异常的空响应.

【讨论】:

嗨,Rakon_188,我也遇到了同样的问题..但我正在尝试不同的方式..你能把完整的代码发给我吗.. 在这里遇到了同样的问题,但看起来我不得不将我的切换到 POST 而不是 GET。如果您只是有错误的错误,似乎会引发此错误,因此您可能需要简单地切换您的呼叫。【参考方案2】:

在我的例子中,我指的是一个甚至不存在的函数名。在更正函数后检查你的网络服务的函数名名字,它对我有用!

【讨论】:

【参考方案3】:

这是一个关于如何使用 GSON 进行解析的 sn-p

    Gson gson = new GsonBuilder().create();
    JsonParser jsonParser = new JsonParser();

     String response = client.getResponse();
    JsonObject jsonResp = jsonParser.parse(response).getAsJsonObject();
    // Important note: JsonObject is the one from GSON lib!
    // now you build as you like e.g.
    Event mEvent = gson.fromJson(jsonResp, Event.class);

...

【讨论】:

@user3458711,和你竞争的vandalize other posts不好。【参考方案4】:

这个错误有时也是因为json_encode函数要求所有传入的数据都被UTF-8编码。

尝试将mysqli_set_charset($con, 'utf8'); 添加到您的 php 中。

【讨论】:

以上是关于org.json.JSON 异常:字符 0 处的输入结束的主要内容,如果未能解决你的问题,请参考以下文章

线程“主”java.nio.file.InvalidPathException 中的异常:索引 2 处的非法字符 <:>:

Sample.exe 中 0x7537812f 处的未处理异常:0xC0020001:字符串绑定无效 [重复]

雪花:因异常 java.io.IOException 失败:org.apache.parquet.io.ParquetDecodingException:无法在块 -1 中读取 0 处的值

Flutter json 1处的意外字符

JSON入门之二:org.json的基本使用方法

JSONException:字符 0 处的输入结束