获取JSON的一部分并将其保存到JSONArray

时间:2018-05-25 10:14:59

标签: android json

echo json_encode($ response)的结果是

{"error":false,"message":"Success","neededData":[{"column1":1,"column2":2,"column3":3},{"column1":4,"column2":5,"column3":6}]}

我需要的只是

[{"column1":1,"column2":2,"column3":3},{"column1":4,"column2":5,"column3":6}] and save it to a JSONArray array.


JSONObject userJson = obj.getJSONObject("neededData");
JSONArray array = new JSONArray(userJson);

似乎不起作用。

3 个答案:

答案 0 :(得分:0)

 try {
        JSONObject  jsonObject = new JSONObject("YOUR RESPONSE STRING FROM OF JSON OBJECT");
        JSONArray neededData = jsonObject.getJSONArray("neededData");
    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

neededData是一个json数组而不是一个json对象 你做错了

    JSONObject JjsonResponse = new JSONObject(yourresponce);
    JSONArray data= jsonResponse.getJSONArray("neededData ");

答案 2 :(得分:0)

我的代码如下所示。

String strjsonobj='{"error":false,"message":"Success","neededData":[{"column1":1,"column2":2,"column3":3},{"column1":4,"column2":5,"column3":6}]}';


try {
    JSONObject  jsonObject = new JSONObject(strjsonobj);
    JSONArray neededData = jsonObject.getJSONArray("neededData");
} catch (JSONException e) {
    e.printStackTrace();
}