Json


public class JsonTools {
    public JsonTools() {

    }

    // {"persons":[{"id":1001,"address":"China","name":"cat"},{"id":1002,"address":"Japan","name":"dog"}]}
    public static JSONObject createArrayJson(List<Person> lists) {
        JSONObject persons = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            for (Person list : lists) {
                JSONObject person = new JSONObject();

                person.put("id", list.getId());
                person.put("name", list.getName());
                person.put("address", list.getAddress());
                array.put(person);
            }
            persons.put("persons", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return persons;
    }

    public static List<Person> analysisArrayJson(String json) {
        List<Person> lists = new ArrayList<Person>();
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("gg", e.getMessage(), e);
        }
        if (jo == null) {
            return lists;
        }

        JSONArray ja = jo.optJSONArray("persons");
        if (ja != null) {
            for (int i = 0; i < ja.length(); i++) {
                JSONObject obj = ja.optJSONObject(i);
                if (obj == null) {
                    continue;
                }
                Person person = new Person();
                person.setId(obj.optInt("id"));
                person.setName(obj.optString("name"));
                person.setAddress(obj.optString("address"));
                lists.add(person);
            }
        }
        return lists;
    }

    // {"id":"1","name":"李磊","age":"30"}
    public static String createSimpleJson() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("id", 1);
            jo.put("name", "李磊");
            jo.put("age", 30);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static void analysisSmipleJson(String json) {
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (jo == null) {
            return;
        }
        int age = jo.optInt("age");
        String name = jo.optString("name");
        int id = jo.optInt("id");
        id++;
    }

    // 假设现在要创建这样一个json文本
    // {
    // "id" : "1",                  // 字符串
    // "phone" : ["110", "120"],    // 数组
    // "age" : 100,                 // 数值
    // "address" : { "country" : "Chian", "city" : "Beijing" }, // 对象
    // "married" : false            // 布尔值
    // }
    public static String createcomplexJson() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("id", "1");

            JSONArray array = new JSONArray();
            array.put("110");
            array.put("120");
            jo.put("phone", array);

            jo.put("age", 30);

            JSONObject address = new JSONObject();
            address.put("country", "China");
            address.put("city", "Beijing");
            jo.put("address", address);

            jo.put("married", false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static void analysisComplexJson(String json) {
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (jo == null) {
            return;
        }

        String id = jo.optString("id");
        JSONArray ja = jo.optJSONArray("phone");
        if (ja != null) {
            for (int i = 0; i < ja.length(); i++) {
                String phone = ja.optString(i);
            }
        }

        int age;
        // try {
        //  age = jo.getInt("agea");
        // } catch (JSONException e) {
        //  age = 0;
        //  e.printStackTrace();
        // }
        age = jo.optInt("age"); // 如果有错,有默认值:0
        JSONObject address = jo.optJSONObject("address");
        if (address != null) {
            String country = address.optString("country");
            String city = address.optString("city");
        }
        boolean married = jo.optBoolean("married");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值