最近项目里搞很复杂的json数据,借此机会整理了一下fastJSON操作各种格式的json数据;举一反三,学会以下几种JSON数据类型的操作,其他再复杂的json数据也能轻松解决;
如果是maven项目,首先引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
1 最简单得到Map格式的json数据
{
"name": "李四",
"age": 20 ,
"sex": "男"
}
测试
package com.maltose.util;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
* @author sgw
* @DATE 20200625
*/
public class FastJsonTest {
public static void main(String[] args) {
String jsonData = "{\n" + "\t\"name\": \"李四\",\n" + "\t\"age\": 20 ,\n" + "\t\"sex\": \"男\"\n" + "}";
Map map = (Map) JSONObject.parse(jsonData);
System.out.println(map.get("name") + "," + map.get("age") + "," + map.get("sex"));
}
}
结果
2 json里嵌套json(不含数组)
即Map里嵌套Map
{
"user1": {
"username": "张三",
"age": 21
},
"user2": {
"username": "Tom",
"age": 39
},
"user3": {
"username": "李四",
"age": 43
}
}
测试
package com.maltose.util;
import com.alibaba.fastjson.JSONObject;
import java.util.Iterator;
import java.util.Set;
/**
* @author sgw
* @DATE 20200625
*/
public class FastJsonTest {
public static void main(String[] args) {
String jsonData = "{\n" +
"\t\"user1\": {\n" +
"\t\t\"username\": \"张三\",\n" +
"\t\t\"age\": 21\n" +
"\t},\n" +
"\t\"user2\": {\n" +
"\t\t\"username\": \"Tom\",\n" +
"\t\t\"age\": 39\n" +
"\t},\n" +
"\t\"user3\": {\n" +
"\t\t\"username\": \"李四\",\n" +
"\t\t\"age\": 43\n" +
"\t}\n" +
"}";
//先将这条数据解析为JSONObject
JSONObject jsonObject = JSONObject.parseObject(jsonData);
//我们需要得到user,才能得到它对应的内部json
Set<String> jsonSet = jsonObject.keySet();
Iterator<String> iterator = jsonSet.iterator();
while (iterator.hasNext()){
//通过迭代器可以取到外部json的key
String json = iterator.next();
//取得内部json字符串
String string = jsonObject.getString(json);
//将内部json字符串解析为object对象
JSONObject inJson = JSONObject.parseObject(string);
//通过score,得到value值
String name = inJson.getString("username");
String age = inJson.getString("age");
System.out.println(name+":"+age);
}
}
}
结果
3 数组格式的json数据
[
{
"studentName": "lily",
"studentAge": 12
},
{
"studentName": "lucy ",
"studentAge": 15
}
]
测试
package com.maltose.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.util.Map;
/**
* @author sgw
* @DATE 20200625
*/
public class FastJsonTest {
public static void main(String[] args) {
String arryJsonData="[\n" +
" {\n" +
"\t\t\"studentName\": \"lily\",\n" +
"\t\t\"studentAge\": 12\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"studentName\": \"lucy \",\n" +
"\t\t\"studentAge\": 15\n" +
"\t}\n" +
"]";
//需要使用的JSON的parseArray方法,将arryJsonData解析为object类型的数组
JSONArray jsonArray = JSON.parseArray(arryJsonData);
for (int i = 0; i < jsonArray.size(); i++) {
Map map =(Map) jsonArray.get(i);
System.out.println("name:"+map.get("studentName")+" age:"+map.get("studentAge"));
}
}
}
结果
4 json格式里嵌套数组
{
"students": [{
"studentName": "lily",
"studentAge": 12
}, {
"studentName": "lucy",
"studentAge": 15
}],
"class": "02",
"school": "希望小学"
}
测试
package com.maltose.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
* @author sgw
* @DATE 20200625
*/
public class FastJsonTest {
public static void main(String[] args) {
String jsonData = "{\n" +
"\t\"students\": [{\n" +
"\t\t\"studentName\": \"lily\",\n" +
"\t\t\"studentAge\": 12\n" +
"\t}, {\n" +
"\t\t\"studentName\": \"lucy\",\n" +
"\t\t\"studentAge\": 15\n" +
"\t}],\n" +
"\t\"class\": \"02\",\n" +
"\t\"school\": \"希望小学\"\n" +
"}";
Object jsonObject = JSONObject.parse(jsonData);
Map map = (Map) JSONObject.parse(jsonData);
//ArrayList students=(ArrayList)map.get("students");
System.out.println("学校:" + map.get("school"));
System.out.println("班级:" + map.get("class"));
//将students属性值转换为json格式
String students = JSONObject.toJSONString(map.get("students"));
//转换为json数组
JSONArray jsonArray = JSON.parseArray(students);
for (int i = 0; i < jsonArray.size(); i++) {
Map student = (Map) jsonArray.get(i);
System.out.println("name:" + student.get("studentName") + " age:" + student.get("studentAge"));
}
}
}
结果