JSON对象数组到Java POJO

时间:2019-03-19 19:16:49

标签: java arrays json object

将此JSON对象转换为Java中的类,映射将如何出现在您的POJO类中?

{
    "ownerName": "Robert",
    "pets": [
        {
            "name": "Kitty"
        },
        {
            "name": "Rex"
        },
        {
            "name": "Jake"
        }
    ]
}

3 个答案:

答案 0 :(得分:3)

在json中,您将pets作为属性,将public class Response { private String ownerName; private List<Pet> pets; // getters and setters } 作为对象列表

public class Pet {

 private String name;

 //getters and setters

  }

宠物POJO

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\backends\base\base.py" in _commit
  239.                 return self.connection.commit()

The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\contrib\admin\options.py" in wrapper
  604.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\contrib\admin\sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\contrib\admin\options.py" in add_view
  1637.         return self.changeform_view(request, None, form_url, extra_context)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\utils\decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\contrib\admin\options.py" in changeform_view
  1525.             return self._changeform_view(request, object_id, form_url, extra_context)

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\transaction.py" in __exit__
  212.                         connection.commit()

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\backends\base\base.py" in commit
  261.         self._commit()

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\backends\base\base.py" in _commit
  239.                 return self.connection.commit()

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\Users\jack_\.virtualenvs\SNDProject-v81rGQ8f\lib\site-packages\django\db\backends\base\base.py" in _commit
  239.                 return self.connection.commit()

Exception Type: IntegrityError at /admin/auth_pp/posts/add/
Exception Value: FOREIGN KEY constraint failed

答案 1 :(得分:3)

这种问题很受欢迎,需要一般性回答。如果您需要基于POJOJSON生成JSON Schema模型,请使用www.jsonschema2pojo.org。打印屏幕示例显示了如何使用它: enter image description here

如何使用它:

  1. 选择目标语言。 Java
  2. 选择来源。 JSON
  3. 选择注释样式。这可能很棘手,因为它取决于要用于序列化/反序列化JSON的库。如果架构很简单,请不要使用注释(None选项)。
  4. 选择其他可选配置选项,例如Include getters and setters。您也可以在IDE中做到这一点。
  5. 选择Preview按钮。如果架构很大,请下载ZIP,其中包含生成的类。

此工具为您的JSON生成:

public class Person {

 private String ownerName;
 private List <Pet> pets = null;

 public String getOwnerName() {
  return ownerName;
 }

 public void setOwnerName(String ownerName) {
  this.ownerName = ownerName;
 }

 public List < Pet > getPets() {
  return pets;
 }

 public void setPets(List < Pet > pets) {
  this.pets = pets;
 }

}

public class Pet {

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

对于Android StudioKotlin,请阅读RIP http://www.jsonschema2pojo.org

答案 2 :(得分:1)

您可以使用以下类:

{{1}}