我如何通过Gson java传递这个转义的Json? [英] How do I pass this escaped Json with Gson java?

查看:404
本文介绍了我如何通过Gson java传递这个转义的Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
消息:someName someLastName向你发送了一个问题,
parameters:{\firstName\:\someName\,\lastName\:\someLastName \},
id:141
}

在一目了然看起来很简单,但参数元素需要作为json对象来读取,而我不能为我的生活找出如何去做。这是我现在正在尝试的:

  JsonObject parameters = data.getAsJsonObject()。get(parameters)。 getAsJsonObject(); 
/ throws java.lang.IllegalStateException:不是JSON对象:{\firstName \:\someName \,\lastName \:\someLastName \ }

所以我试过了:

  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:使用JsonReader.setLenient(true)在第1行接受格式错误的JSON第5行路径$

数据在哪里(通常是从服务器取得的):

<$ p jsonElement data = new Gson()。fromJson({\ n+
\message \:\someName someLastName发送给您一个问题,\ n+
\参数\:\{\\\名字\\\\:\\\someName\\ \\,lastname \\\:\\\someLastName \\\\\} \,\\\
+
\id \:141 \\\
+
},JsonElement.class);

当然,这不是一个难题?

解决方案

你在这里

 parameters:{\firstName \:\someName \,\lastName\:\someLastName \},

是一个JSON对,其中名称(始终是JSON字符串)和值都是JSON字符串。该值是一个可以解释为JSON对象的字符串。所以做到这一点

 字符串jsonString = data.getAsJsonObject()。get(parameters)。getAsJsonPrimitive()。getAsString( ); 
JsonObject参数= gson.fromJson(jsonString,JsonObject.class);






以下

  Gson gson = new Gson(); 
JsonElement data = gson
.fromJson({\ n+\message \:\someName someLastName has sent a question \,\\\

+\参数\:\{\\\firstName \\\:\\\someName \\\,\\ \\lastName \\\:\\\someLastName\\\} \,\\\

+\id \\ \\:141 \\\
+},JsonElement.class);
String jsonString = data.getAsJsonObject()。get(parameters)。getAsJsonPrimitive()。getAsString();
JsonObject参数= gson.fromJson(jsonString,JsonObject.class);
System.out.println(parameters);

输出该 JsonObject 的JSON文本表示形式,

  {firstName:someName,lastName:someLastName} 


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. 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. The value is a String that can be interpreted as a JSON object. 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

{"firstName":"someName","lastName":"someLastName"}

这篇关于我如何通过Gson java传递这个转义的Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆