-1

I have some sample json data with objects only.

{
  "0": {
    "image": null,
    "title": "Government to issue new ₹500 and ₹2,000 notes from Nov 10 ",
    "time": "10:46 pm ",
    "date": "08 Nov ",
    "content": "The government will start issuing the new ₹500 and ₹2,000 currency notes from November 10, said ...",
    "link": "https://full-story.newsinshorts.com/v1/article/f0619b6a-738e-4470-897f-4e5cdad3ea52-1 "
  },
  "1": {
    "image": null,
    "title": "₹1,000, ₹5,000 and ₹10,000 notes were demonetised in 1978 ",
    "time": "10:20 pm ",
    "date": "08 Nov ",
    "content": "The Indian government had demonetised the ₹1,000, ₹5,000 and ₹10,000 currency notes in 1978 ...",
    "link": null
  },
  ...

}

It has more objects than what is visible. How can I proceed with parsing this data?

10
  • 1
    You can try this sample, I see it nearly same your json. Commented Nov 9, 2016 at 1:37
  • 1
    Possible duplicate of How to parse JSON in Android Commented Nov 9, 2016 at 2:05
  • Why you trying to parse make it as model class using GSON and use that model Commented Nov 9, 2016 at 2:29
  • @Saveen - How do you make an Object named 0? Commented Nov 9, 2016 at 3:06
  • @Cricket_007 Sir this json is not correct, Need json array Commented Nov 9, 2016 at 3:10

3 Answers 3

0

I assume the above JSON is gotten from a given source say web server. This is how I would handle it

   String yourJSON = {
          "0": {
            "image": null,
            "title": "Government to issue new ₹500 and ₹2,000 notes from Nov 10 ",
            "time": "10:46 pm ",
            "date": "08 Nov ",
            "content": "The government will start issuing the new ₹500 and ₹2,000 currency notes from November 10, said ...",
            "link": "https://full-story.newsinshorts.com/v1/article/f0619b6a-738e-4470-897f-4e5cdad3ea52-1 "
          },
          "1": {
            "image": null,
            "title": "₹1,000, ₹5,000 and ₹10,000 notes were demonetised in 1978 ",
            "time": "10:20 pm ",
            "date": "08 Nov ",
            "content": "The Indian government had demonetised the ₹1,000, ₹5,000 and ₹10,000 currency notes in 1978 ...",
            "link": null
          },
          ...

        };

        // create a JSON object to handle the response JSON
        JSONObject yourJSONObject = new JSONObject(yourJSON);

        /*
        *this converts entire JSON object to JSON array since there is no further nesting
        *this can be placed in a loop and the 0 replaced by an incremental variable say "int i"
        */
        JSONArray data = yourJSONObject.getJSONArray(String.valueOf(0));

        // loop through the items in the first JSON array which in this case is only one therefore at index 0
        for (int i = 0; i < data.length(); i++) {
            // create JSON object to hold each item in array
            JSONObject json = null;

            try {
                //Getting json
                json = data.getJSONObject(i);

                String image = json.getString("image");
                String title = json.getString("title");
                String time = json.getString("time");
                String date = json.getString("date");
                String content = json.getString("content");
                String link = json.getString("link");

                // perform action on this specific item


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

9 Comments

Does getJSONArray(0) compile?
According to the documentation, that method doesn't exist. There are no arrays in that JSON, anyways
It does compile since this is an extract of code from one of my working Android projects
The method signature is getJSONArray(String name), though, not an Integer... As I said, though, there isn't any array in that JSON
make sure it is String. Sorry for the misslead. Make it "0" or if you are using an incremental variable say int i then do 0+""+i
|
0

Ok ...

Try something like this ... just give you an idea ...

MyObject myObject = null;
JSONObject object = new JSONObject(response);
try{
    myObject = new MyObject();
    int position = 0;
    while(position < 100){
        JSONObject jsonObject = object.getJSONObject(String.valueOf(position));
        String title = jsonObject.getString("title");
        myObject.setTitle(title);

        ...


        position++;
    }
}catch (JSONException e){
    return e.getMessage();
}finally{
    return myObject;
}

Comments

0

Try this way i had same json response and it work for me

class LoadAllStates extends AsyncTask<String, String, ArrayList<String>> {
    private ProgressDialog pDialog;
    private String test;
    private ArrayList<String> statedata;
    private ArrayList<String> idadddata;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ShippingAddress.this.getActivity());
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected ArrayList<String> doInBackground(String... args) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        statedata = new ArrayList<String>();

        idadddata = new ArrayList<String>();


        String jsonStr = sh.makeServiceCall(GET_ADDRESS_DETAIL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {

                jsonObj = new JSONObject(jsonStr);
                for (int i = 1; i <= jsonObj.length(); i++) {

                    JSONObject user = jsonObj.getJSONObject(""+i);


                    idaddress=String.valueOf(i);
                    System.out.println("userr"+i);

                    username= (user.has("name")) ? user.getString("name") : null;
                    usermobile= (user.has("mobile_number")) ? user.getString("mobile_number") : null;
                    useraddress= (user.has("address")) ? user.getString("address") : null;
                    userlandmark= (user.has("landmark")) ? user.getString("landmark") : null;
                    usercity= (user.has("city")) ? user.getString("city") : null;
                    userstate= (user.has("state")) ? user.getString("state") : null;
                    userpincode= (user.has("pin_code")) ? user.getString("pin_code") : null;
                    useremail= (user.has("email")) ? user.getString("email") : null;
                    if(username!=null)
                        statedata.add(username+","+usermobile+","+useraddress+","+userlandmark+","+usercity+","+userstate+","+userpincode+","+useremail);

                    idadddata.add(idaddress);
                    Log.i("inner",user.toString());
                }
                System.out.println("WifeBday"+statedata.size());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return statedata;
    }

    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        pDialog.dismiss();
    }
}

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.