-2

I'm trying to parse multiple objects,Bellow i'm receiving Json Sample The requirement completed and my question now outdated, can someone please up-vote to help me for asking next question? Will he helpfull for and thanks for

 {
    "0": //outer objects are multiples, i just post one object for sample
    {
        "id": "1",      
        "name": "B2 MR1",
        "description": 
        {
            "0": //it is also multiple objects m just showing one
            {

                "title": "Carve the Future",
                "description": "Welcome to Meeting Room 1",
                "push_notification": "Carve the Future",

            }
        }
    },//after that the next obj will be show
  .
  .
}

In second object 1 i also have above keys and values, i can't handel it, here is my Model.

 public class JsonModel {   

      private String id;    //getter setter
      private String name;  //getter setter
     List<InnerDescprtion> description;  //getter setter
    }

Here is my InnerDescprtion Model

 private class InnerDescprtion {
       private String id; //getter setter
       private String title;  //getter setter
     }

And below is my java code for parsing it using Gson,

   JsonModel outterModelClass= null;
                    List<JsonModel> listObj = new ArrayList<>();
                    for (int i = 0; i < responseJson.length(); i++) {

                        try {
                            outterModelClass= new Gson().fromJson(responseJson.getString(String.valueOf(i)), JsonModel.class);
                            listObj.add(outterModelClass); //here i'm getting exception,,
                        } catch (JSONException e) {
                            e.printStackTrace();
                             }
                    }

I get the solution, Please up-vote to help me.

Learner313
  • 11
  • 7

3 Answers3

0

If it is possible for you I would change the json to something like this:

 [{
        "id": "1",
        "name": "B2 MR1",
        "description": [{
            "id" : "1-1",
            "title": "Carve the Future",
            "description": "Welcome to Meeting Room 1",
            "push_notification": "Carve the Future"
        }]
    },
    {
        "id": "2",
        "name": "B2 MR2",
        "description": [{
            "id" : "2-1",
            "title": "Carve the Future 2",
            "description": "Welcome to Meeting Room 2",
            "push_notification": "Carve the Future 2"
        }]
    }
 ]

Then your approach should work with just a few changes:

BufferedReader br = new BufferedReader(new FileReader("c:/test/test.json"));
Type listType = new TypeToken<ArrayList<JsonModel>>(){}.getType();

List<JsonModel> outterModels = new Gson().fromJson(br, listType);

If you can't change the json I would suggest to use another JSON library like json simple and extract everything manually.

JanTheGun
  • 2,165
  • 17
  • 25
0

Your 'listObj' should be defined this way:

ArrayList<JsonModel> listObj = new ArrayList<JsonModel>();
Manzurul
  • 633
  • 6
  • 11
0

Well that is a nasty looking JSON. However I recommend you use volley android library. I had a task with somewhat similar problem. Only there was a single object inside of another object. To include volley in your project, update your build.gradle app module with compile 'com.android.volley:volley:1.0.0' inside dependencies{}. baseUrl is the url where you are fetching the JSON from.

Then you can do something like:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            baseUrl,
            null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        // Parsing json object response
                        // response will be a json object
                        for (int i=0; i<response.length(); i++){
                            JSONObject obj = response.getJSONObject(i);

                            //id
                            //name
                            try{
                                for (int j=0; j<obj.length() ; j++) {
                                JSONObject description = obj.getJSONObject(j);
                                //title
                                //description
                                //push notification

                                }
                            } catch (JSONException e) {
                                 e.printStackTrace();
                                Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                            }


                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                    hidepDialog();

                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            VolleyLog.d(TAG,"Error: "+ volleyError.getMessage() );
            Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            hidepDialog();
        }
    });
    //adding request to request queue
    AppController.getmInstance().addToRequestQueue(jsonObjReq);

Add this in your parseJSON(){} method or whatever you've named it.

I have not tried doing what you are trying do to. But it seems doable, with the use of volley library.

ravi
  • 899
  • 8
  • 31