Java中JSON Simple库的问题

时间:2015-03-16 07:20:27

标签: java json

好的,让我们说以下是我的JSON文件。我可以很好地从文件中获取ID和名称,但是对于列表,它似乎只是将每个人的最终列表读取。所以约翰和戴夫都有史蒂夫的名单。你知道我在哪里弄错了吗?

[
    {
        "id":1,
        "name":"John",
        "listings":[
            {
                "id":1,
                "other_id":34,
            },
            {
                "id":2,
                "other_id":16,
            },
            {
                "id":3,
                "other_id":39,
            }
        ]
    },
    {
        "id":2,
        "name":"Dave",
        "listings":[
            {
                "id":1,
                "other_id":156,
            },
            {
                "id":2,
                "other_id":189,
            },
            {
                "id":3,
                "other_id":312,
            }
        ]
    },
    {
        "id":3,
        "name":"Steve",
        "listings":[
            {
                "id":1,
                "other_id":876,
            },
            {
                "id":2,
                "other_id":534,
            },
            {
                "id":3,
                "other_id":456,
            }
        ]
    }
]

我的java代码

ArrayList<Person> people = new ArrayList<>();

JSONArray array = (JSONArray) parser.parse(reader);

for (Object object : array){
    JSONObject jsonObject = (JSONObject) object;
    int id = (int) jsonObject.get("id");
    String name = (String) jsonObject.get("name");

    ArrayList<Listing> listing = new ArrayList<>();

    JSONArray listings = (JSONArray) jsonObject.get("listings");

    for(Object item : listings){
        JSONObject jsonItem = (JSONObject) item;
        int itemID = (int) jsonItem.get("id");
        int otherID = (int) jsonItem.get("other_id");

        Listing temp = new Listing(itemID, otherID);

        listing.add(temp);

    }

    people.add(new Person(id, name, listing));

}

for (Person person : people) {
    System.out.println("ID: " + person.getId() + ", " + " Name: " + person.getName());
    for (Listing list : person.getListing()) {
        System.out.println("ID: " + list.getID() + ", " + " OtherID: " + list.getOtherID());
    }
}

2 个答案:

答案 0 :(得分:1)

更容易使用jackson解析器。

ObjectMapper mapper;
mapper.readValue(jsonFile, new TypeReference<List<Person>>(){});

它将为您完成所有工作(填充Person对象)

答案 1 :(得分:0)

您使用的listing.add(id, otherID)ArrayList.add(int index,E element)所以我稍微检查了一下这是否有效。

for (int index = 0; index < array.length(); index++) {
        JSONObject jsonObject = (JSONObject) array.get(index);
        int id = (int) jsonObject.get("id");
        String name = (String) jsonObject.get("name");
        List<Listing> listing = new ArrayList<>();
        JSONArray listings = (JSONArray) jsonObject.get("listings");
        for (int index1 = 0; index1 < listings.length(); index1++) {
            JSONObject jsonItem = (JSONObject) listings.get(index1);
            int itemID = (int) jsonItem.get("id");
            int otherID = (int) jsonItem.get("other_id");
            Listing listing2 = new Listing(itemID, otherID);
            listing.add(listing2);
            System.out.println(name + " " + itemID + " " + otherID);
        }
        people.add(new Person(id, name, listing));
    }