在Android

时间:2015-10-21 06:47:29

标签: android arrays json

我需要以下面的格式构建JSON:

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

有关如何实现这一目标的任何想法?我在Gson上查了一些库,有点令人困惑。提前谢谢。

5 个答案:

答案 0 :(得分:0)

使用Gson,使用字段

创建一个类(ClassA)
String name; 
String sex; // an Enum is better here
Details[] details; //create another class

使用Gson:

 // Serialize a single object.    
    public String serializeToJson(ClassA myClass) {
        Gson gson = new Gson();
        String j = gson.toJson(myClass);
        return j;
    }

答案 1 :(得分:0)

你实际上并不需要Gson。您可以创建一个类似于;

的simle对象
JSONObject object = new JSONObject();
object.put("name","value");
object.put("sex","value");

创建一个像;

的数组
JSONArray array = new JSONArray();
array.put(object);

答案 2 :(得分:0)

您必须创建JSONArray并将其设置为主JSON详细信息的值。

您可以执行以下代码,而无需使用任何第三方库:

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

答案 3 :(得分:0)

这是一个简单的Gson答案

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

打印:

{"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"}]}

答案 4 :(得分:-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));