2016-08-22 84 views
2

我必须在url中通过json获取文本。如何在json中访问对象>数组>对象>数组>对象?

层次结构在下面给出:

对象>阵列>对象>阵列>对象。

我想文字与此代码。但我得到错误:org.json.JSONException: No value for text

下面是代码: - JSON数据

public class ListViewActivity extends Activity { 
    // Log tag 
    private static final String TAG = ListViewActivity.class.getSimpleName(); 
    // change here url of server api 
    private static final String url = "http://2e9b8f52.ngrok.io/api/v1/restaurants?per_page=5&km=1&location=true&lat=19.0558306414&long=72.8339840099"; 
    private ProgressDialog pDialog; 
    private List<Movie> movieList = new ArrayList<Movie>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_listview); 
     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, movieList); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Movie movie = movieList.get(position); 
       Intent intent = new Intent(ListViewActivity.this, SecondActivity.class); 
       intent.putExtra("name", movie.getName()); 
       intent.putExtra("average_ratings", movie.getAverage_ratings()); 
       intent.putExtra("full_address", movie.getAddress()); 
       intent.putExtra("image_url", movie.getThumbnailUrl()); 
       intent.putExtra("cuisine",movie.getCuisine()); 
       intent.putExtra("cost",movie.getCost()); 
       startActivity(intent); 

      } 
     }); 
     listView.setAdapter(adapter); 
     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Please Keep patience.Its loading..."); 

     pDialog.show(); 

     // Creating volley request obj 
     JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET, 
       url, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, response.toString()); 
       JSONArray 

         restaurantsJSONArray= null; 
       try { 
        restaurantsJSONArray = response.getJSONArray("restaurants"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       hidePDialog(); 
       // Parsing json 
       for (int i = 0; i < restaurantsJSONArray.length(); i++) { 
        try { 

         JSONObject obj =restaurantsJSONArray.getJSONObject(i); 
         Movie movie = new Movie(); 
         //movie.setTitle(obj.getString("title")); 
         movie.setName(obj.getString("name")); 
         //movie.setThumbnailUrl(obj.getString("image")); 
         movie.setThumbnailUrl(obj.getString("org_image_url")); 
         movie.setAverage_ratings(obj.getString("average_ratings")); 
         movie.setCuisine(obj.getString("cuisine")); 
         movie.setAddress(obj.getJSONObject("address").getString("area")); 
         // movie.setAddress(obj.getJSONObject("address").getString("full_address")); 
         movie.setCost(obj.getString("cost")); 
         movie.setDistance(obj.getDouble("distance")); 
         movie.settext(obj.getString("text")); 
         movieList.add(movie); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }); 


     AppController.getInstance().addToRequestQueue(movieReq); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 



} 

我附上快照。在快照中,我可以看到我必须访问的颜色“文字=帐单的15%折扣”。 enter image description here

+0

hy你可以发布你的json字符串 –

+0

使用[gson](https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ )或[杰克逊](https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/)进行对象映射。 – KDeogharkar

+0

@SathyaBaman你可以击中这个 http://2e9b8f52.ngrok.io/api/v1/restaurants?per_page=10&km=2&location=true&lat=19.0558306414&long=72.8339840099 – user6313669

回答

1
 JSONArray restaurantsJSONArray= null; 
         try { 
          restaurantsJSONArray = response.getJSONArray("restaurants"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

         hidePDialog(); 
         // Parsing json 
         for (int i = 0; i < restaurantsJSONArray.length(); i++) { 
          try { 

           JSONObject obj =restaurantsJSONArray.getJSONObject(i); 
           Movie movie = new Movie(); 
           //movie.setTitle(obj.getString("title")); 
           movie.setName(obj.getString("name")); 


    JSONArray textJSONArray= obj.getJSONArray("restaurant_offers"); 

    for (int j = 0; j < textJSONArray.length(); j++) { 

     JSONObject txtobj =textJSONArray.getJSONObject(i); 
    movie.settext(txtobj .getString("text")); 
    }  
           movieList.add(movie); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

试试这个代码restaurant_offers是一个JSONArray这样你就可以分析这样

+0

for(int j = 0; j user6313669

0

您可以分析这样

JSONObject apiResponseJsonObject= // Your API Response 
     try { 

      JSONArray restaurantJSONArray = apiResponseJsonObject.getJSONArray("restaurants"); 
      // you can get any text from an object like this 
      restaurantJSONArray.getJSONObject(index).getString("name"); 
      restaurantJSONArray.getJSONObject(index).getString("cost"); // Like this 

      //If you want to access phone numbers of specific object 
      JSONArray phoneJSONArray=restaurantJSONArray.getJSONObject(index).getJSONArray("phone_numbers"); 
      // And you can get specific data from phoneNumber like this 
      phoneJSONArray.getJSONObject(phoneNumberIndex).getString("number"); 


      //TO get Address, you can use like this 
      JSONObject addressJSONObject=restaurantJSONArray.getJSONObject(index).getJSONObject("address"); 

      //Like this you can parse whatever you want. 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
0

您必须更改像这样

JSONObject obj =restaurantsJSONArray.getJSONObject(i); 
    JSONArray restauranstOffersJSONArray = obj.getJSONArray("restaurants_offers"); 
    for (int i = 0; i < restauranstOffersJSONArray.length(); i++) { 
     JSONObject offersObj = restauranstOffersJSONArray.get(i); 
     String text = offersObj.getString("text"); 
    } 
环内容
2
try { 
    String yourresponseString ="";// this string refer to your api response 
    JSONObject jsonObject = new JSONObject(yourresponseString); 
    JSONArray objJsonArray = new JSONArray(jsonObject.getString("restaurants")); 
    for (int i = 0; i < objJsonArray.length(); i++) { 
     JSONArray objInnerJsonArray = objJsonArray.getJSONObject(i).getJSONArray("restaurant_offers"); 
     for (int j = 0; j < objInnerJsonArray.length(); j++) { 
      //Here you can acess youe bill discount value 
      JSONObject objInnerJSONObject = objInnerJsonArray.getJSONObject(j); 
      System.out.println("Discount==>" + objInnerJSONObject.getString("text")); 
     } 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

你可以像这样解析。使用这个类可以解析任何类型的层次结构。