0%

java之json

添加依赖

  • 首先需要在pom.xml添加依赖文件,本次用的阿里提供的解析json的包
1
2
3
4
5
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.42</version>
</dependency>
  • 常见用法
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
34
35
36
37
38
package org.example;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class Main {
public static void main(String[] args) {
String jsonString = "{\"name\":\"Tom\",\"age\":20,\"address\":{\"street\":\"123 Main St\",\"city\":\"New York\",\"state\":\"NY\"},\"phoneNumbers\":[{\"type\":\"home\",\"number\":\"123-456-7890\"},{\"type\":\"work\",\"number\":\"111-222-3333\"}]}";

// 将JSON字符串解析为JSONObject对象
JSONObject jsonObj = JSON.parseObject(jsonString);
// 新增
jsonObj.put("id", 1001);
// 新增一个数组
String[] strings = {"哈哈","忸怩"};
jsonObj.put("test",strings);
// ...{"id":1001,"test":["哈哈","忸怩"]}
System.out.println(jsonObj);

// 从JSONObject中获取属性值
String name = jsonObj.getString("name");
int age = jsonObj.getIntValue("age");
// map里面嵌套map
JSONObject address = jsonObj.getJSONObject("address");
String street = address.getString("street");
String city = address.getString("city");
String state = address.getString("state");
// 循环json中的数组
JSONArray phoneNumbers = jsonObj.getJSONArray("phoneNumbers");
for (int i = 0; i < phoneNumbers.size(); i++) {
JSONObject phone = phoneNumbers.getJSONObject(i);
String type = phone.getString("type");
String number = phone.getString("number");
System.out.println(type + ": " + number);
}
}
}

还有其他更多用法,比如删除、替换、查询是否存在等,不在演示

构建json对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.example;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class JsonToBean {
public static void main(String[] args) {
// Json对象中添加的是键值对
JSONObject jsonObject = new JSONObject();
jsonObject.put("1","a");
jsonObject.put("2","b");
jsonObject.put("3","c");
// JSONArray中添加的是Json对象
JSONArray JsonArray = new JSONArray();

jsonObject.put("key", "value");//JSONObject对象中添加键值对
JsonArray.add(jsonObject);//将JSONObject对象添加到Json数组中

System.out.println(jsonObject);
System.out.println(JsonArray);
}
}

结果如下:

{“1”:”a”,”2”:”b”,”3”:”c”,”key”:”value”}
[{“1”:”a”,”2”:”b”,”3”:”c”,”key”:”value”}]

JavaBean转Json

  • user.class
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
package org.example;

public class User {
private int id;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
@Override
public String toString() {
return "user:id" + id +"-name:" + name + "-sex:" + sex;
}
}

  • JsonToBean.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.example;
import com.alibaba.fastjson2.JSONObject;

public class JsonToBean {
public static void main(String[] args) {
User user = new User();
user.setSex(null);
user.setName("行三");
user.setId(1);
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(user));
System.out.println(jsonObject);
}
}

结果

{“id”:1,”name”:”行三”}

sex为null丢失了

Json转JavaBean

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
package org.example;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.List;

public class JsonToBean {
public static void main(String[] args) {
String jsonString = "{\"name\":\"Tom\",\"sex\":\"男\"}";
// 前面是JSON字符串 后面是java对象类型
User user=JSONObject.parseObject(jsonString,User.class);
// name: Tom---sex: 男
System.out.println("name: "+user.getName()+"---"+"sex: "+user.getSex());
String listStr = "[{\"name\":\"Tom\",\"sex\":\"男\"},{\"name\":\"jay\",\"sex\":\"女\"}]";

// 转Json List
JSONArray jsonArray = JSONArray.parseArray(listStr);
// [{"name":"Tom","sex":"男"},{"name":"jay","sex":"女"}]
System.out.println(jsonArray);
// 转具体的List<实体类> 然后循环
List<User> userList = JSONArray.parseArray(listStr, User.class);
for (User item : userList){
System.out.println(item.toString());
System.out.println(item.getName());
}

}
}