zoukankan      html  css  js  c++  java
  • json一 ------Gson

    1.什么是json

      json:就是一种轻量级的数据交换格式

         json语法:“键”:“值”组成,多个之间通过逗号隔开,{}表示对象,[]表示数组

      json字符串: '{"a": "Hello", "b": "World"}';

      json对象: {a: 'Hello', b: 'World'};

      json数组: ["a":"Hello"] ;

      json对象数组: [ {"name":"张三","age":20},

               {"name":"李四","age":21} ]

      更多详情请看官网:http://www.json.org/json-zh.html

    2.前台

      将对象转为json:JSON.stringify(对象)

      将json转为对象:JSON.parse(json)

    3.后台(总的来说就是将对象、集合转为json,将字符串json转为对象、集合)

      一:Google的Gson,需要json.jar包,(fromJson()将json字符串转为对象、集合,toJson()将集合、对象转为json)

        下载地址:http://download.csdn.net/download/muyeju/9999530

        1、将json字符串转为对象:gson对象.fromJson(字符串, 实体.class) ;

    package com.cn.test.test;
    
    import com.cn.test.pojo.TestStudent;
    import com.google.gson.Gson;
    
    public class Test {
        
        public static void main(String[] args) {
            String student = "{'name':'张三','age':20}" ;
            Gson gson = new Gson() ;
            TestStudent s = gson.fromJson(student, TestStudent.class) ;
            System.out.println("-----------name----------"+s.getName());
            System.out.println("-----------age-----------"+s.getAge());
        }
    }

        2、将json字符串转为集合: gson对象.fromJson(字符串, new TypeToken<List<实体>>(){}.getType()) ; TypeToken是gson提供的数据类型转换器,可以支持各种数据集合类型转换。

    package com.cn.test.test;
    
    import java.util.List;
    
    import com.cn.test.pojo.TestStudent;
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    
    public class Test {
        
        public static void main(String[] args) {
            String student = "[{'name':'张三','age':20},{'name':'李四','age':21}]" ;
            Gson gson = new Gson() ;
            List<TestStudent> s = gson.fromJson(student, new TypeToken<List<TestStudent>>(){}.getType()) ;
            for (TestStudent testStudent : s) {
                System.out.println("-----------name----------"+testStudent.getName());
                System.out.println("-----------age-----------"+testStudent.getAge());
            }
        }
    }

        3、将对象转为json:toJson(对象)

    package com.cn.test.test;
    
    import com.cn.test.pojo.TestStudent;
    import com.google.gson.Gson;
    
    public class Test {
        
        public static void main(String[] args) {
            TestStudent s = new TestStudent() ;
            s.setAge(20);
            s.setName("张三");
            Gson gson = new Gson() ;
            String student = gson.toJson(s) ;
            System.out.println("------------------"+student);
        }
    }
    结果:------------------{"name":"张三","age":20}

        4、将集合转为json:toJson(集合)

    package com.cn.test.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.cn.test.pojo.TestStudent;
    import com.google.gson.Gson;
    
    public class Test {
        
        public static void main(String[] args) {
            List<TestStudent> student = new ArrayList<TestStudent>() ;
            TestStudent s = new TestStudent() ;
            s.setAge(20);
            s.setName("张三");
            TestStudent s1 = new TestStudent() ;
            s1.setAge(30);
            s1.setName("李四");
            student.add(s) ;
            student.add(s1) ;
            Gson gson = new Gson() ;
            String testStudent = gson.toJson(student) ;
            System.out.println("------------------"+testStudent);
        }
    }
    结果:------------------[{"name":"张三","age":20},{"name":"李四","age":30}]

    4.总结

      Gson是目前功能最全的Json解析神器,是Google公司开发的,Gson的应用主要是toJson与fromJson两个转换函数,Gson在功能上面无可挑剔,但是性能上面不如FastJson

  • 相关阅读:
    Clob实践
    Read Clob字段 Function最终版
    asp.net mvc proflie 使用 区别 疑惑
    csdn中关于程序员的一封信
    【解惑】女生适合干计算机什么方面的工作
    网络女友跟现实女友的区别
    [转]俞敏洪老师郑州大学演讲
    【解惑】这么多技术我该怎么学
    google使用技巧
    杨澜给二十几岁女孩子的忠告
  • 原文地址:https://www.cnblogs.com/-scl/p/7599481.html
Copyright © 2011-2022 走看看