Gson fromjson示例
时间:2020-02-23 14:41:23 来源:igfitidea点击:
在本教程中,我们将看到关于Gson fromjson example.we已在上一篇文章中看到Simple Gson示例。
Gson fromjson方法用于将JSON字符串或者JSONObject或者读取器转换为相应对象。
我们可以通过GSON页面读取关于FROMJSON方法的各种变体。
下面示例演示Gson fromjson方法。
读取simple JSON字符串
我们可以使用FROMJSON(String JSON,Classoft)方法来读取简单的JSON字符串。
让我们说你有下面的国家程序。
package org.igi.theitroad; public class Country { int id; String countryName; long population; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public String toString() { return "Country [id=" + id + ", countryName=" + countryName + ", population=" + population + "]"; } }
创建一个名为"simplegsonmain"的类
package org.igi.theitroad; import com.google.gson.Gson; public class SimpleGsonMain { public static void main(String[] args) { String jsonString="{'id'=1,countryNasme='Netherlands',population=20000}"; Gson gson=new Gson(); Country country=gson.fromJson(jsonString, Country.class); System.out.println(country.toString()); } }
运行上面的程序时,我们将得到以下输出。
Country [id=1, countryName=null, population=20000]
阅读JSON数组到列表
假设我们需要从文件中读取下面的JSON字符串。
[ { "id":1, "countryName":"Netherlands", "population":20000 }, { "id":2, "countryName":"Nepal", "population":12000 }, { "id":3, "countryName":"Bhutan", "population":6000 }, { "id":4, "countryName":"China", "population":30000 } ]
我们需要将以上json数组转换为列表<country>我们将无法使用以下代码进行。
List<Country> listOfCountries=gson.fromJson(jsonString, ArrayList.class);
上面的代码不起作用。
如果对象是参数化类型类型,则需要使用类型来转换它。
因此,我们可以使用FROMJSON(REVERER JSON,Typeoft)读取以上JSON文件。
package org.igi.theitroad; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonReadFileToListMain { public static void main(String[] args) { Reader reader; try { reader = new FileReader("/Users/apple/Desktop/Countries.json"); Gson gson=new Gson(); //Usinf fromJson(reader,type) List<Country> countryList=gson.fromJson(reader,new TypeToken<List<Country>>(){}.getType()); System.out.println("======================="); System.out.println("List of Countries are:"); System.out.println("======================="); for (Country country : countryList) { System.out.println(country); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
运行上面的程序时,我们将得到以下输出:
======================= List of Countries are: ======================= Country [id=1, countryName=Netherlands, population=20000] Country [id=2, countryName=Nepal, population=12000] Country [id=3, countryName=Bhutan, population=6000] Country [id=4, countryName=China, population=30000]
阅读JSON字符串并将其转换为映射
让我们说我们有平普通的GSON字符串,我们希望将它放在地图中。
我们可以简单地使用FROMJSON(String JSON,Typeoft)并将字符串转换为如下映射:
package org.igi.theitroad; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonFromJsonToMapMain { public static void main(String args[]) { Gson gson=new Gson(); String counrtyCapitalJson="{'Netherlands':'Delhi','Nepal':'Kathmandu','China':'Beijing','Bhutan':'Thimphu'}"; Map<String,String> map=gson.fromJson(counrtyCapitalJson,new TypeToken<Map<String,String>>(){}.getType()); for(String key:map.keySet()) { System.out.println(map.get(key)+" is capital of "+key); } } }
运行上面的程序时,我们将得到以下输出:
Delhi is capital of Netherlands Kathmandu is capital of Nepal Beijing is capital of China Thimphu is capital of Bhutan