如何在 Java 中构造 JSON 数据

我想使用 Apache HttpClient 发布 JSON 请求。但是我想发送到目标系统的 Json 数据有点复杂。下面是我要发送的 json


{

  "name":"xyz",

  "id":"428",

"mailId":

  [

   "mailme@mail.com"

  ],

  "bundle1":

  {

      "opwarden":

      {

         "number":"132344345",

         "title":"title"

      }     

  }

}

在 Java 中收缩上述 json 数据的最好和最简单的方法是什么?


沧海一幻觉
浏览 150回答 2
2回答

郎朗坤

使用Jackson 的POJO 和ObjectMapper :public class Data {&nbsp; &nbsp; private final String name;&nbsp; &nbsp; private final String id;&nbsp; &nbsp; private final List<String> mailId;&nbsp; &nbsp; private final List<Opwarden> bundle1;&nbsp; &nbsp; public Data(final String name, final String id, final List<String> mailId, final List<Opwarden>&nbsp; &nbsp; &nbsp;bundle1) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.mailId = mailId;&nbsp; &nbsp; &nbsp; &nbsp; this.bundle1 = bundle1;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public List<String> getMailId() {&nbsp; &nbsp; &nbsp; &nbsp; return mailId;&nbsp; &nbsp; }&nbsp; &nbsp; public List<Opwarden> getBundle1() {&nbsp; &nbsp; &nbsp; &nbsp; return bundle1;&nbsp; &nbsp; }}和欧普华登:public class Opwarden {&nbsp; &nbsp; private final String number;&nbsp; &nbsp; private final String title;&nbsp; &nbsp; public Opwarden(final String number, final String title) {&nbsp; &nbsp; &nbsp; &nbsp; this.number = number;&nbsp; &nbsp; &nbsp; &nbsp; this.title = title;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNumber() {&nbsp; &nbsp; &nbsp; &nbsp; return number;&nbsp; &nbsp; }&nbsp; &nbsp; public String getTitle() {&nbsp; &nbsp; &nbsp; &nbsp; return title;&nbsp; &nbsp; }}您可以使用以下方法创建 JSON:ObjectMapper objectMapper = new ObjectMapper();Data data = new Data("xyz", "428", List.of("mailme@mail.com"), List.of(new Opwarden("132344345", "title")));System.out.println(objectMapper.writeValueAsString(data));输出:{&nbsp; &nbsp; "name": "xyz",&nbsp; &nbsp; "id": "428",&nbsp; &nbsp; "mailId": [&nbsp; &nbsp; &nbsp; &nbsp; "mailme@mail.com"&nbsp; &nbsp; ],&nbsp; &nbsp; "bundle1": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "number": "132344345",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title": "title"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]}

鸿蒙传说

我建议使用 JACKSON 对象映射器。如果您不想重新创建上述模型结构,请编写 pojo。可以在这里找到一个很好的教程在这里输入链接描述你基本上是这样使用它的:ObjectMapper&nbsp;objectMapper&nbsp;=&nbsp;new&nbsp;ObjectMapper(); String&nbsp;jsonRepresentation&nbsp;=&nbsp;objectMapper.writeValueAsString(&nbsp;anyObject&nbsp;);如上所述,anyObject 也可以是映射键/值,值也可以再次映射。您的具体用例将是这样的:&nbsp; &nbsp;ObjectMapper m = new ObjectMapper();&nbsp; &nbsp; Map<String, Object> input = new HashMap<String, Object>();&nbsp; &nbsp; input.put( "name", "xyz" );&nbsp; &nbsp; input.put( "id", "428" );&nbsp; &nbsp; input.put( "mailId", new String[] { "mailme@mail.com" } );&nbsp; &nbsp; Map<String, Object> opwarden = new HashMap<String, Object>();&nbsp; &nbsp; opwarden.put( "number", "132344345" );&nbsp; &nbsp; opwarden.put( "title", "title" );&nbsp; &nbsp; Map<String, Object> bundle1 = new HashMap<String, Object>();&nbsp; &nbsp; bundle1.put( "opwarden", opwarden );&nbsp; &nbsp; input.put( "bundle1" , bundle1 );&nbsp; &nbsp; String json = m.writeValueAsString( input );
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java