简体   繁体   English

JSON 解析将字符串属性更改为 Java 中的 object 属性

[英]JSON parse to change String attribute to an object atribute in Java

I have a JSON object that one of the a attributes is a JSON string.我有一个 JSON object ,其中一个属性是 JSON 字符串。

a = {
"dt" : "2022-01-02 00:00:00"
"fx": "{\"id\":1,\"faixaId\":1,\"speedAtivo\":true}",
"hash": "8c91a61a0a49b73de2fc13caed00e6a93dbe435b354216802da0dbe8bfda3300",
}

In JavaScript, I can convert the "fx" attribute to an object using:在 JavaScript 中,我可以使用以下命令将“fx”属性转换为 object:

a.fx = JSON.parse(a.fx)

And the new JSON:还有新的 JSON:

a = {
"dt" : "2022-01-02 00:00:00"
"fx": {"id":1,
        "faixaId":1,
        "speedAtivo":true
      },
"hash": "8c91a61a0a49b73de2fc13caed00e6a93dbe435b354216802da0dbe8bfda3300",
}

There is a way to do this with Java?有没有办法用 Java 做到这一点?

Yes, you can parse JSON string using one of these libraries to use these library check the docs and maven dependency是的,您可以使用这些库之一解析 JSON 字符串以使用这些库检查文档maven 依赖项

But make sure that your JSON is in correct format as the above JSON is missing comma after the first line ending.但请确保您的 JSON 格式正确,因为上述 JSON 在第一行结束后缺少逗号。

Below is the simple example to parse a JSON String using the above library.下面是使用上述库解析 JSON 字符串的简单示例。

String jsonStr = "{\"dt\":\"2022-01-02 00:00:00\",
    \"fx\":\"id\":1,\"faixaId\":1,\"speedAtivo\":true},
   \"hash\":\"8c91accsiamkFVXtw6N7DnE3QtredADYBYU35b354216802da0dbe8bfda3300\", 
}";

JSONObject strObj = JSONObject.fromObject(jsonStr);

Output: Output:

{
   "dt": "2022-01-02 00:00:00",
   "fx": {
       "id": 1,
       "faixaId": 1,
       "speedAtivo": true
   },
   "hash": 
   "8c91accsiamkFVXtw6N7DnE3QtredADYBYU35b354216802da0dbe8bfda3300"
}

If you use Jackson lirary to convert objects from JSON String to objects via custom deserializer.如果您使用 Jackson 库通过自定义反序列化器将对象从 JSON 字符串转换为对象。

First you create Classes that will represent the main object and fx object首先,您创建代表主要 object 和 fx object 的类

public class AClass {
    public String dt;
    @JsonDeserialize(using = Deserializer.class)
    public FxClass fx;
    public String hash;
}

public class FxClass {
    public int id;
    public int faixaId;
    public boolean speedAtivo;
}

Then you create deserizalizer然后你创建反序列化器

public class Deserializer extends StdDeserializer<FxClass> {
    protected Deserializer(Class<?> vc) {
        super(vc);
    }
    protected Deserializer() {
        this(null);
    }

    @Override
    public FxClass deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        TextNode treeNode = jsonParser.getCodec().readTree(jsonParser);
        return new ObjectMapper().readValue(treeNode.asText(), FxClass.class);
    }
}

And add deserializer to ObjectMapperConfiguration并将反序列化器添加到 ObjectMapperConfiguration

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(FxClass.class, new Deserializer());
    mapper.registerModule(module);

And thats it.就是这样。 To convert JsonString to object将 JsonString 转换为 object

AClass readValue = new ObjectMapper.readValue(json, AClass.class);

For additional info try reading https://www.baeldung.com/jackson-deserialization有关更多信息,请尝试阅读https://www.baeldung.com/jackson-deserialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM