0

I have a requirement to build JSON in the format below :

{
"name" : "value",
"sex" : "value",
"details" : [
              { 
               "Land" : "value",
               "nationality": "value",
               "birthDate" : "value"
              }
           ]
} 

Any idea on how to accomplish this ? I looked up some libraries on Gson, a bit confusing. Thanks in advance.

user3747512
  • 203
  • 1
  • 6
  • 15

5 Answers5

0

use Gson, create a class (ClassA)with the fields

String name; 
String sex; // an Enum is better here
Details[] details; //create another class

use the Gson:

 // Serialize a single object.    
    public String serializeToJson(ClassA myClass) {
        Gson gson = new Gson();
        String j = gson.toJson(myClass);
        return j;
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You don't actually need Gson. You can create a simle object like;

JSONObject object = new JSONObject();
object.put("name","value");
object.put("sex","value");

And create an array like;

JSONArray array = new JSONArray();
array.put(object);
Faruk Yazici
  • 2,344
  • 18
  • 38
0

You have to create JSONArray and set it as value for details in main JSON.

Following code you can do, without using any third party library:

JSONObject reqJson = new JSONObject();
reqJson.put("Land", "value");                            
reqJson.put("nationality", "value" );
reqJson.put("birthDate", "value" );

JSONArray array = new JSONArray();
array.put(reqJson)

JSONObject json = new JSONObject();
json.put("name", "value");   
json.put("sex", "value");   
json.put("details", array.toString());   
Uniruddh
  • 4,427
  • 3
  • 52
  • 86
0

Here is a simple Gson answer

public class Main {

/* Sample data
{
"name" : "value",
"sex" : "value",
"details" : [
         {
           "Land" : "value",
           "nationality": "value",
           "birthDate" : "value"
          }
       ]
}
*/

    public static void main(String[] args) {
        new Main().example();
    }

    public static class Model {
        public String name;
        public String sex;
        public List<Details> details;

        public static class Details {
            public String Land;
            public String nationality;
            public String birthDate;
        }
    }

    private void example() {
        Model model = new Model();
        model.name = "value";
        model.sex = "value";
        model.details = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Model.Details details = new Model.Details();
            details.Land = "value";
            details.birthDate = "value";
            details.nationality = "value";
            model.details.add(details);
        }
        String json = new Gson().toJson(model);
        System.out.println(json);
    }
}

prints:

{"name":"value","sex":"value","details":[{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"},{"Land":"value","nationality":"value","birthDate":"value"}]}
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
-1
public class Person {


/**
 * sex : value
 * name : value
 * details : [{"nationality":"value","birthDate":"value","Land":"value"}]
 */
private String sex;
private String name;
private List<DetailsEntity> details;

public void setSex(String sex) {
    this.sex = sex;
}

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

public void setDetails(List<DetailsEntity> details) {
    this.details = details;
}

public String getSex() {
    return sex;
}

public String getName() {
    return name;
}

public List<DetailsEntity> getDetails() {
    return details;
}

public static class DetailsEntity {
    /**
     * nationality : value
     * birthDate : value
     * Land : value
     */
    private String nationality;
    private String birthDate;
    private String Land;

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public void setLand(String Land) {
        this.Land = Land;
    }

    public String getNationality() {
        return nationality;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public String getLand() {
        return Land;
    }
}

}

        Person p = new Person();
    p.setName("name");
    p.setSex("sex");

    Person.DetailsEntity bean = new Person.DetailsEntity();
    bean.setBirthDate("1988");
    bean.setLand("land");
    bean.setNationality("xxxx");

    ArrayList<Person.DetailsEntity> list = new ArrayList<Person.DetailsEntity>();
    list.add(bean);

    p.setDetails(list);

    System.out.print(new Gson().toJson(p));
cocoa
  • 378
  • 1
  • 3
  • 8