那么多json库

本文介绍了JSON的基本概念,包括其定义、特点及优势,并列举了多种用于处理JSON数据的Java类库,如json-lib、org.json、gson、fastjson和jackson等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 什么是json

什么是 JSON ?

    JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
    JSON 是轻量级的文本数据交换格式
    JSON 独立于语言 *
    JSON 具有自我描述性,更易理解

2. 好多json类库

我们在java编程中用到json的时候会经常引入json类库,我们进行简单的罗列

2.1 json-lib

项目地址:http://json-lib.sourceforge.net/index.html

(1)pom文件
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>
(2)示例
    /**
     * 将对象序列化成json字符串
     * @param obj
     * @return
     */
    public static String bean2Json(Object obj){
        JSONObject jsonObject=JSONObject.fromObject(obj);
        return jsonObject.toString();
    }
    
    /**
     * 将json字符串反序列化为对象
     * @param jsonStr
     * @param objClass 反序列化为该类的对象
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T json2Bean(String jsonStr,Class<T> objClass){
        return (T)JSONObject.toBean(JSONObject.fromObject(jsonStr), objClass);
    }

2.2 org.json

(1)pom文件
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

2.3 gson

项目地址:https://github.com/google/gson

(1)pom文件
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

2.4 fastjson

项目地址:https://github.com/alibaba/fastjson

(1)pom文件
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.56</version>
</dependency>
2.5 jackson

项目地址:https://github.com/FasterXML/jackson

(1)pom文件
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值