一,几个基本概念
①JSONArray 相当于 JAVA中的List<Object>,如:['a','b','c'....]
②JSONObject相当于JAVA中的Map<String, Object>,如:{'1':'a', '2':'b'...}
③对于具有结构层次的JSON格式的数据,可以一层一层地来解析,可参考:这篇文章
二,当待解析的JSON文件很大时,可使用JSON Stream API,比如如下 List类型的数据在 F:\\test.txt 中,假设有上万条时...:
[
{"begin_int":"1677721","end_int":"1677747"},
{"begin_int":"1677747","end_int":"1677823"},
{"begin_int":"1677824","end_int":"1677926"},
{"begin_int":"1677926","end_int":"1678131"},
{"begin_int":"1678131","end_int":"1678540"},
{"begin_int":"1678540","end_int":"1679359"},
{"begin_int":"1690880","end_int":"1690905"},
{"begin_int":"1690905","end_int":"1690931"},
{"begin_int":"1690931","end_int":"1690956"},
{"begin_int":"1690956","end_int":"1690982"}
]
解析代码:将List中的每个元素当作一个Object
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.alibaba.fastjson.JSONReader;
public class ParseListByFastJsonStreamApi {
private static final String FILE_PATH = "F:\\test.txt";
public static void main(String[] args) throws FileNotFoundException{
JSONReader jsonReader = new JSONReader(new FileReader(new File(FILE_PATH)));
jsonReader.startArray();//---> [
while(jsonReader.hasNext())
{
String info = jsonReader.readObject().toString();//---> {"key":"value"}
System.out.println(info);
}
jsonReader.endArray();//---> ]
jsonReader.close();
}
}
或者用如下代码来解析:(将List中的每个元素(如: {"begin_int":"1690956","end_int":"1690982"})再进一步分解 成 Key 和 Value 对)
public static void parse() throws FileNotFoundException{
JSONReader jsonReader = new JSONReader(new FileReader(new File(FILE_PATH)));
jsonReader.startArray();//---> [
while(jsonReader.hasNext())
{
jsonReader.startObject();
while(jsonReader.hasNext()) {
String objKey = jsonReader.readString();
String objVal = jsonReader.readObject().toString();
System.out.println("key: " + objKey + ", value: " + objVal);
}
jsonReader.endObject();
}
jsonReader.endArray();//---> ]
jsonReader.close();
}
上面的第9行 和 第10行解析代码也验证了:“JSONObject相当于JAVA中的Map<String, Object>”。
或者根据 JAVA Bean 类来解析:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.alibaba.fastjson.JSONReader;
public class ParseListByFastJsonStreamApi {
private static final String FILE_PATH = "F:\\test.txt";
public static void main(String[] args) throws FileNotFoundException{
JSONReader jsonReader = new JSONReader(new FileReader(new File(FILE_PATH)));
jsonReader.startArray();//---> [
while(jsonReader.hasNext())
{
BeginEndBean obj = jsonReader.readObject(BeginEndBean.class);//根据 java bean 来解析
int begin_int = obj.getBegin_int();
int end_int = obj.getEnd_int();
System.out.println("begin_int:" + begin_int + ", end_int" + end_int);
}
jsonReader.endArray();//---> ]
jsonReader.close();
}
}
JAVA Bean类如下:
public class BeginEndBean {
private int begin_int;
private int end_int;
public int getBegin_int() {
return begin_int;
}
public void setBegin_int(int begin_int) {
this.begin_int = begin_int;
}
public int getEnd_int() {
return end_int;
}
public void setEnd_int(int end_int) {
this.end_int = end_int;
}
}
三,当需要解析JSON数据格式有点复杂(非扁平的数据)时,比如下面的JSON格式数据:
{"key":"value","anotherKey":[
{"begin_int":"1677721","end_int":"1677747"},
{"begin_int":"1687552","end_int":"1690828"},
{"begin_int":"1690905","end_int":"1690931"},
{"begin_int":"1690931","end_int":"1690956"},
{"begin_int":"1690956","end_int":"1690982"}
],"thirdKey":{"subKey":"subVal","anotherSubKey":["1","2","3"]}}
"key" 对应的就是只有一个值,"anotherKey"对应的是一个列表,"thirdKey"对应的是一个对象(Map)。
解析代码如下:
第17行,将整个Json格式的文件当作一个JSONObject,该JSONObject里面有三个子元素,分别是:"key" 、"anotherKey"、"thirdKey"。因此第18行 while(hasNext())找到每个key,然后 if-else 分别解析对应的值。比如第25行,解析到"anotherKey"时,它对应的是一个List,因此在第26行 startArray() 来读取
由于List中的每个元素其实又是一个个的:{"begin_int":"1687552","end_int":"1690828"}
因此,第29行又开启 startObject() 读取,而每个{"begin_int":"1687552","end_int":"1690828"} 又有两个 ”xxx_int“:"xxx",因此第30行又有一个while(hasNext())循环。
总之,读取Map格式的数据对应的是JSONObject,读取的方法就是 jsonReader.readObject()
读取复杂格式的JSON数据时,解析的规则就像是“剥洋葱”一样,一层一层地来解析相应的对象(Object/List)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.alibaba.fastjson.JSONReader;
public class ParseListByFastJsonStreamApi {
private static final String FILE_PATH = "F:\\test.txt";
public static void main(String[] args) throws FileNotFoundException {
parseData();
}
public static void parseData() throws FileNotFoundException {
JSONReader jsonReader = new JSONReader(new FileReader(new File(FILE_PATH)));
jsonReader.startObject();//将整个json文件当作 Map<String,Object> 对象来解析 {,}
while(jsonReader.hasNext()) {
String key = jsonReader.readString();
if(key.equals("key"))//"key"对应的Object只有一个
{
Object obj = jsonReader.readObject();//
String val = obj.toString();
System.out.println("obj: " + obj + ", value: " + val);
}else if(key.equals("anotherKey")) {//"anotherKey"对应的是一个List对象
jsonReader.startArray();//---> [ 开启读List对象
while(jsonReader.hasNext()) {
jsonReader.startObject();
while(jsonReader.hasNext()) {
String objKey = jsonReader.readString();
String objVal = jsonReader.readObject().toString();
System.out.println("objKey: " + objKey + ", objVal: " + objVal);
}
jsonReader.endObject();
}
jsonReader.endArray();//---> ]
}else if(key.equals("thirdKey")) {
jsonReader.startObject();//{"subKey":"subVal","anotherSubKey":["1","2","3"]}
while(jsonReader.hasNext()) {
String sub_key = jsonReader.readString();
Object third_obj = jsonReader.readObject();
String subVal = third_obj.toString();
System.out.println("sub_key: " + sub_key + ", subVal: " + subVal);
}
jsonReader.endObject();
}
}
jsonReader.endObject();
jsonReader.close();
}
}
也可以借助JAVA Bean 来解析 anotherKey 对应的 List 对象。代码如下:
public class ParseListByFastJsonStreamApi {
private static final String FILE_PATH = "F:\\test.txt";
public static void main(String[] args) throws FileNotFoundException {
parseData();
}
public static void parseData() throws FileNotFoundException {
JSONReader jsonReader = new JSONReader(new FileReader(new File(FILE_PATH)));
jsonReader.startObject();//将整个json文件当作 Map<String,Object> 对象来解析 {,}
while(jsonReader.hasNext()) {
String key = jsonReader.readString();
if(key.equals("key"))//"key"对应的Object只有一个
{
Object obj = jsonReader.readObject();//
String val = obj.toString();
System.out.println("obj: " + obj + ", value: " + val);
}else if(key.equals("anotherKey")) {//"anotherKey"对应的是一个List对象
jsonReader.startArray();//---> [ 开启读List对象
while(jsonReader.hasNext()) {
BeginEndBean objBean = jsonReader.readObject(BeginEndBean.class);
int begin_int = objBean.getBegin_int();
int end_int = objBean.getEnd_int();
System.out.println("begin_int: " + begin_int + ", " + end_int);
}
jsonReader.endArray();//---> ]
}else if(key.equals("thirdKey")) {
jsonReader.startObject();//{"subKey":"subVal","anotherSubKey":["1","2","3"]}
while(jsonReader.hasNext()) {
String sub_key = jsonReader.readString();
Object third_obj = jsonReader.readObject();
String subVal = third_obj.toString();
System.out.println("sub_key: " + sub_key + ", subVal: " + subVal);
}
jsonReader.endObject();
}
}
jsonReader.endObject();
jsonReader.close();
}
}
两种方法的对比如下:
else if(key.equals("anotherKey")) {//"anotherKey"对应的是一个List对象
jsonReader.startArray();//---> [ 开启读List对象
while(jsonReader.hasNext()) {
BeginEndBean objBean = jsonReader.readObject(BeginEndBean.class);
int begin_int = objBean.getBegin_int();
int end_int = objBean.getEnd_int();
System.out.println("begin_int: " + begin_int + ", " + end_int);
}
jsonReader.endArray();//---> ]
}
---------------------------------------------------------------------------
else if(key.equals("anotherKey")) {//"anotherKey"对应的是一个List对象
jsonReader.startArray();//---> [ 开启读List对象
while(jsonReader.hasNext()) {
jsonReader.startObject();
while(jsonReader.hasNext()) {
String objKey = jsonReader.readString();
String objVal = jsonReader.readObject().toString();
System.out.println("objKey: " + objKey + ", objVal: " + objVal);
}
jsonReader.endObject();
}
jsonReader.endArray();//---> ]
}