2

I have to fetch text via json in url .

The hierarchy is given below :

object>array>object>array>object.

I want to get text with this code .But I am getting error :org.json.JSONException: No value for text

Below is the code :-

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;
        }
    }



}

I am attaching snapshot of json data. In the snapshot we can see the color "Text=15% discount on bill " i have to access .enter image description here

10
  • hy can you post your json string Commented Aug 22, 2016 at 10:40
  • use gson or jackson for object mapping. Commented Aug 22, 2016 at 10:42
  • @SathyaBaman You can get by hit this 2e9b8f52.ngrok.io/api/v1/… Commented Aug 22, 2016 at 10:42
  • Possible duplicate of Parse json(android) Commented Aug 22, 2016 at 10:43
  • @shubhamsharma for backend ruby on rails(Mysql) using Commented Aug 22, 2016 at 10:51

4 Answers 4

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();
}

You can parse like this.And using this classes you can parse any type of hierarchy.

Sign up to request clarification or add additional context in comments.

Comments

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();
                            }
                        }

try this code restaurant_offers is a JSONArray so you can parse like this

1 Comment

for ( int j = 0; j < textJSONArray.length(); j++) should replace with for (int i = 0; i < textJSONArray.length(); i++) Please edit your answer .
0

You can parse like this

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();
        }

Comments

0

You must change the for loop content like this

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");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.