本文介绍了GSON抛“预期BEGIN_OBJECT但BEGIN_ARRAY”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON响应。

  {\"message\":\"[{\\\"first_name\\\":\\\"Sushant\\\",\\\"last_name\\\":\\\"Bhatnagar\\\",\\\"receiver_id\\\":\\\"19\\\",\\\"docket_number\\\":\\\"Test12\\\",\\\"status\\\":\\\"open\\\"}]\",\"$c$c\":200,\"format\":\"json\"}

和我有创建两个类解析如下: -

 公共类JsonResponse实现Serializable {公共字符串code;
公共字符串格式;
公开名单<消息>信息;

}

公共类信息实现Serializable {

 公共字符串FIRST_NAME;
公共字符串姓氏;
公共字符串receiver_id;
公共字符串docket_number;
公共字符串状态;

}

使用gSOAP的用于解析JSON,得到上述错误。 code为解析JSON是: -

 公共静态JsonResponse readDockets(字符串移动电话号码){
    JsonResponse解析度=新JsonResponse();
    HttpClient的客户端=新DefaultHttpClient();
    字符串服务=htt​​p://api.pod.iamwhiney.com:8994/api.php?path=/deliveryRecord/refresh/\"+\"9968395206;
    HTTPGET HTTPGET =新HTTPGET(服务);
    尝试{
        HTT presponse响应= client.execute(HTTPGET);
        状态行状态行= response.getStatusLine();
        INT状态code = statusLine.getStatus code();
        如果(状态code == 200){            HttpEntity getResponseEntity = response.getEntity();
            InputStream的HTT presponseStream = getResponseEntity.getContent();
            阅读器的InputStreamReader =新的InputStreamReader(HTT presponseStream);
            GSON GSON =新GSON();
            RES = gson.fromJson(InputStreamReader的,JsonResponse.class);        }其他{        }
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    返回水库;
}


解决方案

我不知道为什么你使用GSON,由于Android都有它自己建立JSON解析器。至于你得到的错误...那是因为你解析JSON是一个JSONArray,不是一个JSONObject。我不太知道什么@Yaqub在看,但你的JSON响应应该如下:

  {消息:
    {FIRST_NAME:Sushant
     姓氏:纳加尔.....
     身份:打开
    },code:200,格式:JSON}

也就是说,没有[]周围的内容,因为这告诉JSON解析器,这是一个JSON阵列只有1指数,而你显然希望一个JSON对象来代替。以上JSONString将允许您解析它,你可以从信息标签中获取一个JSONObject。

注意:我已经删除了越狱,因为我想通过一个解析器来运行我的编辑,但你可以很容易地添加这些回来了,它应该仍然工作

注意:在code:200 在原始JSON必须code:200,否则你会得到另一个错误有

i have following JSON Response .

  {"message":"[{\"first_name\":\"Sushant\",\"last_name\":\"Bhatnagar\",\"receiver_id\":\"19\",\"docket_number\":\"Test12\",\"status\":\"open\"}]","code":200,"format":"json"}

and I have create two classes for parse it as below :-

     public class JsonResponse implements Serializable {

public String code;
public String format;
public List<Message> message;

}

public class Message implements Serializable{

public String first_name;
public String last_name;
public String receiver_id;
public String docket_number;
public String status;

}

Using GSOAP for parse json , getting above error . Code for parse JSON is :-

         public static JsonResponse readDockets(String mobileNumber) {
    JsonResponse res = new JsonResponse();
    HttpClient client = new DefaultHttpClient();
    String service = "http://api.pod.iamwhiney.com:8994/api.php?path=/deliveryRecord/refresh/"+"9968395206";
    HttpGet httpGet = new HttpGet(service);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {

            HttpEntity getResponseEntity = response.getEntity();
            InputStream httpResponseStream = getResponseEntity.getContent();
            Reader inputStreamReader = new InputStreamReader(httpResponseStream);
            Gson gson = new Gson();
            res = gson.fromJson(inputStreamReader, JsonResponse.class);

        } else {

        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}
解决方案

I don't know why you're using Gson, as Android has it's own build in JSON parser. As for the error you're getting...it's because the JSON you're parsing is a JSONArray, not a JSONObject. I'm not quite sure what @Yaqub is looking at, but your JSON response should be as follows:

{"message":
    {"first_name":"Sushant",
     "last_name":"Bhatnagar".....
     "status":"open"
    },"code":"200","format":"json"}

That is, without the [ ] around the content, as this tells the JSON parser that it's a JSON Array with only 1 index, whereas you clearly want a single JSON Object instead. The above JSONString will allow you to parse it where you can get a JSONObject from the 'message' tag.

Note: I've removed the escapes as I wanted to run my edit through a parser, but you could easily add these back in and it should still work.

Note: the "code":200 in your original JSON needs to be "code":"200" otherwise you'll get another error there

这篇关于GSON抛“预期BEGIN_OBJECT但BEGIN_ARRAY”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:32