没有JSON数组的值

时间:2014-10-18 12:45:48

标签: android json arrays

Heyy guyz我正在接收JSON使用谷歌地方详细信息api然后在那个Json我正在寻找“评论”数组。但在logcat我收到系统错误,没有值的评论。但我检查了Json有存在的评论array.Where问题?Thanx提前。这是我的JSON数据:

   {
   "result" : {
   "address_components" : [
     {
        "long_name" : "Kalyan",
        "short_name" : "Kalyan",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "Maharashtra",
        "short_name" : "Maharashtra",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "India",
        "short_name" : "IN",
        "types" : [ "country", "political" ]
     },
     {
        "long_name" : "421301",
        "short_name" : "421301",
        "types" : [ "postal_code" ]
     }
      ],

     "reviews" : [
      {
         "aspects" : [
           {
              "rating" : 0,
              "type" : "overall"
           }
        ],
        "author_name" : "Kunal Korde",
        "author_url" : "https://plus.google.com/108837578005055609416",
        "language" : "en",
        "rating" : 1,
       "text" : "it is very bad to call domino  one and only kalyan 
        "time" : 1382351314
      },
     {
        "aspects" : [
           {
              "rating" : 3,
              "type" : "food"
           },
           {
              "rating" : 2,
              "type" : "decor"
           },
           {
              "rating" : 1,
              "type" : "service"
           }
        ],
        "author_name" : "Hits Daiya",
        "author_url" : "https://plus.google.com/101565870698816750539",
        "language" : "en",
        "rating" : 4,
        "text" : "wt a excellent food anybody can get here! I wanna to say that wt a 
        "time" : 1371385367
        },
      {
        "aspects" : [
           {
              "rating" : 1,
              "type" : "overall"
           }
        ],
        "author_name" : "nirmit jallawar",
        "author_url" : "https://plus.google.com/116255076196839398528",
        "language" : "en",
        "rating" : 3,
        "text" : "Good but a bit more of standard is necessary ",
        "time" : 1402139860
     },
     {
        "aspects" : [
           {
              "rating" : 2,
              "type" : "food"
           },
           {
              "rating" : 0,
              "type" : "decor"
           },
           {
              "rating" : 1,
              "type" : "service"
           }
        ],
        "author_name" : "Rutam Mokashi",
        "author_url" : "https://plus.google.com/112151348544733227698",
        "language" : "en",
        "rating" : 3,
        "text" : "best place for pizzas in kalyan...",
        "time" : 1353151680
     },
     {
        "aspects" : [
           {
              "rating" : 1,
              "type" : "food"
           },
           {
              "rating" : 2,
              "type" : "decor"
           },
           {
              "rating" : 2,
              "type" : "service"
           }
        ],
        "author_name" : "A.A Varghese varghese",
        "author_url" : "https://plus.google.com/103110235851606786394",
        "language" : "en",
        "rating" : 4,
        "text" : "nice hotel.",
        "time" : 1375596316
     }
  ],
  "scope" : "GOOGLE",
  "types" : [ "restaurant", "food", "establishment" ],
  "url" : "https://plus.google.com/107013844902194125587/about?hl=en-US",
  "user_ratings_total" : 32,
  "utc_offset" : 330,
  "vicinity" : "Plot No. C 1, Ground Floor, Shop No. 2, 3 & 4, Chikanghar, Kalyan",
  "website" : "http://www.dominos.co.in/"
   },
   "status" : "OK"
  }

这是我的代码:

   public class Review_activity extends ActivityGroup {
   protected static LocalActivityManager mLocalActivityManager;


  // Alert Dialog Manager
   AlertDialogManager alert = new AlertDialogManager();
    ProgressDialog pDialog;
    JSONObject json = null;
  // Review Listview
    ListView list;
    String reference;

    public static final String TAG_name = "-NA-";
    public static final String TAG_rating = "-NA-";
    public static final String TAG_text = "-NA-";
    public static String reference_value = "reference";
    public static String KEY_REFERENCE = "reference";// id of the place
     private static final String TEL_PREFIX = "tel:";
    ArrayList<HashMap<String, String>> oslist = newArrayList<HashMap<String,String>>();
    //  public static String url = "";


    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.review);
    oslist = new ArrayList<HashMap<String, String>>();


 // Getting listview
    list = (ListView) findViewById(R.id.list);

    // Getting place reference from the map
     reference = getIntent().getStringExtra("reference");
     Log.d("yogesh", reference);

    String sb ="https://maps.googleapis.com/maps/api/place/details/json?";


    String sb1=sb.concat("&reference="+reference);

    String sb2=sb1.concat("&sensor=true");

    String sb3=sb2.concat("&key=AIzaSyChVcy-8fLkAq5-ZJCuNomF1lIf-Gda7s8");

    String url=sb3;
    Log.d("URL", url);
    new JSONParse().execute(url);
 }
private class JSONParse extends AsyncTask<String, String, JSONObject> {

   @Override
     protected void onPreExecute() {
         super.onPreExecute();

         pDialog = new ProgressDialog(Review_activity.this);
         pDialog.setMessage("Getting Reviews ...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(true);
         pDialog.show();
   }
   @Override
     protected JSONObject doInBackground(String... params) {
     JSONParser jParser = new JSONParser();
     // Getting JSON from URL
     String s = params[0];
     Log.d("URL", s);
     JSONObject json = jParser.getJSONFromUrl(s);
     return json;
   }
    @Override
      protected void onPostExecute(JSONObject json) {
      pDialog.dismiss();
      try {
         // Getting JSON Array from URL
          JSONArray jArray = json.getJSONArray("reviews");

            int k;
            k=jArray.length();
            k--;
            String Alength=String.valueOf(k);
            Log.d("Array length", Alength);
            //To get the items from the array
            int j=0;

            for (int i=0;i<=k;i++)
            {
            JSONObject r1 = jArray.getJSONObject(j);
             String aname = r1.getString("author_name");
             String arating = r1.getString("rating");
             String text = r1.getString("text");


            Log.d("review", aname);
            Log.d("review", arating);
            Log.d("review", text);
            j++;


         // Adding value HashMap key => value
         HashMap<String, String> map = new HashMap<String, String>();
         map.put(TAG_name, aname);
         map.put(TAG_rating, arating);
         map.put(TAG_text,text );
         oslist.add(map);
            }
         list=(ListView)findViewById(R.id.list);
         ListAdapter adapter = new SimpleAdapter(Review_activity.this, oslist,
             R.layout.review_item,
             new String[] { TAG_name,TAG_rating, TAG_text }, new int[] {
                 R.id.textView2,R.id.textView4, R.id.textView6});
         list.setAdapter(adapter);


     } catch (JSONException e) {
       e.printStackTrace();
     }
    }


     }

    }

2 个答案:

答案 0 :(得分:1)

我发现你的json不是一个有效的json

使用check if json is valid or not来检查有效的json

我纠正它,有效的json在这里

{
"result": {
    "address_components": [
        {
            "long_name": "Kalyan",
            "short_name": "Kalyan",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "Maharashtra",
            "short_name": "Maharashtra",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "India",
            "short_name": "IN",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "421301",
            "short_name": "421301",
            "types": [
                "postal_code"
            ]
        }
    ],
    "reviews": [
        {
            "aspects": [
                {
                    "rating": 0,
                    "type": "overall"
                }
            ],
            "author_name": "Kunal Korde",
            "author_url": "https://plus.google.com/108837578005055609416",
            "language": "en",
            "rating": 1,
            "text": "it is very bad to call domino  one and only kalyan",
            "time": 1382351314
        },
        {
            "aspects": [
                {
                    "rating": 3,
                    "type": "food"
                },
                {
                    "rating": 2,
                    "type": "decor"
                },
                {
                    "rating": 1,
                    "type": "service"
                }
            ],
            "author_name": "HitsDaiya",
            "author_url": "https: //plus.google.com/101565870698816750539",
            "language": "en",
            "rating": 4,
            "text": "wtaexcellentfoodanybodycangethere!Iwannatosaythatwta",
            "time": 1371385367
        },
        {
            "aspects": [
                {
                    "rating": 1,
                    "type": "overall"
                }
            ],
            "author_name": "nirmitjallawar",
            "author_url": "https: //plus.google.com/116255076196839398528",
            "language": "en",
            "rating": 3,
            "text": "Goodbutabitmoreofstandardisnecessary",
            "time": 1402139860
        },
        {
            "aspects": [
                {
                    "rating": 2,
                    "type": "food"
                },
                {
                    "rating": 0,
                    "type": "decor"
                },
                {
                    "rating": 1,
                    "type": "service"
                }
            ],
            "author_name": "RutamMokashi",
            "author_url": "https: //plus.google.com/112151348544733227698",
            "language": "en",
            "rating": 3,
            "text": "bestplaceforpizzasinkalyan...",
            "time": 1353151680
        },
        {
            "aspects": [
                {
                    "rating": 1,
                    "type": "food"
                },
                {
                    "rating": 2,
                    "type": "decor"
                },
                {
                    "rating": 2,
                    "type": "service"
                }
            ],
            "author_name": "A.AVarghesevarghese",
            "author_url": "https: //plus.google.com/103110235851606786394",
            "language": "en",
            "rating": 4,
            "text": "nicehotel.",
            "time": 1375596316
        }
    ],
    "scope": "GOOGLE",
    "types": [
        "restaurant",
        "food",
        "establishment"
    ],
    "url": "https: //plus.google.com/107013844902194125587/about?hl=en-US",
    "user_ratings_total": 32,
    "utc_offset": 330,
    "vicinity": "PlotNo C1 GroundFloor ShopNo 2 3 4 Chikanghar Kalyan",
    "website": "http: //www.dominos.co.in/"
},
"status": "OK"

}

我发现你的代码看起来非常正确......

更安全的一面是首先打印响应json并检查它是否有效....

答案 1 :(得分:0)

问题是你首先得到json对象&#34;结果&#34;然后使用该对象查找json数组以供查看。

 jsonobject = json.getJsonObject("result");
 resultArray = jsonobject.getJsonArray("review");