message.json
[
{
"id": 912345678901,
"text": "How do I read a JSON stream in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
},
{
"id": 912345678902,
"text": "@json_newb just use JsonReader!",
"geo": [50.454722, -104.606667],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]
User.java
package com.vince.JSONMessage;
public class User {
private String name;
private int followers_count;
public User() {
}
public User(String name, int followers_count) {
this.name = name;
this.followers_count = followers_count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFollowers_count() {
return followers_count;
}
public void setFollowers_count(int followers_count) {
this.followers_count = followers_count;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", followers_count=" + followers_count +
'}';
}
}
Message.java
package com.vince.JSONMessage;
import java.util.ArrayList;
public class Message {
private long id;
private String text;
private ArrayList<Double> geo;
private User user;
public Message() {
}
public Message(long id, String text, ArrayList<Double> geo, User user) {
this.id = id;
this.text = text;
this.geo = geo;
this.user = user;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public ArrayList<Double> getGeo() {
return geo;
}
public void setGeo(ArrayList<Double> geo) {
this.geo = geo;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Message{" +
"id=" + id +
", text='" + text + '\'' +
", geo=" + geo +
", user=" + user +
'}';
}
}
JSONDemo.java
package com.vince.JSONMessage;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class JSONDemo {
@Test
public void jsonParse(){
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/vince/JSONMessage/message.json");
InputStreamReader in = new InputStreamReader(is);
JsonReader reader = new JsonReader(in);
//调用解析数组的方法,即从这里就真正开始解析
ArrayList<Message> m =readArray(reader);
for(Message m1:m){
System.out.println(m1);
}
}
//解析JSON最外层的数组
//为什么要一开始先解析数组?
//因为该json文件的最外层是[],当被JsonReader(in)读取后就判定为最外层的是数组,所以得一开始先解析数组beginAray
private ArrayList<Message> readArray(JsonReader reader) {
ArrayList<Message> list = new ArrayList();
try {
reader.beginArray();
while(reader.hasNext()){
list.add(readObject(reader));
}
reader.endArray();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//解析JSON最外层数组里的message对象
private Message readObject(JsonReader reader) {
Message message = new Message();
try {
reader.beginObject();
while(reader.hasNext()){
String name = reader.nextName();
if("id".equals(name)){
message.setId(reader.nextLong());
}else if("text".equals(name)){
message.setText(reader.nextString());
//reader.peek()表示:只是去获取看看数据,不是真正的读取数据;
// JsonToken.NUL表示:判断这个JSON的值是不是为空的
}else if("geo".equals(name) && reader.peek()!= JsonToken.NULL){
message.setGeo(readGeo(reader));
}else if("user".equals(name)){
message.setUser(readUser(reader));
}else {
//reader.skipValue()表示当上述循环的条件不满足时,就直接跳过进行下一轮循环{比如这里解析geo为空的时候,如果不加这个跳过,程序就会卡在那里}
reader.skipValue();
}
}
reader.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return message;
}
//解析JSON数组里每个Message对象里的geo数组
//解析每个{}json对象里的子对象数组
private ArrayList<Double> readGeo(JsonReader reader) {
ArrayList<Double> list1 = new ArrayList<>();
try {
reader.beginArray();
while(reader.hasNext()){
double v = reader.nextDouble();
list1.add(v);
}
reader.endArray();
} catch (IOException e) {
e.printStackTrace();
}
return list1;
}
//解析JSON数组里每个Message对象里的user对象
//解析每个{}json对象里的子对象User对象
private User readUser(JsonReader reader) {
User user = new User();
try {
reader.beginObject();
while(reader.hasNext()){
String name = reader.nextName();
if("name".equals(name)){
user.setName(reader.nextString());
}else if("followers_count".equals(name)){
user.setFollowers_count(reader.nextInt());
}else{
reader.skipValue();
}
}
reader.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return user;
}
}
总结:
1、为什么要一开始先解析数组?
因为该json文件的最外层是[],当被JsonReader(in)读取后就判定为最外层的是数组,所以得一开始先解析数组beginAray
2、如果该json文件的最外层是{},当被JsonReader(in)读取后就判定为最外层的是对象,所以得一开始先解析对象beginObject