从json数组中获取值 - Java

时间:2015-08-04 06:44:14

标签: java android json

这是我的json数组:

{"code":1,"auth_key":null,"gps_key":null,"status":"Success","message":"Changes found","data":null,"result":["2015-08-03 01:05 PM: Scheduled visit to Kandy Food City under Food City canceled.","2015-08-03 05:45 PM: Scheduled visit to Kandy Food City under Food City canceled.","2015-08-09 05:25 AM: Scheduled visit to Kandy Food City under Food City canceled.","2015-08-05 09:45 PM: New visit added to Kandy Food City under Food City."],"request_date_time":"2015-08-03 18:32:48"}

我试图逐个从json字符串中取出结果。它不适合我。 这是我的java代码

            JSONObject jObject = new JSONObject(value);
            JSONArray msg = (JSONArray) jObject.get("result");
            Iterator<String> iterator = msg.iterator();
            while (iterator.hasNext())
            {
                System.out.println(iterator.next());
            }

4 个答案:

答案 0 :(得分:0)

这是我的代码。用它作为参考:

JSONArray moviewJArray = new JSONArray(response);
for (int i=0;i<moviewJArray.length();i++)
{
     String movieName = (String) moviewJArray.get(i);
     // Do anything with string
}

在你的情况下:

JSONArray msg = (JSONArray) jObject.getJSONArray("result");
for (int i=0;i<msg.length();i++)
{
     String visit = (String) msg.get(i);
     // Do anything with string
}

答案 1 :(得分:0)

试试这个:

    JSONObject jObject = new JSONObject(value);
    // Getting JSON Array node
    JSONArray contactList = jObject.getJSONArray("result");
    for(int i=0;i<contactList.length();i++){
        Log.e("contact->",(String)contactList.get(i));
    }

答案 2 :(得分:0)

iterator.next将为您提供JSONObject。然后你必须做以下步骤才能得到结果。

 String jsonData="{\"code\":1,\"auth_key\":null,\"gps_key\":null,\"status\":\"Success\",\"message\":\"Changes found\",\"data\":null,\"result\":[\"2015-08-03 01:05 PM: Scheduled visit to Kandy Food City under Food City canceled.\",\"2015-08-03 05:45 PM: Scheduled visit to Kandy Food City under Food City canceled.\",\"2015-08-09 05:25 AM: Scheduled visit to Kandy Food City under Food City canceled.\",\"2015-08-05 09:45 PM: New visit added to Kandy Food City under Food City.\"],\"request_date_time\":\"2015-08-03 18:32:48\"}";  
              org.json.simple.parser.JSONParser jobj = new org.json.simple.parser.JSONParser();
        JSONObject jobj1 = (JSONObject)jobj.parse(jsonData);
JSONArray jArr = (JSONArray)jobj1.get("result");
java.util.Iterator itr =  (java.util.Iterator)jArr.iterator();

                while(itr.hasNext())
               {
                    Object o=(Object)itr1.next();
               }

答案 3 :(得分:0)

已经谷歌发布了一个名为GSON的开源库,用于将json解析为对象

请参考 https://github.com/google/gson