简体   繁体   English

将嵌套的JSON字符串转换为Java对象列表

[英]Convert nested JSON string to list of java object

I have this json String 我有这个json字符串

{"data":"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]", "id":"123"}

And corresponding java classes are 和相应的java类是

public class Level {
  public LevelKey key;
  public String id;
 }


 public class LevelKey{
 public String keyEnd;
 }

I want to convert this data json string to list of Level object using Jackson 我想使用杰克逊将此data json字符串转换为Level对象的列表

 ObjectMapper mapper = new ObjectMapper();
    List<Level> arr = mapper.readValue(data, new TypeReference<List<Level>>(){});

But I am getting below error 但是我正在错误以下

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Level': was expecting ('true', 'false' or 'null')

Is there any other method to parse it? 还有其他解析方法吗?

The below does not look like a proper JSON for the purpose (except for a standard fixed string) 以下看起来不适合用于此目的的JSON(标准固定字符串除外)

"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]"

You could correct the data part of your JSON to something like below (Closest to your JSON in question) : 您可以将JSON的数据部分更正为以下内容(与您所讨论的JSON最接近):

[\"Level [key=LevelKey [keyEnd=0], Description=abc]\",\" Level [key=levelKey [keyEnd=1], Description=xyz]\"]

Is there any other method to parse it? 还有其他解析方法吗?

You could use a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string : 您可以使用ArrayList的直接类引用,而不必像下面那样实例化TypeReference来解析上述(更正后的)json字符串:

List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());

This was an interesting one I must say. 我必须说这很有趣。 Take a look at code snippet I think I got it correct : 看一下代码片段,我认为我理解正确了:

String data ="{\"data\":\"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]\", \"id\":\"123\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
System.out.println(arr);

I got the following output : 我得到以下输出:

[{data=[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz], id=123}]

Also if you encountered any JsonParseException which according to documentation means : 另外,如果遇到任何JsonParseException,根据文档,这意味着:

Exception type for parsing problems, used when non-well-formed content (content that does not conform to JSON syntax as per specification) is encountered. 解析问题的异常类型,当遇到格式不正确的内容(按照规范不符合JSON语法的内容)时使用。

So while hacking the JSON you can update the ObjectMapper object like this : 因此,在破解JSON时,您可以像这样更新ObjectMapper对象:

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Also as mentioned by Exception_al using a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string. 也如Exception_al所述,使用ArrayList的直接类引用,而不必像下面那样实例化TypeReference来解析上面的(更正的)json字符串。

List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());

Hope this helped. 希望这会有所帮助。

The String is not the JSON representation of what you expect you get deserialized into JAVA. String不是您期望反序列化为JAVA的JSON表示形式。
This is a JSON String: 这是一个JSON字符串:

"{"data":[{"key":{"keyEnd":0},"Description":"abc"},{"key":{"keyEnd":1},"Description":"abc"}],"id":"123"}"

So, there is either a problem with the String or you need to do the parsing yourself. 因此,String可能有问题,或者您需要自己进行解析。

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

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