标签:
* {} 大括号表示对象,[] 方括号表示数组或集合
* 单个对象用JsonObject
Student2 stu = new Student2(1, "张三", 20); // 生成json字符串。 JSONObject obj = new JSONObject(stu); System.out.println(obj.toString());
* 多个对象用JsonArray
List<Student2> list = new ArrayList<Student2>(); for(int i=0;i<5;i++){ Student2 stu = new Student2(i,"aa"+i,20+i); list.add(stu); } //创建json字符串。 JSONArray array = new JSONArray(list); System.out.println(array.toString());
JSON生成
// 1:{"id":1,"age":20,"name":"张三"} public static String fun1(){ Student stu = new Student(1,"zhangsan",20); JSONObject obj = new JSONObject(stu); return obj.toString(); } // 2:{"student":{"id":1,"age":20,"name":"张三"}} public static String fun2(){ Student stu = new Student(1,"zhangsan",20); JSONObject obj = new JSONObject(stu); JSONObject obj2 = new JSONObject(); try { obj2.put("student", obj); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return obj2.toString(); } // 3:{"student":{"id":1,"age":20,"name":"张三"},"class_name":"and1510"} public static String fun3(){ Student stu = new Student(1,"zhangsan",20); MyClass myclass = new MyClass(stu,"and1510"); JSONObject obj = new JSONObject(myclass); return obj.toString(); } //4 {"list":[{"id":1,"age":20,"name":"张三"}, //{"id":2,"age":30,"name":"张三"}],"class_name":"and1510"} public static String fun4(){ Student stu1 = new Student(1,"zhangsan",20); Student stu2 = new Student(2,"lisi",21); List<Student> stulist = new ArrayList<Student>(); stulist.add(stu1); stulist.add(stu2); MyClass2 myclass = new MyClass2("and1510", stulist); JSONObject obj1 = new JSONObject(myclass); return obj1.toString(); } /*[{"id":1,"age":20,"name":"张三"},{"id":2,"age":30,"name" * :"张三"}]*/ public static String fun5(){ Student stu1 = new Student(1,"zhangsan",20); Student stu2 = new Student(2,"lisi",21); List<Student> stulist = new ArrayList<Student>(); stulist.add(stu1); stulist.add(stu2); JSONArray array = new JSONArray(stulist); return array.toString(); } /*{"myclass":{"list":[{"id":1,"age":20,"name":"张三"},{"id":2,"age":30,"name" * :"张三"}],"class_name":"and1510"}}*/ public static String fun6(){ Student stu1 = new Student(1,"zhangsan",20); Student stu2 = new Student(2,"lisi",21); List<Student> stulist = new ArrayList<Student>(); stulist.add(stu1); stulist.add(stu2); MyClass2 myclass = new MyClass2("and1510", stulist); JSONObject obj1 = new JSONObject(myclass); JSONObject obj2 = new JSONObject(); try { obj2.put("myclss", obj1); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return obj2.toString(); }
JSON解析
//{"student":{"id":1,"age":20,"name":"张三"}} String json = JSONService.createJSON_02(); System.out.println(json); Student stu = new Student(); JSONObject obj = new JSONObject(json); //通过键student获得JSONObject对象 JSONObject obj2 = obj.getJSONObject("student"); //再从JSONObject对象中获得指定值所对应的值。并赋给Student stu.setName(obj2.getString("name")); stu.setAge(obj2.getInt("age")); stu.setId(obj2.getInt("id")); System.out.println(stu);
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5243483.html