java json解析jar包基本使用记录

一、使用json-lib.jar包处理

/*--------------------------------------------json字符串--->json对象----------------------------------------------------*/
/**
 *  简单的json解析
 */
public static void test1() {
    String jString = "{'name':'lisi','age':18}";
    try {
        JSONObject jsonObject = new JSONObject(jString);
        String name = jsonObject.getString("name");
        int age = jsonObject.getInt("age");
        Student student = new Student(name, age);
        System.out.println(student);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 *   包含对象的
 */
public static void test2() {

    String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':90}}";

    try {
        JSONObject jsonObject = new JSONObject(jString);
        String name = jsonObject.getString("name");
        int age  = jsonObject.getInt("age");

        JSONObject scoreObj = jsonObject.getJSONObject("score");
        int math = scoreObj.getInt("math");
        int chinese = scoreObj.getInt("chinese");

        Student student = new Student(name, age,new Score(math, chinese));
        System.out.println(student);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
/**
 * 字符串数组转转换
 */
public static void  test3() {

    String jString = "{'studentList':[{'name':'张山','age':19,'score':{'math':'39','chinese':'50'}},{'name':'耿钊','age':17,'score':{'math':'68','chinese':'95'}}]}";
    try {
        JSONObject jsonObject = new JSONObject(jString);
        String arrStr = jsonObject.getString("studentList");
        JSONArray array = new JSONArray(arrStr);

        ArrayList<Student> list = new ArrayList<>();
        JSONObject object = null;
        Student student = null;
        Score score = null;

        for (int i = 0; i < array.length(); i++) {
            object = array.getJSONObject(i);

            String name = object.getString("name");
            int age = object.getInt("age");

            JSONObject scoreObj = object.getJSONObject("score");
            int math = scoreObj.getInt("math");
            int chinese = scoreObj.getInt("chinese");

            score = new Score(math, chinese);
            student = new Student(name, age, score);

            list.add(student);
        }
        //打印输出
        for (Student student2 : list) {
            System.out.println(student2);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
/*--------------------------------------------对象--->json字符串----------------------------------------------------*/

/**
 * 简单对象转json字符串对象
 * @throws Exception 
 */
public static void test4() throws Exception {

    Student student = new Student("lisi", 23);

    JSONObject jsonObject = new JSONObject(student);

    System.out.println(jsonObject.getString("name"));
}

/**
 * 复杂对象转json字符串对象
 */
public static void test5() {

    Student student = new Student("耿钊", 18, new Score(67, 89));

    JSONObject jsonObject = new JSONObject(student);

    System.out.println(jsonObject);
}

/**
 * 数组转json
 */
public static void test6() {

    ArrayList< Student> list = new ArrayList<>();

    Student stu1 = new Student("lisi", 18, new Score(100, 100));
    Student stu2 = new Student("耿钊", 28, new Score(80, 90));

    list.add(stu1);
    list.add(stu2);

    Message message = new Message(list, 1000);

//JSONArray jsonArray = new JSONArray(list);
    JSONObject jsonObject  = new JSONObject(message);

    System.out.println(jsonObject);
    }
}

二、使用gson-2.2.1.jar

public class GsonTest {

    /**
     * 字符串转换成对象
     */
    public static void test1() {

        String jsString = "{'name':lisi','age':18}";

        Gson gson = new Gson();

        Student student = gson.fromJson(jsString, Student.class);

        System.out.println(student);
    }

    /**
     * 复杂字符串转换
     */
    public static void test2() {

        String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";

        Gson gson = new Gson();

        Student student = gson.fromJson(jString, Student.class);

        System.out.println(student);
    }

    /**
     * 转成集合
     */
    public static void test3() {

        String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
                + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
                + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";

        Gson gson = new Gson();

        // 下面这种方式行不通
        /*
         * ArrayList<Student> list = gson.fromJson(jString, ArrayList.class);
         * for (Student student : list) { System.out.println(student); }
         */
        // TypeToken<ArrayList<Student>> token = new
        // TypeToken<ArrayList<Student>>(){};
        TypeToken<ArrayList<Student>> typeToken = new TypeToken<ArrayList<Student>>() {
        };
        ArrayList<Student> list = gson.fromJson(jString, typeToken.getType());

        for (Student student : list) {
            System.out.println(student);
        }
    }

    /**
     * 将java对象转成json字符串
     */
    public static void test4() {

        Student student = new Student("lisi", 18, new Score(100, 100));

        Gson gson = new Gson();

        System.out.println(gson.toJson(student));

    }

    /**
     * 将java集合对象转成json
     */
    public static void test5() {

        Student student1 = new Student("lisi", 18, new Score(100, 100));
        Student student2 = new Student("耿钊", 10, new Score(100, 100));
        Student student3 = new Student("penghui", 19, new Score(100, 100));

        ArrayList<Student> list = new ArrayList<>();

        list.add(student1);
        list.add(student2);
        list.add(student3);
        Gson gson = new Gson();
        System.out.println(gson.toJson(list));
        ;
    }

三、使用fastjson-1.1.22.jar

public class FastJson {

/**
 * json字符串转换成对象
 * 
 *注意:对象定义有参构造函数后,要显示声明无参构造函数,否则会抛异常、
 */
public static void test1() {

    String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";

    Student student = JSON.parseObject(jString,Student.class);

    System.out.println(student);
}
/**
 * json字符串转换成数组集合
 */
public static void test2() {

    String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
            + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
            + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";

    List<Student> list = JSON.parseArray(jString, Student.class);

    for (Student student : list) {
        System.out.println(student);
    }
}

/**
 * java对象转换成json字符串;
 */
public static void test3() {

    Student student = new Student("lisi", 18, new Score(100,199));

    String string = JSON.toJSONString(student);

    System.out.println(string);
}

public static void test4() {

    Student student1 = new Student("lisi", 18, new Score(100, 100));
    Student student2 = new Student("耿钊", 10, new Score(100, 100));
    Student student3 = new Student("penghui", 19, new Score(100, 100));

    ArrayList<Student> list = new ArrayList<>();

    list.add(student1);
    list.add(student2);
    list.add(student3);

    System.out.println(JSON.toJSONString(list));

}

总结

以上为个人学习笔记,仅供参考!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值