2014-10-18 35 views
0

Heyy guyz我收到JSON使用谷歌地方的詳細信息api然後在那個JSON我在找「評論」array.But在logcat我得到系統錯誤,評論沒有值。但我檢查?的JSON存在審查array.Where是問題感謝名單中advance.This是我的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" : "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(); 
    } 
    } 


    } 

    } 

回答

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,檢查其有效與否....

+0

\ @Angad是正確的JSON不就行了48個有效...解析錯誤: ......「文本「:」這是非常糟糕的c -----------------------^ 期待'STRING','NUMBER','NULL',' TRUE','FALSE','{','[' – 2014-10-18 13:01:44

+0

我怎樣才能打印JSON在logcat中的響應 – user3930098 2014-10-18 13:06:11

+0

Log.i(「json response」,json.toString()); – 2014-10-18 13:07:00

0

的問題是,你首先得JSON對象「結果」然後使用該對象來查找json數組以供查看。

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