JSON避难所化问题:错误的json值为类型

发布于 2025-01-30 07:20:20 字数 464 浏览 6 评论 0原文

我有一个类似于下面的复合对象:

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", "{\"a\" :3, \"b\" : 5}");

           
m = {a=b, c={"a" :3, "b" : 5}}

我必须通过https调用提交此请求,以便对Java对象进行应序列化,因此我将其转换为JSON字符串,使用,

objectmapper.writeValueAsString(m)

当我转换时,它将其附加到C的值中。 :

{"a":"b","c":"{\"a\" :3, \"b\" : 5}"}

在客户端进行对象的挑选时,请求未能说 “错误的JSON值为类型:类”

任何帮助?

I have a composite object like below:

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", "{\"a\" :3, \"b\" : 5}");

           
m = {a=b, c={"a" :3, "b" : 5}}

I have to submit this request via https call in order to deserialize to a java object, hence i converted this to JSON string, using,

objectmapper.writeValueAsString(m)

when i convert it , it is appending quotes to the value of c:

{"a":"b","c":"{\"a\" :3, \"b\" : 5}"}

and While deserializing this object at the client side, the request fails saying
"Error deserialize JSON value into type: class"

Any help??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2025-02-06 07:20:20

值的类型c是字符串,因此对象映射器逃脱了所有非法字符,将字符串包裹在引号中。

您可以制作c另一个映射:

Map<String, Object> c = new HashMap<>();
c.put("a", 3);
c.put("b", 5);

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", c);

或者您可以创建自定义pojo并使用 @jsonrawvalue 注释:

public class MyPojo{

   private String a;

   @JsonRawValue
   private String c;

   // getter and setters
}

MyPojo m = new MyPojo();
m.setA("b");
m.setB("{\"a\" :3, \"b\" : 5}");

objectmapper.writeValueAsString(m);

documentation

标记注释,表明应通过在不引用字符的情况下将属性的文字字符串值序列化,以序列化。这对于在JSON中序列化或将JavaScript函数定义从服务器传递到JavaScript客户端可能很有用。
警告:根据您的输入值,所得的JSON流可能无效。

客户端错误的含义可能是它无法将字符串归为对象(它期望{而不是)。

The type of the value C is String, so the object mapper escapes all illegal characters a wraps the string in quotes.

You could make C an another map:

Map<String, Object> c = new HashMap<>();
c.put("a", 3);
c.put("b", 5);

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", c);

Or you can create custom POJO and use @JsonRawValue annotation:

public class MyPojo{

   private String a;

   @JsonRawValue
   private String c;

   // getter and setters
}

MyPojo m = new MyPojo();
m.setA("b");
m.setB("{\"a\" :3, \"b\" : 5}");

objectmapper.writeValueAsString(m);

From the documentation:

Marker annotation that indicates that the annotated method or field should be serialized by including literal String value of the property as is, without quoting of characters. This can be useful for injecting values already serialized in JSON or passing javascript function definitions from server to a javascript client.
Warning: the resulting JSON stream may be invalid depending on your input value.

The client error meaning probably is that it can't deserialize String into an Object (it expects { instead of ").

终难遇 2025-02-06 07:20:20

您最好将JSONOBJECT用于c值:

JSONObject json = new JSONObject();
json.put("a", 3);
json.put("b", 5);

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", json);

完成代码:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.minidev.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public static void main(String[] args) throws JsonProcessingException {

    JSONObject json = new JSONObject();
    json.put("a", 3);
    json.put("b", 5);

    Map<String, Object> m = new HashMap<>();
    m.put("a", "b");
    m.put("c", json);


    ObjectMapper objectMapper = new ObjectMapper();
    String valueAsString = objectMapper.writeValueAsString(m);

    System.out.println(valueAsString);
}

输出为:

{“ a”:“ b”,“ c”:{“ a”:3,“ b”:5}}}

You better use JSONObject for c value:

JSONObject json = new JSONObject();
json.put("a", 3);
json.put("b", 5);

Map<String, Object> m = new HashMap<>();
m.put("a", "b");
m.put("c", json);

Complete code:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.minidev.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public static void main(String[] args) throws JsonProcessingException {

    JSONObject json = new JSONObject();
    json.put("a", 3);
    json.put("b", 5);

    Map<String, Object> m = new HashMap<>();
    m.put("a", "b");
    m.put("c", json);


    ObjectMapper objectMapper = new ObjectMapper();
    String valueAsString = objectMapper.writeValueAsString(m);

    System.out.println(valueAsString);
}

The output is:

{"a":"b","c":{"a":3,"b":5}}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文