2017-03-02 131 views
0

我正在做一个移动应用程序,它从我自己的API中检索信息。我正在尝试在JSON中获取餐厅详细信息并解析它们以显示。这里是我得到的错误:JSON解析错误没有变量值

D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1 
I/Timeline: Timeline: Activity_idle id: [email protected] time:148477558 
E/MainActivity: Response from url: { 
       "address1": "Market Square, Smithfield, Dublin Dublin 7", 
       "address2": "Dublin 7", 
       "cost": 35, 
       "lat": 53.3489980000, 
       "lng": -6.2788120000, 
       "menu_type": "BBQ", 
       "name": "My Meat Wagon", 
       "offer": "Meal for 10\u20ac", 
       "phone": 53463267, 
       "rate": 4.1 
      } 
E/MainActivity: Json parsing error: No value for restaurants 
D/ViewRootImpl: #3 mView = null 

,这里是我使用的代码:当只有一个结果

protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       JSONArray restaurants = jsonObj.getJSONArray("restaurants"); 

       // looping through the JSON object 
        JSONObject c = restaurants.getJSONObject(0); 

        String name = c.getString("name"); 
        String address1 = c.getString("address1"); 
        String address2 = c.getString("address2"); 
        String lat = c.getString("lat"); 
        String lng = c.getString("lng"); 
        String cost = c.getString("cost"); 
        String menu_type = c.getString("menu_type"); 
        String rate = c.getString("rate"); 
        String offer = c.getString("offer"); 


        // Phone node is JSON Object 
        String mobile = c.getString("mobile"); 
        // tmp hash map for single restaurant 
        HashMap<String, String> restaurant = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        restaurant.put("name", name); 
        restaurant.put("address1", address1); 
        restaurant.put("address2", address2); 
        restaurant.put("lat", lat); 
        restaurant.put("lng", lng); 
        restaurant.put("cost", cost); 
        restaurant.put("menu_type", menu_type); 
        restaurant.put("rate", rate); 
        restaurant.put("offer", offer); 
        restaurant.put("mobile", mobile); 

        contactList.add(restaurant); 

      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       MainActivity.this, contactList, 
       R.layout.list_item, new String[]{"name", "address1", 
       "address2","lat","lng","menu_type","Phone","rate","offer","cost"}, new int[]{R.id.name, 
       R.id.address1, R.id.address2,R.id.lat,R.id.lng,R.id.menu,R.id.mobile,R.id.rate,R.id.offer,R.id.cost}); 

     lv.setAdapter(adapter); 
    } 

} 
    } 
+0

您打印的JSON对象'jsonStr',没有名为“餐馆”的节点。 –

+0

“餐厅没有价值”你的json对象中没有这样的关键字,那么你期望什么? – njzk2

回答

0

我会建议使用Google Gson库解析JSON字符串。

基本上你需要导入库,创建相应的POJO并调用gson.fromJson(jsonStringToParseFrom,YourPOJOClassName.class)。所以,你的doInBackground会像:

protected Void doInBackground(Void... arg0) { 
    HttpHandler sh = new HttpHandler(); 

    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url); 

    Gson gson = new Gson(); 
    YourPOJOClass parsedResponse = gson.fromJson(jsonStr, YourPOJOClass.class); 

如果将在GSON你会得到什么地方出了错灵通的例外中的某些分析问题,你将摆脱掉所有的样板解析代码。