gson操作json格式字符串
- 测试数据(test.json)
- 存储数据的类(Message.java和User.java)
- 使用JsonReader类读取json
- 使用Gson读写json
测试数据(test.json)
[
{
"id": 91234,
"text": "How do I read a JSON stream in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
},
{
"id": 91235,
"text": "@json_newb just use JsonReader!",
"geo": [50.454722, -104.606667],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]
存储数据的类(Message.java和User.java)
package json;
import java.util.List;
public class Message {
private int id;
private String text;
private List<Double> geo;
private User user;
public Message(){}
public Message(int id, String text, List<Double> geo,User user) {
this.id = id;
this.text = text;
this.geo = geo;
this.user=user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<Double> getGeo() {
return geo;
}
public void setGeo(List<Double> geo) {
this.geo = geo;
}
@Override
public String toString() {
return "Message{" +
"id=" + id +
", text='" + text + '\'' +
", geo=" + geo +
", user=" + user +
'}';
}
}
package json;
public class User {
private String name;
private int followers_count;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", 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;
}
}
使用JsonReader类读取json
package json;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JsonReaderTest {
public static void main(String[] args)throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("json/test.json");
JsonReader jsonReader = new JsonReader(new InputStreamReader(in));
jsonReader.beginArray();
List<Message> list=new ArrayList<>();
while (jsonReader.hasNext()){
Message msg=getMessage(jsonReader);
list.add(msg);
}
jsonReader.endArray();
System.out.println(Arrays.toString(list.toArray()));
}
public static Message getMessage(JsonReader jsonReader)throws IOException{
Message msg=new Message();
jsonReader.beginObject();
while (jsonReader.hasNext()){
String name = jsonReader.nextName();
if(name.equals("id")){
msg.setId(Integer.parseInt(jsonReader.nextString()));
}else if(name.equals("text")){
msg.setText(jsonReader.nextString());
}else if(name.equals("geo")&&jsonReader.peek()!= JsonToken.NULL){
msg.setGeo(getGeoList(jsonReader));
}else if(name.equals("user")){
msg.setUser(getUser(jsonReader));
}else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
return msg;
}
public static List<Double> getGeoList(JsonReader jsonReader)throws IOException{
jsonReader.beginArray();
List<Double> list=new ArrayList<>();
while (jsonReader.hasNext()){
list.add(jsonReader.nextDouble());
}
jsonReader.endArray();
return list;
}
public static User getUser(JsonReader jsonReader)throws IOException{
User user = new User();
jsonReader.beginObject();
while (jsonReader.hasNext()){
String name = jsonReader.nextName();
if (name.equals("name")){
user.setName(jsonReader.nextString());
}else if(name.equals("followers_count")){
user.setFollowers_count(jsonReader.nextInt());
}else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
return user;
}
}
"C:\Program Files\Java\jdk-9.0.4\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=4735:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\test\bin;D:\java\test\libs\guava-27.0-jre.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\junit.jar;D:\java\test\libs\apache-mina-2.0.20\dist\mina-core-2.0.20.jar;D:\java\test\libs\apache-mina-2.0.20\lib\slf4j-api-1.7.26.jar;D:\java\test\libs\jdom-2.0.6.jar;D:\java\test\libs\dom4j-2.0.2.jar;D:\java\test\libs\xstream-1.4.11.1.jar;D:\java\test\libs\xpp3-1.1.3.4.C.jar;D:\java\test\libs\gson-2.8.4.jar" json.JsonReaderTest
[Message{id=91234, text='How do I read a JSON stream in Java?', geo=null, user=User{name='json_newb', followers_count=41}}, Message{id=91235, text='@json_newb just use JsonReader!', geo=[50.454722, -104.606667], user=User{name='jesse', followers_count=2}}]
使用Gson读写json
package json;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.*;
import java.util.List;
public class GsonTest {
public static void main(String[] args) throws FileNotFoundException,IOException {
Gson gson = new Gson();
JsonArray jsonArray = new JsonArray();
JsonObject obj = new JsonObject();
obj.addProperty("id",2222);
obj.addProperty("text","hello");
JsonArray geoArray=new JsonArray();
geoArray.add(234);
geoArray.add(567);
obj.add("geo",geoArray);
JsonObject obj1 = new JsonObject();
obj1.addProperty("id",3333);
obj1.addProperty("text","world");
JsonArray geoArray1=new JsonArray();
geoArray1.add(234);
geoArray1.add(567);
obj.add("geo",geoArray1);
jsonArray.add(obj);
jsonArray.add(obj1);
FileWriter out=new FileWriter("gson_test.json");
out.write(jsonArray.toString());
out.flush();
FileReader in = new FileReader("gson_test.json");
TypeToken<List<Message>> typeToken=new TypeToken<>(){};
List<Message> list=gson.fromJson(in, typeToken.getType());
for (Message msg:list){
System.out.println(msg);
}
}
}
"C:\Program Files\Java\jdk-9.0.4\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=4832:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\test\bin;D:\java\test\libs\guava-27.0-jre.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\junit.jar;D:\java\test\libs\apache-mina-2.0.20\dist\mina-core-2.0.20.jar;D:\java\test\libs\apache-mina-2.0.20\lib\slf4j-api-1.7.26.jar;D:\java\test\libs\jdom-2.0.6.jar;D:\java\test\libs\dom4j-2.0.2.jar;D:\java\test\libs\xstream-1.4.11.1.jar;D:\java\test\libs\xpp3-1.1.3.4.C.jar;D:\java\test\libs\gson-2.8.4.jar" json.GsonTest
Message{id=2222, text='hello', geo=[234.0, 567.0], user=null}
Message{id=3333, text='world', geo=null, user=null}