使用带有JSON.Simple的JSON列表获取对象列表

时间:2016-08-11 11:54:18

标签: java json json-simple

我有一个里面有数组的JSON数组。我如何使用Json.simple解析它。

JSON对象示例:

[  
   {  
      "ID":"1",
      "Objects":[  
         {  
            "ObjectID":"5"
         },
         {  
            "ObjectID":"9"
         }
      ]
   },
   {  
      "ID":"2",
      "Objects":[  
         {  
            "ObjectID":"99"
         },
         {  
            "ObjectID":"11"
         }
      ]
   }
]

Class Foo

public int ID;
public List<Obj> Objects;

Class Obj

public int ObjectID;

所以我想要一个Foo列表但是在foo中还需要填充对象

所以我需要的是:

List<Foo> foos = .... the output from the JSON

2 个答案:

答案 0 :(得分:0)

我为你编写了一个演示代码,使用jackson.databind你可以解析代码如下:

    package test;

    import java.io.IOException;

    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;

    public class jsonParsing {
        public static void main(String args[]){
            String jsonString = 
                      "{"
                        +"\"ID\":\"1\","
                         +"\"Objects\":["
                           +"{"
                           +"\"ObjectID\":\"5\""
                            + "},"
                            + "{"  
                              +"\"ObjectID\":\"9\""
                            + "}"
                        +"]"
                      + "}";


        System.out.println("Print ObjectID from  --- "+jsonString);


        try{
            ObjectMapper mapper = new ObjectMapper();
            JsonMapper jm = mapper.readValue(jsonString.toString(), JsonMapper.class);
            System.out.println("ID is :"+jm.getId());
            for(JsonSubMapper jsm : jm.getObjects()){
                System.out.println("ObjectID is :"+jsm.getObjectID());
            }
        }catch(JsonMappingException jme){
            jme.printStackTrace();
        }catch(JsonParseException jpe){
            jpe.printStackTrace();
        }catch(IOException io){
            io.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }           
        }
    }

package test;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({ "ID", "Objects"})

public class JsonMapper {

    @JsonProperty("ID")
    private String id;

    @JsonProperty("Objects")
    private List<JsonSubMapper> objects = new ArrayList<JsonSubMapper>();;

    public String getId() {
        return id;
    }

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

    public List<JsonSubMapper> getObjects() {
        return objects;
    }

    public void setObjects(List<JsonSubMapper> objects) {
        this.objects = objects;
    }

}

    package test;
    import javax.annotation.Generated;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Generated("org.jsonschema2pojo")
    @JsonPropertyOrder({ "ObjectID"})

    public class JsonSubMapper {

        @JsonProperty("ObjectID")
        private String objectID;

        public String getObjectID() {
            return objectID;
        }

        public void setObjectID(String objectID) {
            this.objectID = objectID;
        }

    }

输出:

Print ObjectID from  --- {"ID":"1","Objects":[{"ObjectID":"5"},{"ObjectID":"9"}]}
ID is :1
ObjectID is :5
ObjectID is :9

答案 1 :(得分:0)

拥有以下课程:

public class Foo {

    private int id;
    private List<Obj> objects;

    // Getters and setters ommited
}
public class Obj {

    private int objectID;

    // Getters and setters ommited      
}

使用这段代码解析您的JSON:

String json = "[{\"ID\":\"1\",\"Objects\":[{\"ObjectID\":\"5\"},{\"ObjectID\":\"9\"}]},{\"ID\":\"2\",\"Objects\":[{\"ObjectID\":\"99\"},{\"ObjectID\":\"11\"}]}]";
JSONParser parser = new JSONParser();
List<Foo> foos = parseFooArray((JSONArray) parser.parse(json));

这就是神奇发生的地方:

private List<Foo> parseFooArray(JSONArray array) {

    List<Foo> list = new ArrayList<>();

    for (Object item : array) {
        JSONObject object = (JSONObject) item;
        Foo foo = new Foo();
        foo.setId(Integer.valueOf((String) object.get("ID")));
        foo.setObjects(parseObjectArray((JSONArray) object.get("Objects")));
        list.add(foo);
    }

    return list;
}
private List<Obj> parseObjectArray(JSONArray array) {

    List<Obj> list = new ArrayList<>();

    for (Object item : array) {
        JSONObject object = (JSONObject) item;
        Obj obj = new Obj();
        obj.setObjectID(Integer.valueOf((String) object.get("ObjectID")));
        list.add(obj);
    }

    return list;
}