简体   繁体   English

将JSON解析为Java中的对象列表

[英]Parsing JSON to List of Objects in Java

I got a json-object that includes two objects of different classes in the following form 我得到了一个json-object,它包含以下形式的两个不同类的对象

{"field1":"value1","field2":"value2"...}{"f1":"v1","f2":"v2"...}

In order to retrieve both of the initial objects, I am trying to parse this json to some sort of container and then cast each one. 为了检索两个初始对象,我试图将这个json解析为某种容器,然后将其每个转换。

public void update(String JSONmap) throws JsonParseException, JsonMappingException, IOException {
    System.out.println(JSONmap);
    List<Object> list = mapper.readValue(JSONmap, new TypeReference<List<Object>>(){});
    System.out.println("Got list");
    ....

the JSONmap is printed out like above, the second line executes without and output but the third line (sysout("Got list")) and the rest of the method never executes. JSONmap像上面一样被打印出来,第二行不执行并输出,但是第三行(sysout(“ Got list”)),其余方法从不执行。

Should my json-file be formatted in a different way in order to map it to the list or is there any better solution for this? 我的json文件是否应该以其他方式格式化以将其映射到列表,或者对此有更好的解决方案?

Any help is appreciated 任何帮助表示赞赏

That's not an individual JSON object because it contains multiple objects (as you say it's JSON-like) 这不是单个JSON对象,因为它包含多个对象(就像您说的那样,类似于JSON)

{"field1":"value1","field2":"value2"...}{"f1":"v1","f2":"v2"...}

This is two objects. 这是两个对象。 I think if you want to parse it using a standard JSON parser you'll want to use an object structure like 我认为,如果您想使用标准的JSON解析器进行解析,则需要使用类似以下的对象结构

{
    'object1':     {"field1":"value1","field2":"value2"...},
    'object2':     {"f1":"v1","f2":"v2"...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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