JSONObject

什么是JSONObject?

JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。

JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。

JSONObject相关依赖jar包

1、方法一(直接下载)

下载地址(含1、2两种架包)http://download.csdn.net/download/justinqin/10158995

2、方法二(maven pom.xml依赖)

com.alibaba.fastjson.JSONObject依赖代码

1
2
3
4
5
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
</dependency>

net.sf.json.JSONObject依赖代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!-- JSONObject对象依赖的jar包 -->
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ezmorph</groupId>
        <artifactId>ezmorph</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.2.3</version>
        <classifier>jdk15</classifier><!-- 指定jdk版本 -->
    </dependency>
    <!-- Json依赖架包下载 -->

JSONObject基本使用

1.通过原生生成json数据格式。

1
2
3
4
5
6
7
8
9
JSONObject zhangsan = new JSONObject();
    try {
        //添加
        zhangsan.put("name", "张三");
        zhangsan.put("age", 18.4);
        System.out.println(zhangsan.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

2.通过hashMap数据结构生成

1
2
3
4
HashMap<String, Object> map= new HashMap<>();
 map.put("name", "张三");
 map.put("age", 18.4);
 System.out.println(new JSONObject(map).toString());

3.通过实体生成 

1
2
3
4
5
6
7
8
Student student = new Student();
student.setId(1);
student.setAge("20");
student.setName("张三");
//生成json格式
System.out.println(JSON.toJSON(student));
//对象转成string
String stuString = JSONObject.toJSONString(student);

4.JSON字符串转换成JSON对象

1
2
3
4
5
6
String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";
  
//JSON字符串转换成JSON对象
JSONObject jsonObject1 = JSONObject.parseObject(stuString);
  
System.out.println(jsonObject1);

5.list对象转listJson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ArrayList<Student> studentLsit = new ArrayList<>();
    Student student1 = new Student();
    student1.setId(1);
    student1.setAge("21");
    studentLsit.add(student1);
 
    Student student2 = new Student();
    student2.setId(2);
    student2.setAge("22");
    studentLsit.add(student2);
 
    //list转json字符串
    String string = JSON.toJSON(studentLsit).toString();
    System.out.println(string);
 
    //json字符串转listJson格式
    JSONArray jsonArray = JSONObject.parseArray(string);
    System.out.println(jsonArray);

阿里的json很好用,还有一个谷歌Gson也不错。有兴趣的可以看一看!

posted @   江南大才子  阅读(825)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示