【问题标题】:Parse JSON android [duplicate]解析 JSON android [重复]
【发布时间】:2014-04-16 17:38:18
【问题描述】:
[
  {
    "state":1,
    "phone":"123456789",
    "message":"Message number 1"
  },
  {
    "state":1,
    "phone":"123456789",
    "message":"Message number 1"
  }
]

我该如何解析这个??

我试过了:

JSONObject jObject = new JSONObject(result); // 结果是json 回应

但得到: java: 未报告的异常 org.json.JSONException;必须被抓住或宣布被扔掉

请帮忙。

【问题讨论】:

  • 你在发帖前查过错误吗?另外,在你这样做并解决它之后,你的 JSON 是一个数组还是一个对象?
  • 感谢@Sotirios Delimanolis

标签: java android json


【解决方案1】:

首先 您可以尝试使用 catch blok 来检测错误:

try
{
//do parsing
 } catch (JSONException e) {
        //some exception handler code.
    }  

你可以这样解析:

   JSONArray jArray = new JSONArray(result);
  for(int i=0; i < jArray.length(); i++) {
    JSONObject jObject = jArray.getJSONObject(i);
    String state_= jObject.getString("state"));   
             //you can do to the others
                  ............
    }

【讨论】:

  • 我编辑我的答案,感谢@SotiriosDelimanolis 的建议
猜你喜欢
  • 1970-01-01
  • 2018-04-12
  • 2011-04-12
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多