[英]How do I parse this escaped Json with Gson java?
So I'm getting responses like the following which I have no control over:因此,我收到了以下我无法控制的响应:
{
"message": "someName someLastName has sent you a question",
"parameters": "{\"firstName\":\"someName\",\"lastName\":\"someLastName\"}",
"id": 141
}
At a glance it seems simple, but the parameters element needs to be read as a json object and I cannot for the life of me work out how to do it.乍一看似乎很简单,但需要将参数元素作为 json 对象读取,而我终其一生都无法弄清楚如何去做。 This is what I am trying at the moment:
这就是我目前正在尝试的:
JsonObject parameters = data.getAsJsonObject().get("parameters").getAsJsonObject();
/throws java.lang.IllegalStateException: Not a JSON Object: "{\"firstName\":\"someName\",\"lastName\":\"someLastName\"}"
So I tried:所以我试过:
String elementToString = data.getAsJsonObject().get("parameters").toString().replace("\\\"", "\"");
JsonObject parameters = new Gson().fromJson(elementToString, JsonElement.class).getAsJsonObject();
//throws com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 5 path $
Where data is (typically this is pulled from a server):数据在哪里(通常是从服务器中提取的):
JsonElement data = new Gson().fromJson(" {\n" +
" \"message\": \"someName someLastName has sent you a question\",\n" +
" \"parameters\": \"{\\\"firstName\\\":\\\"someName\\\",\\\"lastName\\\":\\\"someLastName\\\"}\",\n" +
" \"id\": 141\n" +
" }", JsonElement.class);
Surely this is not a difficult problem?这当然不是一个困难的问题吗?
What you have here你在这里有什么
"parameters": "{\"firstName\":\"someName\",\"lastName\":\"someLastName\"}",
is a JSON pair where both the name (which is always a JSON string) and the value are JSON strings.是一个 JSON 对,其中名称(始终是 JSON 字符串)和值都是 JSON 字符串。 The value is a String that can be interpreted as a JSON object.
该值是一个可以解释为 JSON 对象的字符串。 So do just that
所以就这样做
String jsonString = data.getAsJsonObject().get("parameters").getAsJsonPrimitive().getAsString();
JsonObject parameters = gson.fromJson(jsonString, JsonObject.class);
The following下列
Gson gson = new Gson();
JsonElement data = gson
.fromJson(" {\n" + " \"message\": \"someName someLastName has sent you a question\",\n"
+ " \"parameters\": \"{\\\"firstName\\\":\\\"someName\\\",\\\"lastName\\\":\\\"someLastName\\\"}\",\n"
+ " \"id\": 141\n" + " }", JsonElement.class);
String jsonString = data.getAsJsonObject().get("parameters").getAsJsonPrimitive().getAsString();
JsonObject parameters = gson.fromJson(jsonString, JsonObject.class);
System.out.println(parameters);
prints the JSON text representation of that JsonObject
打印该
JsonObject
的 JSON 文本表示
{"firstName":"someName","lastName":"someLastName"}
The solution I use to get the concrete object (regardless whether it's already an object, or an escaped string) is a deserializer like this:我用来获取具体对象的解决方案(无论它已经是一个对象,还是一个转义的字符串)是一个像这样的反序列化器:
public class MyDeserializer implements JsonDeserializer<MyThing> {
@Override
public MyThing deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (element.isJsonObject()) {
//Note Don't use a gson that contains this deserializer or it will recurse forever
return new Gson().fromJson(element.getAsJsonObject(), MyThing.class);
} else {
String str = element.getAsJsonPrimitive().getAsString();
return gson.get().fromJson(str, MyThing.class);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.