GSON示例
时间:2020-02-23 14:41:23 来源:igfitidea点击:
在本教程中,我们将看到我们如何使用GSON读取和编写JSON。
使用JSON-Simple阅读和编写JSON。
我们将使用另一种方式(即,GSON)阅读JSON。
GSON是一个Java库,可用于将Java对象转换为JSON表示。
它也可用于将JSON字符串转换为等效的Java对象。
GSON可以使用任意Java对象,包括我们没有源代码的预先存在的对象。
使用GSON有各种目标或者优势(取自GSON摘机页面)
- 提供简单的tojson()和fromjson()方法,将Java对象转换为JSON,反之亦然
- 允许预先现有的未经修复对象转换为json和json
- 大量支持Java泛型
- 允许对象的自定义表示
- 支持任意复杂的对象(具有深度继承层次结构和广泛使用通用类型)
使用GSON阅读和编写JSON非常简单。
我们可以使用这两种方法:tojson():它会将简单的pojo对象转换为JSON字符串。
fromjson():它将将JSON字符串转换为POJO对象。
在本教程中,我们将使用GSON读写JSON。
从这里下载Gson-2.2.4罐。
创建名为"gsonexample"的Java项目创建一个文件夹jar和粘贴下载的jar gson-2.2.4.jar。
右键单击项目 - >属性 - > java构建路径 - >添加jar然后转到src-> jars-> Gson-2.2.4.jar。
单击确定。
然后,我们将看到JSON-Simple-1.1 JAR添加到库中,然后单击确定
首先创建一个名为"country.java"的pojo
package org.igi.theitroad.pojo;
import java.util.ArrayList;
import java.util.List;
public class Country {
String name;
int population;
private List listOfStates;
//getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public List getListOfStates() {
return listOfStates;
}
public void setListOfStates(List listOfStates) {
this.listOfStates = listOfStates;
}
}
将JSON写入文件:
在src-> org.igi.theitroad中创建名为"gsonwritingtofileexample.java"的新类
package org.igi.theitroad;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.igi.theitroad.pojo.Country;
import com.google.gson.Gson;
/*
* @Author : igi Mandliya
*/
public class GSONWritingToFileExample {
public static void main(String[] args) {
Country countryObj=new Country();
countryObj.setName("Netherlands");
countryObj.setPopulation(1000000);
List listOfStates=new ArrayList();
listOfStates.add("Madhya Pradesh");
listOfStates.add("Maharastra");
listOfStates.add("Rajasthan");
countryObj.setListOfStates(listOfStates);
Gson gson = new Gson();
//convert java object to JSON format,
//and returned as JSON formatted string
String json = gson.toJson(countryObj);
try {
//write converted json data to a file named "CountryGSON.json"
FileWriter writer = new FileWriter("E:CountryGSON.json");
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(json);
}
}
运行上面的程序和文件的内容CountryGson.json将如下:
{"name":"Netherlands","population":1000000,"listOfStates":["Madhya Pradesh","Maharastra","Rajasthan"]}
阅读JSON到文件:
其中我们将阅读以上创建的JSON文件。
在src-> org.igi.onitoad中创建一个名为"jsonsimplereadingfromfilexample.java"的新类
package org.igi.theitroad;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.igi.theitroad.pojo.Country;
import com.google.gson.Gson;
/*
* @Author : igi Mandliya
*/
public class GSONReadingFromFileExample {
public static void main(String[] args) {
Gson gson = new Gson();
try {
System.out.println("Reading JSON from a file");
System.out.println("----------------------------");
BufferedReader br = new BufferedReader(
new FileReader("E:file.json"));
//convert the json string back to object
Country countryObj = gson.fromJson(br, Country.class);
System.out.println("Name Of Country: "+countryObj.getName());
System.out.println("Population: "+countryObj.getPopulation());
System.out.println("States are :");
List listOfStates = countryObj.getListOfStates();
for (int i = 0; i < listOfStates.size(); i++) {
System.out.println(listOfStates.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行上面的程序,我们将获取以下输出:
Reading JSON from a file --------------------------- Name Of Country: Netherlands Population: 1000000 States are : Madhya Pradesh Maharastra Rajasthan

