How to construct JSON data in Java [closed]

2019-08-21 08:49发布

问题:

I would want to POST JSON request using Apache HttpClient. But the Json data is little complex that I would want to send to target system. Below is the json that I would send

{
  "name":"xyz",
  "id":"428",
"mailId":
  [
   "mailme@mail.com"
  ],
  "bundle1":
  {
      "opwarden":
      {
         "number":"132344345",
         "title":"title"
      }     
  }
}

What is the best and easiest way to contract above json data in Java?

回答1:

With POJOs and ObjectMapper for Jackson:

public class Data {

    private final String name;
    private final String id;
    private final List<String> mailId;
    private final List<Opwarden> bundle1;

    public Data(final String name, final String id, final List<String> mailId, final List<Opwarden>     bundle1) {
        this.name = name;
        this.id = id;
        this.mailId = mailId;
        this.bundle1 = bundle1;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public List<String> getMailId() {
        return mailId;
    }

    public List<Opwarden> getBundle1() {
        return bundle1;
    }
}

and Opwarden:

public class Opwarden {

    private final String number;
    private final String title;

    public Opwarden(final String number, final String title) {
        this.number = number;
        this.title = title;
    }

    public String getNumber() {
        return number;
    }

    public String getTitle() {
        return title;
    }
}

You can create a JSON with:

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));

The output:

{
    "name": "xyz",
    "id": "428",
    "mailId": [
        "mailme@mail.com"
    ],
    "bundle1": [
        {
            "number": "132344345",
            "title": "title"
        }
    ]
}


回答2:

I would recommend using JACKSON object mapper. If you dont want to recreate the above model structure writing pojos.

A nice tutorial can be found here enter link description here

You basically use it like this:

ObjectMapper objectMapper = new ObjectMapper();
String jsonRepresentation = objectMapper.writeValueAsString( anyObject );

Like stated above anyObject could also be a Map key/values and the values can also be maps again.

Your specific use case would be like this:

    ObjectMapper m = new ObjectMapper();

    Map<String, Object> input = new HashMap<String, Object>();
    input.put( "name", "xyz" );
    input.put( "id", "428" );
    input.put( "mailId", new String[] { "mailme@mail.com" } );

    Map<String, Object> opwarden = new HashMap<String, Object>();
    opwarden.put( "number", "132344345" );
    opwarden.put( "title", "title" );

    Map<String, Object> bundle1 = new HashMap<String, Object>();
    bundle1.put( "opwarden", opwarden );

    input.put( "bundle1" , bundle1 );

    String json = m.writeValueAsString( input );