1

Need Help

I have Json through which i have to retrieve objects but unable to retrieve. I have used multiple objects also to retrieve but no success. JSON:

{
    "status": 1,
    "data": [{
        "restaurent_id": "1",
        "user_id": "6",
        "zone_id": "1",
        "restaurentAddress": {
            "restaurent_address_id": "1"
        },
        "restaurentInfo": {
            "restaurent_info_id": "1",
            "restaurent_bussiness_owner_name": "Vijay"
        },
        "restaurentSetting": {
            "restaurent_setting_id": "1",
            "minimum_purcase": "200",
            "payment_method_id": "1",
            "title": "Best Hotel"
        },
        "zone": {
            "zone_id": "1",
            "by_zipcode": "1"
        }

    }]
}

and i want to fetch restaurentAddress and restaurentInfo

MY mainActivity.java file

package com.example.premi.jsonlist;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView output = (TextView) findViewById(R.id.textView1);
        String strJson="{\n" +
                "\t\"status\": 1,\n" +
                "\t\"data\": [{\n" +
                "\t\t\"restaurent_id\": \"1\",\n" +
                "\t\t\"user_id\": \"6\",\n" +
                "\t\t\"zone_id\": \"1\",\n" +
                "\t\t\"restaurentAddress\": {\n" +
                "\t\t\t\"restaurent_address_id\": \"1\"\n" +
                "\t\t},\n" +
                "\t\t\"restaurentInfo\": {\n" +
                "\t\t\t\"restaurent_info_id\": \"1\",\n" +
                "\t\t\t\"restaurent_bussiness_owner_name\": \"Vijay\"\n" +
                "\t\t},\n" +
                "\t\t\"restaurentSetting\": {\n" +
                "\t\t\t\"restaurent_setting_id\": \"1\",\n" +
                "\t\t\t\"minimum_purcase\": \"200\",\n" +
                "\t\t\t\"payment_method_id\": \"1\",\n" +
                "\t\t\t\"title\": \"Best Hotel\"\n" +
                "\t\t},\n" +
                "\t\t\"zone\": {\n" +
                "\t\t\t\"zone_id\": \"1\",\n" +
                "\t\t\t\"by_zipcode\": \"1\"\n" +
                "\t\t}\n" +
                "\n" +
                "\t}]\n" +
                "}";
        String dataoutput = "";
        try {
            JSONObject  jsonRootObject = new JSONObject(strJson);

            //Get the instance of JSONArray that contains JSONObjects
            JSONObject status = jsonRootObject.optJSONObject("status");
            JSONArray Dataarray =status.getJSONArray("data");

            //Iterate the jsonArray and print the info of JSONObjects
            for(int i=0; i < Dataarray.length(); i++){
                JSONObject jsonObject = Dataarray.getJSONObject(i);
JSONObject Data = jsonObject.getJSONObject("restaurentAddress");

                for(int j = 0 ; j < Data.length(); j++){
                    JSONObject GetData =Data.getJSONObject(String.valueOf(j));
                int id = Integer.parseInt(GetData.getString("restaurent_address_id"));
                String postcode = GetData.getString("postcode");
                String addresss = GetData.getString("restaurent_address");


                dataoutput += " : \n id= "+ id +" \n postcode= "+ postcode +" \n address= "+ addresss +" \n ";
            }}
            output.setText(dataoutput);
        } catch (JSONException e) {e.printStackTrace();}
    }
}
2
  • 3
    what's the problem? any stacktrace? Commented Jul 2, 2016 at 19:21
  • 07-03 03:54:08.988 2987-2987/com.example.premi.jsonlist E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.premi.jsonlist.MainActivity.access$super Commented Jul 2, 2016 at 20:00

2 Answers 2

2

Try this out:

try {
    JSONObject object = (JSONObject) new JSONTokener(YOUR_JSON_STRING).nextValue();
    String restaurentAddressId = object.getJSONArray("data").getJSONObject(0).getJSONObject("restaurentAddress").getString("restaurent_address_id");
    String restaurentInfoId = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_info_id");
    String restaurentBizOwnerName = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_business_owner_name");

}
catch (JSONException e) {
}
Sign up to request clarification or add additional context in comments.

4 Comments

07-03 04:09:21.584 30798-30798/com.example.premi.jsonlist E/Trace: error opening trace file: No such file or directory (2) 07-03 04:09:21.616 30798-30798/com.example.premi.jsonlist E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
It seems like you are trying to use a method from an API level, API 19 for ArrayMap, that isn't supported by the device you're testing it on. I'm not familiar with this issue but check out stackoverflow.com/questions/36812913/… and stackoverflow.com/questions/36930200/…
I need one more help if i am paasing json locally it is working and when i m using as api (URL) its giving error its showing blank... what will be the reason ? is their any way to first store json locally then retrieve ?
not sure what you mean, but it needs to be in JSON string format for you to parse it. And you can store the JSON string in SharedPreferences to later on retrieve it if I understand you correctly.
1

Its Done I tried this

   try {
        JSONObject  jsonRootObject = new JSONObject(strJson);

        //Get the instance of JSONArray that contains JSONObjects
        JSONArray mainnode =jsonRootObject.getJSONArray("data");

        //Iterate the jsonArray and print the info of JSONObjects
        for(int i=0; i < mainnode.length(); i++){
            JSONObject jsonObject = mainnode.getJSONObject(i);
            JSONObject Data = jsonObject.getJSONObject("restaurentInfo");
                int id = Integer.parseInt(Data.getString("restaurent_info_id"));
                String postcode = Data.getString("restaurent_phone_number");
                String addresss = Data.getString("restaurent_bussiness_owner_name");


                dataoutput += " : \n restaurent_id= "+ id +" \n restaurent_info_id= "+ postcode +" \n restaurent_address_id= "+ addresss +" \n ";

        }
        output.setText(dataoutput);
    } catch (JSONException e) {e.printStackTrace();}
}

I just removed Status Object

and another for loop Thanks For the Help Got little Bit Help from You :)

1 Comment

No problem, glad you figured it out! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.