简体   繁体   English

Object 映射器将 json 格式转换为 object、Z93F725A07423FE1C8846F448B33D2F1

[英]Object Mapper convert json format to object, java

I have a class:我有一个 class:

public class Parent{
   private Child child;

}

public class Child{
  String name;
  String surname;
}

And I got an info body as a json format in two types:我得到了一个json格式的信息体,有两种类型:

1. 1.

   {
    "child"{
      "name":"TOM"
      "surname":"Finn"
            }
    }
  {
    "name":"TOM"
    " surname": "Finn"
   }

And when I try to do the following: Parent parent = objectmapper.convertvalue(body, Parent.class)当我尝试执行以下操作时: Parent parent = objectmapper.convertvalue(body, Parent.class)

In first type of body it's ok but with second I got error IllegalArgumentException .在第一种类型的身体中没关系,但在第二种类型中我得到了错误IllegalArgumentException

How can I fix it that both type of body will be accepted?我怎样才能解决这两种类型的身体都会被接受的问题?

You don't have a comma in JSON and have space in surname string.您在 JSON 中没有逗号,并且在surname字符串中有空格。 And child must be in brackets too.并且child也必须在括号中。 Try this:尝试这个:

{
  "child": {
    "name":"TOM",
    "surname": "Finn"
  }
}

Read about JSON formatting.阅读有关 JSON 格式的信息。 There aren't many rules, but you should stick to them.规则不多,但你应该遵守它们。

EDIT:编辑:

If you are sure your JSON is correct, you can focus on your classes.如果您确定您的 JSON 是正确的,您可以专注于您的课程。 ObjectMapper needs getters or setters to work or instead of you have to change the default configuration using setVisibility() . ObjectMapper需要 getter 或 setter 才能工作,或者您必须使用setVisibility()更改默认配置。

Second, use the readValue() method instead of convertValue() .其次,使用readValue()方法而不是convertValue()

You can read more about ObjectMapper in this article if you still have a problem to handle it.如果您仍然有处理问题,您可以在本文中阅读有关ObjectMapper的更多信息。

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

相关问题 Jackson Object mapper将java地图转换为json维护键的顺序 - Jackson Object mapper to convert java map to json maintaining order of keys Jackson Mapper:无法将JSON对象转换为Java Singleton类 - Jackson Mapper: Unable to convert JSON object to Java Singleton class 如何在 Java 中将 JSON 转换为 Object 格式? - How to convert JSON to Object format in Java? 如何在java中将json对象转换为HTML格式? - How to convert a json object into HTML format in java? Java Object 映射器日期转换为 Json - Java Object Mapper date conversion to Json 使用GSON将JSON对象转换为具有不同格式的Java对象 - Convert JSON object to Java object with different format using GSON 在没有对象映射器的情况下将对象转换为Jackson中的JSON文件 - Convert object into JSON file in Jackson without object mapper Java泛型:将JSON转换为Java对象的对象映射器 - Java Generics : Object Mapper Converting JSON to Java Object 我要转换java object中的json格式数据ZD52387880E1EA228171972D3759Z1 - I want to convert json format data in java object in Java transformation Java object to Json not working using Jackson Object Mapper - Java object to Json not working using Jackson Object Mapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM