Java + jackson 解析错误无法识别的字符转义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30187530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:17:44  来源:igfitidea点击:

Java + Hymanson parsing error Unrecognized character escape

javajsonHymansonhttp-postparse-error

提问by Amila Iddamalgoda

I need to do a POST json string , using HttpClient. Following will be the code i have. From the other end the Json is mapped to an object.

我需要使用 HttpClient 做一个 POST json 字符串。以下将是我拥有的代码。从另一端,Json 被映射到一个对象。

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\mlpdemoins\" }";
post.setEntity(new ByteArrayEntity(         jsonData.toString().getBytes("UTF8")));
HttpResponse response = client.execute(post);

Here all others are correctly mapping expect the userId. Here the problem is with the backward slash(mlpdemo\mlpdemins). I guess. If I send a single string as the user id it will be mapped without any issues. Eg:-

这里所有其他人都正确映射除了 userId。这里的问题在于反斜杠(mlpdemo\mlpdemins)。我猜。如果我发送一个字符串作为用户 id,它将被毫无问题地映射。例如:-

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemoins\" }";

This works .

这有效。

But I need this (mlpdemo\mlpdemins)to be sent through the POSt. Please help me out.

但我需要这个(mlpdemo\mlpdemins)通过 POSt 发送。请帮帮我。

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\mlpdemoins\" }";

Here is the exception Im getting.

这是我得到的例外。

com.fasterxml.Hymanson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
BadRequestException (0ea35150-f33a-4932-a31e-8a1048af53ad): 400 Bad Request, com.strategicgains.restexpress.serialization.DeserializationException: com.fasterxml.Hymanson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:165)
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:181)

采纳答案by Sach141

mlpdemo\mlpdemoinsis an invalid string you can't use it in JSON . But you can use mlpdemo\\mlpdemoinseasily.

mlpdemo\mlpdemoins是一个无效字符串,您不能在 JSON 中使用它。但您可以mlpdemo\\mlpdemoins轻松使用。

below code works fine for me :

下面的代码对我来说很好用:

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\mlpdemoins\" }";

ObjectMapper mapper=new ObjectMapper();

System.out.println(mapper.readTree(jsonData));

It will produce this output JSON :

它将产生此输出 JSON :

{"provider":null,"password":"a","userid":"mlpdemo\mlpdemoins"}

回答by ogarzonm

Set your mapper

设置映射器

mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);