当前位置: 代码迷 >> Android >> 请问java里gson解析有关问题
  详细解决方案

请问java里gson解析有关问题

热度:62   发布时间:2016-04-28 06:59:22.0
请教java里gson解析问题
[
{"Id":1,"Name":"Alin","Grade":[{"Chinese":A,"Math":"A","English":"D"}]}
{"Id":2,"Name":"BoB","Grade":[{"Chinese":B,"Math":"C","English":"A"}]}
{"Id":3,"Name":"Cindy","Grade":[{"Chinese":C,"Math":"A","English":"B"}]}
]

想解析得到如下结果,望各位大侠们指点,最好能有代码!谢谢大家了!
Name=Alin, Chinese=A
Name=BoB, Chinese=B
Name=Cindy, Chinese=C

------解决方案--------------------
try {
String jsontxt = "[{\"Id\":1,\"Name\":\"Alin\",\"Grade\":{\"Chinese\":\"A\",\"Math\":\"A\",\"English\":\"D\"}},{\"Id\":2,\"Name\":\"BoB\",\"Grade\":{\"Chinese\":\"B\",\"Math\":\"C\",\"English\":\"A\"}},{\"Id\":3,\"Name\":\"Cindy\",\"Grade\":{\"Chinese\":\"C\",\"Math\":\"A\",\"English\":\"B\"}}]";
JSONArray jsonary = new JSONArray(jsontxt);
int N = jsonary.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
JSONObject j = jsonary.getJSONObject(i);
sb.append("Name:" + j.getString("Name"));
sb.append("     Chinese:"
+ j.getJSONObject("Grade").getString("Chinese"));
if (i < N - 1)
sb.append("\r\n");
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}

------解决方案--------------------
[
{"Id":1,"Name":"Alin","Grade":[{"Chinese":A,"Math":"A","English":"D"}]}
{"Id":2,"Name":"BoB","Grade":[{"Chinese":B,"Math":"C","English":"A"}]}
{"Id":3,"Name":"Cindy","Grade":[{"Chinese":C,"Math":"A","English":"B"}]}
]

JSONObject jo=new JSONObject("上面的字符串");
JSONArray ja= jo.getJSONArray();
for(int i=0;i<ja.length;i++){
{
  JSONObject temp=ja.getJSONObject(i);
  temp.getString("Name");
  temp.getJSONArray("Grade");
。。。
}
  相关解决方案