0

I want to get a part of list json from a whole nested one. I have a json that looks like following:

{
  "response": 200,
  "responseMsg": "Allright",
  "location": [
    {
      "stateId": 1,
      "stateName": "West Bengal",
      "district": [
        {
          "districtId": 15,
          "districtName": "abc",
          "village": [
            {
              "villageId": 121,
              "villageName": "ABC"
            },
            {
              "villageId": 90,
              "villageName": "XYZ"
            }
          ]
        },
        {
          "districtId": 11,
          "districtName": "xyz",
          "village": [
            {
              "villageId": 58,
              "villageName": "PQR"
            }
          ]
        }
      ]
    }
  ]
}

I have written the bean files as following : Details.java:

public class Details {

    private int response = 0;
    private String responseMsg = null;
    private List<State> states = null;
public List<State> getLocation() {
        return location;
    }  

State.java:

public class State {

    private int stateId = 0;

    private String stateName=null;

    private List<District> district;

    public List<District> getDistrict() {
        return district;
    }

Now, I want only the State json differently so that I can then use use it as List to populate the spinner in android. For parsing the json, I am using

Gson googleJson = builder.create();Details details = googleJson.fromJson(result, Details.class);               
List<State> stateList = details.getLocation();

But when i again convert this to json using gson.toJson(stateList) this gives:

[
  {
    "district": [
      {
        "village": [
          {
            "villageName": "Mekhliganj",
            "villageId": 57
          }
        ],
        "districtName": "Cooch Bihar",
        "districtId": 10
      }
],"stateName=West Bengal","stateId":1
}
}

But this is other way round as state name goes in end when i again convert it to json. Also , this same json (stateList) gives null pointer exception when I again try to parse it as:

State stateObj = gson.fromJson(stateList,State.class);

What should be the correct way to do this ? i.e. get a part of json (list) from a whole using gson and parse that part ?

Community
  • 1
  • 1
Prakruti
  • 301
  • 1
  • 4
  • 14
  • Confusing question I think, please ellaborate with proper way, you mentioned Json with "states" but in your response there is no tag named states. Please explain proper so can help you out, – Pratik Dasa Jun 20 '17 at 06:34
  • @PratikDasa I meant location. I basically want a json that only has part of full json response i.e. a json with only list of states (and districts , villages as they are inside the state) – Prakruti Jun 20 '17 at 06:40
  • you must need to use GSON? And what is result vaiable? I mean string or array? @Prakruti – Pratik Dasa Jun 20 '17 at 06:59
  • Need to get the json as string and then use result as arraylist later again for spinner adapter. – Prakruti Jun 20 '17 at 07:04
  • I think these questions links will help you : (1) https://stackoverflow.com/questions/22753719/how-to-parse-json-parsing-using-gson-in-android (2) https://stackoverflow.com/questions/4556230/using-gson-in-android-to-parse-a-complex-json-object – Nitin Patel Jun 20 '17 at 07:32
  • Have you got the answer? @Prakruti – Pratik Dasa Aug 14 '17 at 09:37

2 Answers2

0

Got the solution for you...

You are not passing the whole response in below function

try this steps to get whole response :

1) Store the data in String or any object class you are storing.

2)

Gson gson = new Gson();
   String string = gson.toJson(response);

Note : You are just parsing remaining response. You are not parsing the original response. Just recheck the response when you parse in above function by putting break point on it.

Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
  • I have done the same thing to get the stae part of list as json. The problem is , I am not able to parse this json in to State Object. I want to parse only the sub part of response. It directly gives null exception when i do State stateObj = gson.fromJson(stateList,State.class); – Prakruti Jun 20 '17 at 07:42
0
public class Details {

    private int response = 0;
    private String responseMsg = null;
    private Location location = null;

    //add getter and setter

}

public class Location{
    private List<State>stateList;

//add getter and setter

}

Finally

public class State {

    private int stateId = 0;
    private String stateName=null;
    private List<District> district;

    //add getter and setter
}
Ait Bri
  • 498
  • 6
  • 15