
本文共 5013 字,大约阅读时间需要 16 分钟。
���������������JSON
������JSON
���������������Java������������JSON���������������������
������������01���������JSONObject
public static void test01() { JSONObject j1 = new JSONObject(); j1.put("name", "������"); j1.put("id", 998856); j1.put("sex", true); j1.put("phone", "15199978652"); String jsonstr = j1.toString(2); System.out.println("������JSON���������\n" + jsonstr);}
������������������JSONObject
������������������������������������������������JSON���������������������������������������������������������������������������JSONObject
������������put
���������������name
���id
���sex
���phone
���������
������������02���������JSONArray������������������������
public static void test02() { JSONArray j = new JSONArray(); j.put("shao"); j.put("wang"); j.put("li"); j.put("guo"); String jsonstr = j.toString(2); System.out.println("������JSON���������\n" + jsonstr);}
���������������������������������������������JSONArray
������������������������toString
���������������������������������������JSON������������������������������������������������������������������������������������������������������������
������������03������POJO���������JSONObject
���������������������������������������POJO���������������������������������������������������getter
������������������������������������������
public static void test03() { Student stu = new Student("������", 159637, true, "15198874562"); JSONObject j = new JSONObject(stu); String jsonstr = j.toString(2); System.out.println("���POJO���������JSON���������\n" + jsonstr);}
������������������������Student
���������������������������������������getter
���������name
���id
���sex
���phone
���JSONObject
������JSONObject
���������������������������������getter
������������������POJO���������������JSON���������
������������04������List���������JSONArray
public static void test04() { Listsss = new ArrayList<>(); sss.add(new Student("guo", 1238901, true, "13810012345")); sss.add(new Student("wang", 1238902, true, "13456678895")); sss.add(new Student("qian", 1238903, false, "1381432435")); sss.add(new Student("chen", 1238904, true, "13342353446")); JSONArray jarray = new JSONArray(sss); String jsonstr = jarray.toString(2); System.out.println("���List���������JSON���������\n" + jsonstr);}
���������������������������������ArrayList
������������������������JSONArray
���������������������������������������JSON������������������������������������������������JSON���������������������������
������������05������������List���������JSONArray
������ArrayList
������������������������������������������������������������������JSONArray
���������
public static void test05() { Listsss = new ArrayList<>(); sss.add(new Student("shao", 1238901, true, "13810012345")); sss.add(new Student("wang", 1238902, true, "13456678895")); sss.add(new Student("qian", 1238903, false, "1381432435")); sss.add(new Student("chen", 1238904, true, "13342353446")); JSONArray jarray = new JSONArray(sss); String jsonstr = jarray.toString(2); System.out.println("���������List���������JSON���������\n" + jsonstr);}
������������������������������������������������������������������������������List
���������������JSONArray
���������������������������������������������������������������������������������������������������������
������JSON
������JSON������
������JSONObject
���������JSON������������
public class ParseJSON { // ������JSON������ public static void test1(String jsonstr) throws Exception { JSONObject j = new JSONObject(jsonstr); String name = j.getString("name"); int id = j.getInt("id"); boolean sex = j.getBoolean("sex"); String phone = j.getString("phone"); // ������������������������������������ JSONObject subobj = j.getJSONObject("xxx"); JSONArray subarray = j.getJSONArray("yyy"); }}
���������������������JSONObject
������������JSON������������������������������������������������������������������������������������������������������������������������������������������������������������getJSONObject
���getJSONArray
���������������������������
������JSON������
������JSONArray
���������JSON���������
public class ParseJSON { // ������JSON������ public static void test2(String jsonstr) throws Exception { JSONArray j = new JSONArray(jsonstr); for (int i = 0; i < j.length(); i++) { JSONObject obj = j.getJSONObject(i); // ������������������ } }}
������������������������������JSONArray
������������������������������������������������������������������������������������getJSONObject(int)
���������������������JSONObject
���������������������������������
������
������������������������������JSON���������JSON���������������������������������������������������������������������������������JSON������������������������JSON���������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
