[英]Parsing JSON nested object as String
I have JSON object and I don't want to parse some of nested objects. 我有JSON对象,并且我不想解析一些嵌套的对象。 Instead I want them as String (I have my reasons). 相反,我希望它们作为String(我有我的理由)。 Is there an easy way to do it? 有一个简单的方法吗? I've tried to implement custom Deserializer but I found it complicated in different ways (I don't want to concatenate tokens I just need whole object. It also doesn't consider ':' as token so it need special handling) or maybe I'm missing smth. 我尝试实现自定义反序列化器,但发现它以不同的方式变得复杂(我不想连接令牌,我只需要整个对象。它也不考虑将“:”作为令牌,因此需要特殊处理)或我错过了。 Also putting quotes in json is not an option. 也不能在json中加上引号。 I need that json the way it is. 我需要这样的json。
JSON example: JSON示例:
{
"lastName":"Bitman",
"jsonObjectIDontWantToParse":{
"somefield":1234
}
}
Java object I want to parse json to. 我想解析json的Java对象。
public class Jack {
public String lastName;
public String jsonObjectIDontWantToParse;
@Override
public String toString() {
return "lastName=" + lastName + ", jsonObjectIDontWantToParse=" + jsonObjectIDontWantToParse;
}
}
Here's my main class 这是我的主班
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
final String jackString = "{\"lastName\":\"Bitman\",\"jsonObjectIDontWantToParse\":{\"somefield\":1234}}";
Jack user = mapper.readValue(jackString, Jack.class);
System.out.println(user);
}
I expect output to be like this 我希望输出是这样的
lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}
Updated: So basically this is example what I'm looking for (except There are no such method). 更新:所以基本上这是我要寻找的示例(除了没有这种方法)。 I want to skip any parsing for that node... 我想跳过对该节点的任何解析...
public class LeaveAsStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
return jp.getWholeObject().toString();
}
}
I think, that you should use JsonNode
class as a type of jsonObjectIDontWantToParse
property: 我认为,您应该将JsonNode
类用作jsonObjectIDontWantToParse
属性的类型:
class Jack {
private String lastName;
private JsonNode jsonObjectIDontWantToParse;
// getters, setters, toString, other
public String getJsonObjectIDontWantToParseAsString() {
return jsonObjectIDontWantToParse.toString();
}
}
You can add one additional method getJsonObjectIDontWantToParseAsString
if you want to get String
representation. 如果要获取String
表示getJsonObjectIDontWantToParseAsString
则可以添加一个附加方法getJsonObjectIDontWantToParseAsString
。
Example usage: 用法示例:
ObjectMapper mapper = new ObjectMapper();
Jack jack = mapper.readValue(json, Jack.class);
System.out.println(jack);
System.out.println(jack.getJsonObjectIDontWantToParseAsString());
Above program prints: 上面的程序打印:
lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}
{"somefield":1234}
To get to this: 要做到这一点:
lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}
You just need to escape the String representation of jsonObjectIDontWantToParse
. 您只需要转义jsonObjectIDontWantToParse
的String表示jsonObjectIDontWantToParse
。 That means change it read: 这意味着将其更改为:
jsonObjectIDontWantToParse: "{ \"somefield\": 1234 }"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.