0

所以我试图从我的数据库中提取数据并创建一个看起来像这样的 JSON 文件。我知道我可以使用 my_json_object.put(); 但这似乎只适用于字符串,字符串。我和我想用 2 个条目创建值,而第二个条目实际上是一个包含许多值的数组。我怎样才能做到这一点?多谢你们。

{"value": {
"name": "History",
"values": [ 
{"front": "History Market", "back": " It is the world of the world that the world    could not say that", "color": "Yellow"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Red"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Purple"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Blue"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Blue"}
]}}
4

3 回答 3

4

将您当前的 jsonObject 创建为:

//Create main json object
JSONObject json = new JSONObject();

//Create value json object
JSONObject valueJson = new JSONObject();

//Create values JSONArray
JSONArray valuesarray = new JSONArray();

for(int i=0;i<=4;i++){
 // Create values jsonObject
JSONObject valuesJson = new JSONObject();
valuesJson.put("front","front_value");
valuesJson.put("back","back_value");
valuesJson.put("color","color_value");
 // put values jsonObject to valuesarray JSONArray
valuesarray.put(valuesJson);    

}
 // put name key-value to value jsonObject
valueJson.put("name","History");
 // put values JSONArray to value jsonObject
valueJson.put("values",valuesarray);

 // put value jsonObject to final json Object
json.put("value",valueJson);
于 2013-01-26T20:03:52.963 回答
2

使用 google gson,它非常简单,可以完美地处理数组和列表

public class CollectionToJson {
public static void main(String[] args) {
    //
    // Converts a collection of string object into JSON string.
    //
    List<String> names = new ArrayList<String>();
    names.add("Alice");
    names.add("Bob");
    names.add("Carol");
    names.add("Mallory");

    Gson gson = new Gson();
    String jsonNames = gson.toJson(names);
    System.out.println("jsonNames = " + jsonNames);

    //
    // Converts a collection Student object into JSON string
    //
    Student a = new Student("Alice", "Apple St",
            CollectionToJson.getDOB(2000, 10, 1));
    Student b = new Student("Bob", "Banana St", null);
    Student c = new Student("Carol", "Grape St",
            CollectionToJson.getDOB(2000, 5, 21));
    Student d = new Student("Mallory", "Mango St", null);

    List<Student> students = new ArrayList<Student>();
    students.add(a);
    students.add(b);
    students.add(c);
    students.add(d);

    gson = new Gson();
    String jsonStudents = gson.toJson(students);
    System.out.println("jsonStudents = " + jsonStudents);

    //
    // Converts JSON string into a collection of Student object.
    //
    Type type = new TypeToken<List<Student>>(){}.getType();
    List<Student> studentList = gson.fromJson(jsonStudents, type);

    for (Student student : studentList) {
        System.out.println("student.getName() = " + student.getName());
    }
}

private static Date getDOB(int year, int month, int date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, date);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

}

输出

 jsonNames = ["Alice","Bob","Carol","Mallory"]
jsonStudents = [{"name":"Alice","address":"Apple St","dateOfBirth":"Nov 1, 3900 12:00:00 AM"},{"name":"Bob","address":"Banana St"},{"name":"Carol","address":"Grape St","dateOfBirth":"Jun 21, 3900 12:00:00 AM"},{"name":"Mallory","address":"Mango St"}]
student.getName() = Alice
student.getName() = Bob
student.getName() = Carol
student.getName() = Mallory
于 2013-01-26T20:08:27.890 回答
0

JSONObject 可以将 JSONArray 嵌套在其中,反之亦然。

在您的示例中,您必须创建:

  • 项目清单
  • JSON对象
  • 在其中有一个名为“value”的 JSONObject
  • 在其中有一个名为“name”的字符串条目,其值为“History”。
  • 和一个 JSONArray 条目。
  • 项目清单
  • 在那个 JSONArray 里面有一组 JSONObjects。
于 2013-01-26T20:05:52.120 回答