user697911
user697911

Reputation: 10531

How to parse this json file with an inner layer?

JSON:

{  
   "prop":"property",
   "inputInfo":{  
      "sku":"6157068",
      "inputText":"iphone"
   }
}

Code:

JSONObject inputObject = JSON.parseObject(input);
String prop = (String)inputObject.get("property");

But how to get the inner layer of 'sku' & 'inputText'?

I am using Alibaba json library in Java.

Upvotes: 1

Views: 533

Answers (4)

Tinus Jackson
Tinus Jackson

Reputation: 3653

If you can use GSON the following is possible

public static void main(String[] args) throws IOException {
    String json = "{\"prop\":\"property\",\"inputInfo\":{\"sku\":\"6157068\",\"inputText\":\"iphone\"}}";

    Gson gson = new Gson();
    HashMap<String, Object> res = gson.fromJson(json, HashMap.class);

    for (Map.Entry<String, Object> entry : res.entrySet()) {

        if ("inputInfo".equalsIgnoreCase(entry.getKey())) {
            HashMap<String, Object> map = gson.fromJson(String.valueOf(entry.getValue()), HashMap.class);

            for (Map.Entry<String, Object> child : map.entrySet()) {
                System.out.println(child.getKey() + " : " + child.getValue());
            }
        }
    }
}

And the result is

inputText : iphone
sku : 6157068.0

Upvotes: 0

T.Kruger
T.Kruger

Reputation: 316

I haven't used the Alibaba library but doesn't it have a getJSONObject() method that you can use on inputObject? I have done this before but I used org.json library.

JSON:

    {"circle":{
        "radius":255819.07998349078,
        "center":{
            "lat":00.000000000000000,
            "lng":00.000000000000000
            }
         }
    }

Java

    JSONObject shape = new JSONObject(entity.getJsonLocation());

    double latitude = shape.getJSONObject("circle")
                           .getJSONObject("center")
                           .getDouble("lat");

    double longitude = shape.getJSONObject("circle")
                            .getJSONObject("center")
                            .getDouble("lng");

This example for instance gets the JSON and creates a JSONObject shape. I can then get the inner json objects by calling getJSONObject() on shape.

I hope this can help you.

Upvotes: 2

Kevinsss
Kevinsss

Reputation: 814

You can do it like this,

JSONObject inputInfoObject = inputObject.getJSONObject("inputInfo");
String sku = inputInfoObject.getString("sku");
String inputText = inputInfoObject.getString("inputText");

Upvotes: 0

Vikki
Vikki

Reputation: 2015

You can create a bean at first, for instance,

public class DemoInfo {

private String id;
private String city;

public void setId(String id) {
     this.id = id;
 }
 public String getId() {
     return id;
 }

public void setCity(String city) {
     this.city = city;
 }
 public String getCity() {
     return city;
 }

}

then,

String s = "{\"id\":\"0375\",\"city\":\"New York\"}";  

DemoInfo info = JSON.parseObject(s, DemoInfo.class); 

or you can use map instead.

JSON.parseObject(s, HashMap.class);

Upvotes: 1

Related Questions