java生成JSON字符串:import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;//如何生成JSON字符串,有七种情况/* * 运用JSON解析时:将实体类的属性变量设置为公共的,去掉get和set方法也行 */public class ShengChengJsonUtils {//第1种:{"id":11,"age":23,"name":"张三"}要建一个学生实体类,这是官方版JSON:注意要导包public static String createJSON_01(){Student student=new Student(11,23,"张三");JSONObject obj=new JSONObject(student);//实体类没有set和get方法时会打印空return obj.toString();}// 第2种:{"student":{"id":11,"age":20,"name":"张三"}}public static String createJSON_02(){Student student=new Student(11,23,"张三");JSONObject obj=new JSONObject(student);JSONObject obj2=new JSONObject();try {obj2.put("student", obj);} catch (JSONException e) {e.printStackTrace();}return obj2.toString();}// 第3种:{"student":{"id":11,"age":23,"name":"张三"},"class_name":"and1510"}public static String createJSON_03(){//这里用到myclass所以建一个class实体类Student student=new Student(11,23,"张三");MyClass myclass=new MyClass("and1510", student);JSONObject obj=new JSONObject(myclass);return obj.toString();}/* * 第4种 :{"list":[{"id":11,"age":23,"name":"张三"}, * {"id":12,"age":22,"name":"李四"}],"class_name":"and1510"} */public static String createJSON_04(){//这里用到myclass2所以建一个class实体类,而且是数组Student student=new Student(11,23,"张三");Student student2=new Student(12,22,"李四");Listlist=new ArrayList ();list.add(student);list.add(student2);MyClass2 myclass2=new MyClass2("and1510", list);JSONObject obj=new JSONObject(myclass2);return obj.toString();}/** * 第5种 * [{"id":11,"age":23,"name":"张三"},{"id":12,"age":22,"name" * :"张三"}] */public static String createJSON_05(){Student student=new Student(11,23,"张三");Student student2=new Student(12,22,"李四");List list=new ArrayList ();list.add(student);list.add(student2);JSONArray objarr=new JSONArray(list);return objarr.toString();}/**第6种 * {"students":[{"id":11,"age":22,"name":"张三"},{"id":12,"age":22,"name" * :"李四"}]} */public static String createJSON_06(){Student student=new Student(11,23,"张三");Student student2=new Student(12,22,"李四");List list=new ArrayList ();list.add(student);list.add(student2);JSONArray objarr=new JSONArray(list);JSONObject obj=new JSONObject();try {obj.put("students", objarr);} catch (JSONException e) {e.printStackTrace();}return obj.toString();}/**第7种 * {"myclass":{"list":[{"id":11,"age":23,"name":"张三"},{"id":12,"age":22,"name" * :"李四"}],"class_name":"and1510"}} */public static String createJSON_07(){Student student=new Student(11,23,"张三");Student student2=new Student(12,22,"李四");List list=new ArrayList ();list.add(student);list.add(student2);MyClass2 myClass2=new MyClass2("and1510", list);JSONObject obj=new JSONObject(myClass2);JSONObject obj2=new JSONObject();try {obj2.put("myclass", obj);} catch (JSONException e) {e.printStackTrace();}return obj2.toString();}}解析JSON字符串:import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.ObjectToJson.ShengChengJsonUtils;//官方JSON解析//将JSON字符串解析成对象的几种情况的工具类//运用JSON解析时:将实体类的属性变量设置为公共的,去掉get和set方法也行public class ParserJsonToObjectUtils {// 第1:{"id":11,"age":22,"name":"张三"}public static void ParseJsonToObject_01(){String json=ShengChengJsonUtils.createJSON_01();System.out.println("需解析的json字符串:--> "+json);try {JSONObject obj=new JSONObject(json);Student student=new Student();int id=obj.getInt("id");int age=obj.getInt("age");String name=obj.getString("name");student.setId(id);student.setAge(age);student.setName(name);System.out.println("解析结果:");System.out.println(student);} catch (JSONException e) {e.printStackTrace();}}// 第2种:{"student":{"id":11,"age":20,"name":"张三"}}public static void ParseJsonToObject_02() throws JSONException{String json=ShengChengJsonUtils.createJSON_02();System.out.println("需解析的json字符串:--> "+json);JSONObject obj=new JSONObject(json);JSONObject obj2=obj.getJSONObject("student");//通过键student获得JSONObject对象Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentint id=obj2.getInt("id");int age=obj2.getInt("age");String name=obj2.getString("name");student.setId(id);student.setAge(age);student.setName(name);System.out.println("解析结果:");System.out.println(student);}// 第3种:{"student":{"id":11,"age":23,"name":"张三"},"class_name":"and1510"}public static void ParseJsonToObject_03() throws JSONException{String json=ShengChengJsonUtils.createJSON_03();System.out.println("需解析的json字符串:--> "+json);JSONObject obj=new JSONObject(json);MyClass myClass=new MyClass();String class_name=obj.getString("class_name");myClass.setClass_name(class_name);JSONObject obj2=obj.getJSONObject("student");//通过键student获得JSONObject对象Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentint id=obj2.getInt("id");int age=obj2.getInt("age");String name=obj2.getString("name");student.setId(id);student.setAge(age);student.setName(name);myClass.setStudent(student);System.out.println("解析结果:");System.out.println(myClass);}/* * 第4种 :{"list":[{"id":11,"age":23,"name":"张三"}, * {"id":12,"age":22,"name":"李四"}],"class_name":"and1510"} */public static void ParseJsonToObject_04() throws JSONException{String json=ShengChengJsonUtils.createJSON_04();System.out.println("需解析的json字符串:--> "+json);JSONObject obj=new JSONObject(json);MyClass2 myClass2=new MyClass2();myClass2.setClass_name(obj.getString("class_name"));JSONArray objarr=obj.getJSONArray("list");//通过键list获得JSONObject对象List list=new ArrayList ();Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentfor(int i=0;i "+json);JSONArray objarr=new JSONArray(json);List list=new ArrayList ();Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentfor(int i=0;i "+json);JSONObject obj=new JSONObject(json);JSONArray objarr=obj.getJSONArray("students");List list=new ArrayList ();Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentfor(int i=0;i "+json);JSONObject obj=new JSONObject(json);MyClass2 myClass2=new MyClass2();JSONObject obj2=obj.getJSONObject("myclass");myClass2.setClass_name(obj2.getString("class_name"));JSONArray array=obj2.getJSONArray("list");List list=new ArrayList ();Student student=new Student();//再从JSONObject对象中获得指定值所对应的值。并赋给Studentfor(int i=0;i list;public MyClass2(String class_name, List list) {super();this.class_name = class_name;this.list = list;}@Overridepublic String toString() {return "MyClass2 [class_name=" + class_name + ", list=" + list + "]";}public String getClass_name() {return class_name;}public void setClass_name(String class_name) {this.class_name = class_name;}public List getList() {return list;}public void setList(List list) {this.list = list;}public MyClass2() {super();// TODO Auto-generated constructor stub}}