在Android中解析嵌套的json数据

时间:2014-02-17 21:40:56

标签: android json parsing

我有这个json

{
"where":"{}",
"expiry":1451606400,
"data":{
    "alert":"alert_message",
    "names":["john","paul","frank","bill"],
    "action":"com.fidelizacion.UPDATE_STATUS"
}

}

在我的Android应用程序中,我解析了这样的json数据:

    String action = intent.getAction();
          String channel = intent.getExtras().getString("com.parse.Channel");
          JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));


          Iterator itr = json.keys();
          while (itr.hasNext()) {
            String key = (String) itr.next();
            Log.d(TAG, "..." + key + " => " + json.getString(key));

          }

和日志显示

action => com.fidelizacion.UPDATE_STATUS
alert => alert_message
names => ["john","paul","frank","bill"]

但我想声明一个包含我收到的名字的数组。任何想法

1 个答案:

答案 0 :(得分:0)

您可以获取JSONArray对象:

JSONArray arr = json.getJSONArray("names");
int len = arr.length();
String[] names = new String[len];
for (int i = 0; i < len; i++) {
    names[i] = arr.getString(i);
}