Gson

作者: 安安静静写代码 | 来源:发表于2017-08-25 17:22 被阅读9次

谷歌对于JSON解析提供了一个开源框架称为GSON,相比于Java的Json要简单的多

解析


import com.google.gson.Gson;
import com.qf.demo.Person;

/**
 * 谷歌的gson
 * @author Administrator
 *
 */
public class Test {

    public static void main(String[] args) {
        String string  = "{name:'zhangsan',age:20}";
        /**
         * 1 创建工具对象
         */
        Gson gson = new Gson();
        // 第一个参数是要解析的json字符串
        Person person = gson.fromJson(string, Person.class);
        System.out.println(person);
        
        
        
    }
}

反向操作

package com.qf.demo4;

import com.google.gson.Gson;
import com.qf.demo3.Dept;
import com.qf.demo3.Person;

public class Test2 {
    
    public static void main(String[] args) {
        Person person = new Person("zhangsan", 10, new Dept(2, "hehe"));
        
        Gson gson = new Gson();
        String json = gson.toJson(person);
        System.out.println(json);           
    }
}

相关文章

网友评论

      本文标题:Gson

      本文链接:https://www.haomeiwen.com/subject/dgxpdxtx.html