JSON总结-持续更新补充

基本的json格式

{
    "name": "jobs",
    "boolean": true,
    "age": null,
    "num": 88
}

json对象

{
    "starcraft": {
        "INC": "Blizzard",
        "price": 60
    }
}

json数组

{
    "person": [
        "jobs",
        60
    ]
}

json对象数组

{
    "array": [
        {
            "name": "jobs"
        },
        {
            "name": "bill",
            "age": 60
        },
        {
            "product": "war3",
            "type": "game",
            "popular": true,
            "price": 60
        }
    ]
}

常见的JSON解析工具

json-lib的net.sf.json的json处理包

  优点:老牌,应用广泛

  缺点:jar包依赖多、转换成bean存在不足(bean里面存在list集合,map等)、性能不足

  依赖jar包:

    - commons-beanutils-1.8.0.jar    

    - commons-collections-3.2.1.jar

    - commons-lang-2.5.jar

    - commons-logging-1.1.1.jar

    - ezmorph-1.0.6.jar

    - json-lib-2.4-jdk15.jar

阿里巴巴官方出的fastjson处理包

  优点:高性能,不需要额外的jar,可以直接跑在jdk上面,速度极致

  缺点:bean转换为json时可能会存在错误(类型上面)

谷歌官方出品的Gson

  优点:无依赖,直接跑在jdk上,只要存在getter、setter方法就可以解析,神器!

  缺点:必须在对象转换之前创建好对象的类型以及其成员,性能稍差

json-lib对json数据的处理

package com.glorze.json;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonlibUtil {
    public static void main(String[] args) {
        Set<Book> bookSet = new HashSet<Book>();
        Book book=new Book();
        book.setId("0808");
        book.setName("基督山伯爵");
        bookSet.add(book);

        Student student = new Student();
        student.setName("高老四");
        student.setAge(25);
        student.setDescribe("男神、帅哥");
        student.setSex("男");
        student.setBooks(bookSet);

        //Bean转换成json字符串
        String json = JSONObject.fromObject(student).toString();
        System.out.println("Bean转换成Json:"+json);

        //json转Bean
        String jsonStr = "{\"id\":\"2\",\"name\":\"Json技术\"}";
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);
        Book book2 = (Book) JSONObject.toBean(jsonObject,Book.class);
        System.out.println("json字符串转Bean:"+book2.toString());

        //json转换成List(Set同理)
        String jsonStr2 = "[{\"id\":\"1\",\"name\":\"Java技术\"},{\"id\":\"2\",\"name\":\"Json技术\"}]";
        JSONArray jsonArray = JSONArray.fromObject(jsonStr2);
        JSONObject jsonObject2 = null;
        Book book3 = null;
        int size = jsonArray.size();
        List list = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            jsonObject2 = jsonArray.getJSONObject(i);
            book3 = (Book) JSONObject.toBean(jsonObject, Book.class);
            list.add(book3);
        }
        System.out.println("json字符串转List:"+JSONArray.fromObject(list).toString());
    }
}

fastjson对json数据的处理

package com.glorze.json;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class FastjsonUtil {
    public static void main(String[] args) {
        Set<Book> bookSet = new HashSet<Book>();
        Book book=new Book();
        book.setId("0808");
        book.setName("基督山伯爵");
        bookSet.add(book);

        Student student = new Student();
        student.setName("高老四");
        student.setAge(25);
        student.setDescribe("男神、帅哥");
        student.setSex("男");
        student.setBooks(bookSet);

        //Bean转换成json字符串
        String json=JSON.toJSONString(student, false);// true:格式化 false(默认):不格式化
        System.out.println("Bean转换成Json:"+json);

        //json转Bean
        String jsonStr = "{\"id\":\"2\",\"name\":\"Json技术\"}";
        Book book2 = JSON.parseObject(jsonStr,Book.class);
        System.out.println("json字符串转Bean:"+book2.toString());

        //json转换成List(Set同理)
        String jsonStr2 = "[{\"id\":\"1\",\"name\":\"Java技术\"},{\"id\":\"2\",\"name\":\"Json技术\"}]";
        List<Book> bookList = JSON.parseObject(jsonStr2,new TypeReference<ArrayList<Book>>(){});
        System.out.println("json字符串转List:"+JSON.toJSONString(bookList,true));

    }
}

Gson对json数据的处理

package com.glorze.json;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonUtil {
    public static void main(String[] args) {

        Set<Book> bookSet = new HashSet<Book>();
        Book book=new Book();
        book.setId("0808");
        book.setName("基督山伯爵");
        bookSet.add(book);

        Student student = new Student();
        student.setName("高老四");
        student.setAge(25);
        student.setDescribe("男神、帅哥");
        student.setSex("男");
        student.setBooks(bookSet);

        //bean转换成json
        Gson gson = new Gson();
        String json = gson.toJson(student);
        System.out.println("Bean转换成Json:"+json);

        //json转Bean
        String jsonStr = "{\"id\":\"2\",\"name\":\"Json技术\"}";
        Book book2 = gson.fromJson(jsonStr, Book.class);
        System.out.println("json字符串转换成Bean:"+book2.toString());

        //json转换成List(Set同样)
        String jsonList = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
        List<Book> bookList = gson.fromJson(jsonList, new TypeToken<List<Book>>(){}.getType());
        String listJson = gson.toJson(bookList);
        System.out.println("json转换成List:"+listJson);

    }
}

附三个封装工具类

JsonlibUtil.java FastjsonUtil.java GsonUtil.java

下载地址

网盘链接:http://pan.baidu.com/s/1c2mu1Ni 密码:72qh

更多内容请查看 高老四博客

时间: 2024-12-21 06:40:55

JSON总结-持续更新补充的相关文章

iOS之github第三方框架(持续更新)

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

自己总结的 iOS ,Mac 开源项目以及库,知识点------持续更新

自己在 git  上看到一个非常好的总结的东西,但是呢, fork  了几次,就是 fork  不到我的 git 上,干脆复制进去,但是,也是认真去每一个每一个去认真看了,并且也是补充了一些,感觉非常棒,所以好东西要分享,为啥用 CN 博客,有个好处,可以随时修改,可以持续更新,不用每次都要再发表,感觉这样棒棒的 我们 自己总结的iOS.mac开源项目及库,持续更新.... github排名 https://github.com/trending,github搜索:https://github.

转-推荐的几个开发常用在线工具,可以提升开发效率(持续更新)

http://blog.csdn.net/kroclin/article/details/40634975 相信开发中每个人手头上面都有那么几个工具可以让你每天洋洋得意的开发软件,而这里我就将我觉得还挺不错的几款在线工具分享出来,仁者见仁啦,喜欢就拿走.还会持续更新,以后有新的我都贴上来. 1.MD5解密:http://www.cmd5.com/ 2.MD5加密:http://md5jiami.51240.com/ 3.json在线解析工具:http://json.parser.online.f

动态加载页面数据的小工具 javascript + jQuery (持续更新)

使用该控件,可以根据url,参数,加载html记录模板(包含json参数对应,以及具体记录位置Index根据参数描述加载对应的属性,并可以根据简单的判断分支加载对应html或者控件)至列表容器内(JQuery选择器字符串)注: 该控件在使用前需引入JQuery框架支持,使用该控件,可极大的减少Ajax列表数据动态加载开发工作的实际工作量. 使用方式: 首先,添加控件引用,并加入Jquery支持 <script src="js/jquery.js"></script&g

iOS:开发常用GitHub开源项目(持续更新)

IOS开发常用GitHub开源项目(持续更新) 数据类 开源库 作者 简介 AFNetworking Mattt 网络请求库 ASIHTTPRequest pokeb 网络请求库 Alamofire cnoon Swift简洁网络请求库 SBJson stig Json解析引擎 JSONKit johnezang Json解析引擎 MJExtension CoderMJLee 字典转模型框架 KissXML robbiehanson XML解析 RNCryptor rnapier AES加密 F

android开发开源宝贝——持续更新。。。

2016年11月11日更新 http://www.apkbus.com/forum-417-1.html http://p.codekk.com/detail/Android/hejunlin2013/LivePlayback www.codekk.com https://github.com/Trinea/android-open-project Android 开源项目分类汇总 我们的微信公众号:codekk.二维码如下: 专注于 Android 开源分享.源码解析.框架设计.Android

神技!微信小程序(应用号)抢先入门体验(附最新案例-豆瓣电影)持续更新

微信小程序 Demo(豆瓣电影) 由于时间的关系,没有办法写一个完整的说明,后续配合一些视频资料,请持续关注 官方文档:https://mp.weixin.qq.com/debug/wxadoc/dev/ Demo 预览 演示视频(流量预警 2.64MB) GitHub Repo 地址 仓库地址:https://github.com/zce/weapp-demo 使用步骤 将仓库克隆到本地: bash $ git clone https://github.com/zce/weapp-demo.g

python tips(持续更新)

1. 引用上一层目录 import syssys.path.append('..')import xx 2. python json JSON是一种轻量级的数据交换格式.可以解决数据库中文存储问题,对象序列化问题,等等. import json encodedjson = json.dumps(obj) decodejson = json.loads(encodedjson) 非常简单. 3. 静态方法 在函数前面@staticmethod @staticmethod def func(): p

android开源应用(主要是博客上带有分析的)收集 【持续更新】

2014.5.24更新: (android高仿系列)今日头条    http://blog.csdn.net/vipzjyno1/article/details/26514543 CSDN Android客户端的制作    http://blog.csdn.net/lmj623565791/article/details/26676137 LookAround开元之旅         http://blog.csdn.net/lancees/article/details/17696805 如果