JSON-4/6(JSON的生成与管理 )
发布日期:2021-05-14 04:14:28 浏览次数:24 分类:精选文章

本文共 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() {    List
sss = 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() {    List
sss = 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���������������������������������������������������������������������������������������������������������

上一篇:JSON-5/6(保存JSON到文件 )
下一篇:JSON-3/6(JSON 语法格式)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月07日 13时03分06秒