简体   繁体   中英

Trouble parsing a json array inside a json array

I am having trouble parsing a simple json in java. Here is the sample json.

[
  {
    "politics": [
      {
        "type": "admin2",
        "friendly_type": "country",
        "name": "United States",
        "code": "usa"
      },
      {
        "type": "admin6",
        "friendly_type": "county",
        "name": "Gratiot",
        "code": "26_057"
      },
      {
        "type": "constituency",
        "friendly_type": "constituency",
        "name": "Eighth district, MI",
        "code": "26_08"
      },
      {
        "type": "admin6",
        "friendly_type": "county",
        "name": "Clinton",
        "code": "26_037"
      },
      {
        "type": "admin4",
        "friendly_type": "state",
        "name": "Michigan",
        "code": "us26"
      },
      {
        "type": "constituency",
        "friendly_type": "constituency",
        "name": "Fourth district, MI",
        "code": "26_04"
      }
    ],
    "location": {
      "latitude": 43.111976,
      "longitude": -84.71275
    }
  }
]

Now this gives me the correct json index.

JSONParser parser = new JSONParser();
Object obj = parser.parse(output);
JSONArray array = (JSONArray)obj;
String jsonobj = array.get(0).toString();
{"politics":[{"code":"usa","name":"United States","type":"admin2","friendly_type":"country"},{"code":"26_057","name":"Gratiot","type":"admin6","friendly_type":"county"},{"code":"26_08","name":"Eighth district, MI","type":"constituency","friendly_type":"constituency"},{"code":"26_037","name":"Clinton","type":"admin6","friendly_type":"county"},{"code":"us26","name":"Michigan","type":"admin4","friendly_type":"state"},{"code":"26_04","name":"Fourth district, MI","type":"constituency","friendly_type":"constituency"}],"location":{"latitude":43.111976,"longitude":-84.71275}}

But I cant seem to get the attribute that I want from it.

JSONObject obj1 = new JSONObject(jsonobj);
String n = obj1.getString("admin4");
System.out.println(n);

All that I need from this json is the state which is Michigan. Where am I wrong? Help would be really appreciated.

the best solution to simplify your search and other operations on json object, is the convert json string to java object and doing your operations.

for convert json string to java object use follow code:

  import org.codehaus.jackson.map.ObjectMapper;
  import org.json.JSONException;
  import org.json.JSONObject; 

YourObject myObject;
ObjectMapper mapper = new ObjectMapper();
try{
    myObject= mapper.readValue(jsonData, myObject.class);
}
catch (Exception e) {
    e.printStackTrace();
}

for example define your class ass follow :

public class myObject{

      private List<Politics> politics;
      private Location location;
      // define getters and setters
}

define Politics and Location class:

public class Politics
{
      String  type;
      String  friendly_type;
      String  name;
      String  code;
      // define getters and setters
}

public class Location
{
  String latitude;
  String longitude;
   // define getters and setters
}

First, array.get(0) will get you the first element from the main array. This first element is a JSON object that has two properties politics and location . You seem to be interested in a value that is inside the array value of the politics property. You'll have to use this ((JSONArray)((JSONObject)array.get(0)).get("politics")) to get that array.

Second, admin4 is not a property it is actually a value of the type property. You'll have to loop through the array to find it.

Here is a complete example:

JSONParser parser = new JSONParser();
Object obj = parser.parse(output);
JSONArray array = (JSONArray)obj;
JSONArray politics = ((JSONObject)array.get(0)).get("politics"));
JSONObject obj = null;
for(int i = 0; i < politics.size(); i++){
    if(((JSONObject)politics.get(i)).getString("type").equals("admin4")){
        obj = ((JSONObject)politics.get(i));
    }
}
if(obj != null){
    // Do something with the object.
}

It seems that you're using the simple json library. I don't remember exactly if it is .get("politics") or .getJSONObject("politics") . There may be other mistakes in method names in my example.

It's because your are trying to get the inner element of the JSON Object. try

JSONObject obj1 = new JSONObject(jsonobj);
JSONArray arr = (JSONArray) obj1.getObject("politics");

You will get a JSONArray object which further constitutes of JSON objects. Now in order to get values using the key you must iterate array as given below:

for(int i=0; i<arr.size(); i++){
    JSONObject obj = arr.getJSONArray(i);
    System.out.println(obj.getString("type"));
}

which will now provide you with output:

admin2
admin6
constituency
admin6
admin4
constituency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM