添加依赖
- 首先需要在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\"}]}";
JSONObject jsonObj = JSON.parseObject(jsonString); jsonObj.put("id", 1001); String[] strings = {"哈哈","忸怩"}; jsonObj.put("test",strings); System.out.println(jsonObj);
String name = jsonObj.getString("name"); int age = jsonObj.getIntValue("age"); JSONObject address = jsonObj.getJSONObject("address"); String street = address.getString("street"); String city = address.getString("city"); String state = address.getString("state"); 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) { JSONObject jsonObject = new JSONObject(); jsonObject.put("1","a"); jsonObject.put("2","b"); jsonObject.put("3","c"); JSONArray JsonArray = new JSONArray();
jsonObject.put("key", "value"); JsonArray.add(jsonObject);
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
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; } }
|
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\":\"男\"}"; User user=JSONObject.parseObject(jsonString,User.class); System.out.println("name: "+user.getName()+"---"+"sex: "+user.getSex()); String listStr = "[{\"name\":\"Tom\",\"sex\":\"男\"},{\"name\":\"jay\",\"sex\":\"女\"}]";
JSONArray jsonArray = JSONArray.parseArray(listStr); System.out.println(jsonArray); List<User> userList = JSONArray.parseArray(listStr, User.class); for (User item : userList){ System.out.println(item.toString()); System.out.println(item.getName()); }
} }
|