Which is the best way to parse JSON data in Androi

2019-06-25 03:08发布

I have JSON data to parse. The structure is not fixed, and sometimes it comes as a single string and other times as an array.

Currently, we are using the GSON library for parsing JSON, but are facing problems when it comes as an array.

For example:

1.  {"msg":"data","c":300,"stat":"k"}


2. {
    "msg": [
        " {\"id\":2,\"to\":\"83662\",\"from\":\"199878\",\"msg\":\"llll\",\"c\":200,\"ts\":1394536776}"
     ],
     "c": 200,
     "stat": "k",
     "_ts": 1394536776
 }

In the example above, sometimes I get msg as a string and sometimes as an array.

Can anyone help me? If I decide to use JSON parsing, it will be very tedious because I have around 20+ API to parse and each API contains a mininum of 50 fields.

4条回答
在下西门庆
2楼-- · 2019-06-25 03:24

You can use JSONObject and JSONArray classes instead of GSON to work with JSON data

for the first example

String jsonStr = "{\"msg\":\"data\",\"c\":300,\"stat\":\"k\"}";

JSONObject jsonObj = new JSONObject(jsonStr);

String msg = jsonObj.getString("msg");
Integer c = jsonObj.getInteger("c");
String stat = jsonObj.getString("stat");

For the second example

   String jsonStr = ... // "your JSON data";

   JSONObject jsonObj = new JSONObject(jsonStr);

   JSONArray jsonArr = jsonObj.getJSONArray("msg");

   JSONObject arrItem = jsonArr.getJSONObject(0);

   //and so on

Also JSONObject class have method opString, opArray which does not throw exception if data you trying to get is not exist or have a wrong type

For example

JSONArray arr = jsonObj.optJSONArray("msg");
JSONObject msg = null;
if (arr != null) {
    msg = arr.getJSONObject(0)
} else {
   msg = jsonObj.getJSONObject("msg");
}
查看更多
劫难
3楼-- · 2019-06-25 03:35

You can use Google GSON lib for directly parse the json to class object. This is easy and accurate.Okay do one thing both time code is different, if the code is 300 directly parse the json object without GSON. if the code is 200 the use the GSON (Define the similar java class)

String c= json.getString("c");
if(c.equals("300")
String message = status.getString("msg");
查看更多
Root(大扎)
4楼-- · 2019-06-25 03:35

There are two ways to parce JSON.

  1. Manually using Android OS JSON Parser Android JSON Parsing And Conversion
  2. Using GSON Library [Library] (https://code.google.com/p/google-gson/downloads/list). This easy to handle if you know all the parameters and models of json response.
查看更多
Evening l夕情丶
5楼-- · 2019-06-25 03:38

Refer the code snippet below to deserialize your json using Google's Gson library without exceptions.

String jsonStr = "your json string ";

Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject();

JsonElement elem = jsonObj.get("msg");

if(elem.isJsonArray()) { //**Array**
   ArrayList<MyMessage> msgList = gson.fromJson(elem.toString(), new TypeToken<List<MyMessage>>(){}.getType());
} else if(elem.isJsonObject()) { //**Object**
   Note note = gson.fromJson(elem.toString(), MyMessage.class);
} else {  //**String**
   String note = elem.toString();
}

MyMessage class

public class MyMessage {
   String to;
   String from;
   String msg;
   int id;
   int c;
   long ts;

   // Setters and Getters
}
查看更多
登录 后发表回答