我想读这个JSONArray,有点困惑..请指导我

时间:2016-10-01 12:58:12

标签: android arrays json

如何阅读图像及其相应位置

 "images": [
  [
    {
      "images_url": "http://provenlogic.info/tinder_web/public/uploads/e9275d47cf5efd929794caafc50e957982c47582.jpg",
      "position": "1"
    },
    {
      "images_url": "http://provenlogic.info/tinder_web/public/uploads/c374561da8583a77b4d21ee4b06f30d1a3fac4bb.jpg",
      "position": "3"
    }
  ]
]

3 个答案:

答案 0 :(得分:0)

try {
            JSONArray jsonArray = rootObject.getJSONArray("images");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String imageUrl=jsonObject.getString("images_url");
                String position=jsonObject.getString("position");
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

Happy Codding !!!

答案 1 :(得分:0)

JSON对象以{开头,以{结尾},而JSON数组以[以#结尾]开头。

JSONArray jArray = json.getJSONArray("images").get;

for(int i = 0; i < jArray.length;i++)
{
   JSONObject jObj = jsonArray.getJSONObject(i);

   String imageUrl = jObj.getString("images_url");
   String position = jObj.getString("position");
}

您需要将整个JSON处理包装在try / catch块中。您可能想尝试http://jsonlint.com/,一个在线json验证器和https://jsonformatter.curiousconcept.com/,一个在线json格式化器,可以轻松理解!

干杯!

答案 2 :(得分:0)

  

首先使用数组名称“images”

获取JsonArray      

并获取每个Json对象。

try {
            JSONArray jsonArray = new JSONArray("result");
            for(int i=0;i<jsonArray.length();i++){
                JSONArray jsonArray1=jsonArray.getJSONArray(i);
                for(int j=0;j<jsonArray1.length();j++){
                    JSONObject jsonObject=jsonArray1.getJSONObject(j);
                    String imageurl=jsonObject.getString("images_url");
                    String position=jsonObject.getString("position");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
相关问题