-1

我正在尝试在使用 google api 的地方解析 json 文件中的一些数据我想获取“名称”,然后将其保存在列表中,但我得到 json 异常,这是我的文件

{
   "html_attributions" : [],
   "next_page_token" : "ClRHAAAAyPJvpPTgNnOURBi9XhfVvw5J2Howz_h41h1HysWrYomdpQrRwMnWeKRKyiGU6ku6ZxxzQ3fLb598QgwyYYIF9PiK6ynAaBj-XOnbr7s2eTESEHTjEzwG8k6iwf3aFGTmeh0aFAH6KblOYgOw80zn7E-NgcdahjZl",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 30.03735730,
               "lng" : 31.2324840
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 30.04394240,
                  "lng" : 31.23577240
               },
               "southwest" : {
                  "lat" : 30.03077210,
                  "lng" : 31.22919559999999
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "b466d997e9c8b69f328895a42143f1da8b445e28",
         "name" : "Garden City",
         "reference" : "CqQBnAAAAHWLMK-4WmAwrZBlBfrSCs8ycrG7voN6sT-vFtqDUhVuxIVHJoAyA2JjVqzCSlJfJcI8DqUg1PFQUwQMDJvZinsx_-YJtYhr01Oa3wvj-CUvGR8m3WlxQEWUB0_tsm6mt5WDCEsMsnmhgRmgrOYmAMePGI_DndEBkOJjhqLoEo1VZblKaa730ZsBe1Wjp_NAYHm1reTeiHlBW5dMVhmCX7cSEAwbfJ3xpIJ_FUCoSlM-5hkaFM3vnuXUv1KVws2t-1L0CR8ldSRB",
         "types" : [ "neighborhood", "political" ],
         "vicinity" : "Cairo"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 30.0429350,
               "lng" : 31.2246040
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "710a28e1d1e2aefdd69ea6ec2e30fbfc9f5b54cb",
         "name" : "Cairo Opera House",
         "photos" : [
            {
               "height" : 478,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRsAAAAJxKE7CfYmG89VEHODwClX4P3XbKD0h1j_xghhy0KvINsO9KMNWxkxq65xXPPXzZHWX104qjm3TTJaxh1mUeATTkRspBOVJll0JwZ-8fA_0DaXJ2RVr4pE7rPAKYkW9VE9qwIQ7ba_nvg5j7Nua96ihIQdEM9euo3M8jXH8CdpSj5ixoUq-W9Me_M9edgTscsjn5pBhnOTt8",
               "width" : 613
            }
         ],
         "rating" : 4.50,
         "reference" : "CoQBfAAAAD6AaWl7Ze1hxuelIZZkH8dD0VrsBBIpDOkfjwSItQ276DnGHiLYc4BEcPGyNuw_OPX7HfaPuIYFvN-ctpgcIV2tC5uw3nPP_Z4OTb5y0zD0CaelxZN3a-m0tBiqZHPylp7FpXOHERNc6BUInaWO3_h4Aze7M_FuOw21-zttSBvnEhBl62ERpUcU8nZIJy7DBrKuGhQn65AeXttY1axK8Qkvn7nKxDOuqA",
         "types" : [ "establishment" ],
         "vicinity" : "Opera Land, EL GEZIRAH"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 30.0459720,
               "lng" : 31.224110
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "fc80f712dc787d1c2c4382992e25895b8994bcdd",
         "name" : "Cairo Tower",
         "photos" : [
            {
               "height" : 1024,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAAKUsL4sanCSNRjYRUSPbEbUoK73LuBWW36PuIAqebOTu3llxvJwtsQmtMpE_aqXjwtkZwjinZQqXWWd_8XQDGRtDPpKj5l97Jy02_zsT8KBLueOgaX4LlU0fAxKvQaM3SMJPehF2-x_qb0yn0Pbm4eRIQgX2VP1lXHnrefEKxk_NIshoUYZXYJAsB-z64TzWV4d6ACAxJqbo",
               "width" : 768
            }
         ],

我试过类似的东西

 String data = EntityUtils.toString(response.getEntity());


           JSONObject jsonObj = new JSONObject(data);
           Object jj=jsonObj.getJSONObject("results").get("name"); 

但我得到了 JSONEXCEPTION

4

1 回答 1

1

results 是一个数组而不是一个对象:

JSONObject jsonObj = new JSONObject(data);    
JSONArray results =jsonObj.getJSONArray("results");

for (int i = 0; i < results.length(); i++) {
  JSONObject result = results.getJSONObject(i);

  String name = result.getString("name");
  Log.d("tag", "name: " + name)
}

这里没有编译器,所以无法检查语法。

于 2013-01-08T16:20:05.680 回答