共计 4794 个字符,预计需要花费 12 分钟才能阅读完成。
目录
一、Json 的解析
1、JSONObject 数据
1 格式
2 构造方式
3 常用方法
2、JSONOArray 数据
1 格式
2 构造方式
3 常用方法
二、Json 格式字符串
1、json 字符串转 java 对象、list、map
一、Json 的解析
1、JSONObject 数据
JSONObject 继承 JSON,JSON 中的方法在 JSONObject 中也存在,不用疑惑使用哪一个。
1 格式
{“key1″:value,”key2”,value,…},最外层必须为{},不能为[]
key 必须是 string,value 可以是 string,也可以是其他任意数据结构。
2 构造方式
1. 创建空 JSONObject,通过 put()添加数据
import com.alibaba.fastjson.JSONObject;
public class demo {public static void main(String[] args) {JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name","花");
jsonObject1.put("age","23");
System.out.println("jsonObject1=" + jsonObject1);
System.out.println("jsonObject1-name=" + jsonObject1.get("name"));
}
}
2. 通过 map 生成 JSONObject
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
public class demo {public static void main(String[] args) {
// 通过 map 生成 JSONObject
HashMap map = new HashMap();
map.put("name", "雨");
map.put("age", 23);
JSONObject jsonObject1 = new JSONObject(map);
System.out.println("jsonObject2-name=" + jsonObject1.get("name"));
}
}
3. 通过实体生成
import com.alibaba.fastjson.JSONObject;
public class demo {public static void main(String[] args) {TestClass testClass = new TestClass();
testClass.setAge("23");
testClass.setName("竹");
// 生成 json 格式
JSONObject json1 = (JSONObject) JSON.toJSON(testClass);
JSONObject json2 = (JSONObject) JSONObject.toJSON(testClass);
System.out.println(json1.get("name")+"----------"+ json2.get("name"));
}
}
4. 由 json 格式字符串生成(字符串格式不符合标准会报错,例如 json 数组格式字符串)
import com.alibaba.fastjson.JSONObject;
public class demo {public static void main(String[] args) {String testString = "{"age":23,"name":" 石 "}";
// 如果字符串格式不符合标准,会报错
JSONObject jsonObject3 = JSONObject.parseObject(testString);
System.out.println(jsonObject3);
}
}
3 常用方法
方法 | 传参 | 返回数据类型 | 用途 |
put(String key, Object value) |
key,value | boolean | 增加属性 |
toString() | String | 转为字符串 | |
getJSONArray(String key) | key | JSONArray | 获取 JSONArray 数组 |
JSONObject getJSONObject(String key) | key | JSONObject | 获取 JSONObject 数组 |
isEmpty(String key) | key | boolean | 判断属性存在且不为空 |
JSONObject.parseObject(String str) | json 格式字符串 | JSONObject | 字符串转 JSONObject(字符串格式不对会报错,可以用来 判断字符串是否符合 json 格式) |
JSONObject.parseArray(String str) | json 列表格式字符串 | JSONArray | 字符串转 JSONArray(字符串格式不对会报错,可以用来 判断字符串是否符合 json 列表格式) |
JSONObject.parseArray(String str, User.class) | json 列表格式字符串、java 类 | Java 实体类 | 字符串转 java 对象 |
JSONObject.toJSONString() | list、map 或实体类 | String | 将 map,list 数据转为 json 格式字符串 |
JSONObject.parse() | Json 格式或 Json 数组格式字符串 | Object | 将 Json 格式或 Json 数组格式字符串转化为 Object,可以用来判断字符串格式是否为 json 或 json 数组 |
JSONObject.toJavaObject(jsonOBj,User.class); |
JSON 对象和 Java 类 | Java 实体类 | 以 json 字符串生成 java 对象 |
2、JSONOArray 数据
1 格式
[{“key”:value},{“key”:value},{“key”:value},…]
本质是数组
2 构造方式
1. 创建空 JSONArray,通过 add()添加数据
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class demo {public static void main(String[] args) {JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name", "花");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "雨");
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
System.out.println(jsonArray);
}
}
2. 对象列表转为 JSONArray
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
public class demo {public static void main(String[] args) {ArrayList testList = new ArrayList();
TestClass testClass1 = new TestClass();
testClass1.setAge("20");
testClass1.setName("花");
testList.add(testClass1);
TestClass testClass2 = new TestClass();
testClass2.setAge("20");
testClass2.setName("竹");
testList.add(testClass2);
//list 转 json 字符串
String string = JSONObject.toJSON(testList).toString();
System.out.println(string);
//json 字符串转 listJson 格式
JSONArray jsonArray = JSONObject.parseArray(string);
System.out.println(jsonArray);
}
}
3.JSONArray 格式字符串转为 JSONArray
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class demo {public static void main(String[] args) {String string = "[{"name":" 花 "},{"name":" 雨 "}]";
JSONArray jsonArray = JSONObject.parseArray(string);
System.out.println(jsonArray);
}
}
3 常用方法
传参 | 返回数据类型 | 用途 | |
add() addAll() |
Object、集合 | boolean | 添加数据 |
JSONArray.parseObject(String str) | json 格式字符串 | JSONObject | 字符串转 JSONObject(字符串格式不对会报错,可以用来 判断字符串是否符合 json 格式) |
JSONArray.parseArray(String str) | json 列表格式字符串 | JSONArray | 字符串转 JSONArray(字符串格式不对会报错,可以用来 判断字符串是否符合 json 列表格式) |
JSONArray.parse() | Json 格式或 Json 数组格式字符串 | Object | 将 Json 格式或 Json 数组格式字符串转化为 Object,可以用来判断字符串格式是否为 json 或 json 数组 |
JSONArray.toJavaObject(jsonArray,UserList.class); |
json 字符串和 Java 列表类 | Java 列表对象 | 以 json 字符串生成 java 对象 |
二、Json 格式字符串
1、json 字符串转 java 对象、list、map
1.json 字符串转 java 对象
User user = JSONObject.parseObject(jsonStr, User.class);
2.json 字符串转 list 对象
List testClasses = JSONObject.parseArray(jsonStr, TestClass.class);
3.json 字符串转 map 对象
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
public class demo {public static void main(String[] args) {String str = "{"name":" 雨 ","age":"20"}";
// 第一种方式
Map maps = (Map) JSON.parse(str);
// 第二种方式
Map mapTypes = JSON.parseObject(str);
// 第三种方式
Map mapType = JSON.parseObject(str,Map.class);
// 第四种方式
Map json = (Map) JSONObject.parse(str);
// 第种五方式
Map mapObj = JSONObject.parseObject(str,Map.class);
System.out.println(json);
}
}
原文地址: Java 中 Json 与 Json 格式字符串的解析、判断和转换