JSON解析没有键的JsonObject

时间:2017-03-17 03:50:08

标签: android json

我试图解析包含以下内容的JSON:

"images": [                 
    "https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
    "https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
    "http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
            ]

问题在于我不知道如何在"图像"内部获取元素串。节点

我的代码目前看起来像:

if(c.has("images") && !c.isNull("images")){
    JSONArray imagenes = jsonObj.getJSONArray("images");
    for (int j = 0; j < imagenes.length(); j++) {
        JSONObject m = imagenes.getJSONObject(i);


    }
}

如何在不使用&#34;键&#34;?的情况下获取数组中的每个字符串 我不知道如何处理&#34; M&#34;下。

2 个答案:

答案 0 :(得分:2)

你想要一个字符串,而不是一个对象。字符串没有嵌套键。

使用getString(i)方法代替getJSONObject(i)

答案 1 :(得分:1)

您的dev.copy(png,'path/pngFile.png') plot(YData ~ XData, data = mydata) dev.off() 数组包含的images值不包含string个对象。因此,您需要获取json而不是string

jsonObject

<强>输出:

if(c.has("images") && !c.isNull("images")){
    JSONArray imagenes = jsonObj.getJSONArray("images");
    for (int j = 0; j < imagenes.length(); j++) {
        String imgURL = imagenes.optString(i);

        System.out.println(imgURL);   
    }
}