Android:尝试从SQLite数据库条目创建JSON文件

时间:2013-01-26 19:49:49

标签: android json sqlite

所以我试图从我的数据库中提取数据,并基本上创建一个看起来像这样的JSON文件。我知道我可以使用my_json_object.put();但这似乎只适用于String,String。我和我想用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"}
]}}

3 个答案:

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

答案 1 :(得分:2)

使用谷歌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

答案 2 :(得分:0)

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

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

  • 列表项
  • 的JSONObject
  • 里面有一个名为“value”的JSONObject
  • 在其中包含一个名为“name”的字符串条目,其字符串值为“History”。
  • 和JSONArray条目。
  • 列表项
  • 在JSONArray里面有一组JSONObjects。