我有一个JSON字符串,如下所示。这来自我在Android应用程序中使用的网站(下面的URL输出到一个页面)。在一个对象中反序列化与Gson的JSON
{"posts": [{"id":"0000001","longitude":"50.722","latitude":"-1.87817","position":"Someplace 1","altitude":"36","description":"Some place 1 "},{"id":"0000002","longitude":"50.722","latitude":"-1.87817","position":"Some PLace 2","altitude":"36","description":"Some place 2 description"}]}
我想反序列化到一个列表,我可以通过他们以后的应用迭代这一点。我该怎么做呢?我创建了一个类,其属性和方法以及List类如下,然后使用fromJson
对其进行反序列化,但它返回NULL。希望这个问题很清楚,并提前感谢。
ListClass
包数据访问;
import java.util.List;
public class LocationList {
public static List<Location> listLocations;
public void setLocationList(List <Location> listLocations) {
LocationList.listLocations = listLocations;
}
public List<Location> getLocationList() {
return listLocations;
}
}
GSON
public LocationList[] getJsonFromGson(String jsonURL) throws IOException{
URL url = new URL(jsonURL);
String content = IOUtils.toString(new InputStreamReader(url.openStream()));
LocationList[] locations = new Gson().fromJson(content, LocationList[].class);
return locations;
}
感谢回答。 Type和TypeToken的命名空间是什么? – Chin
java.lang.Type java.lang.Type和com.google.gson.reflect.TypeToken – Yogu
eclipse flags java.lang.Type as invalid。但是,import java.lang.reflect.Type;很好。那是你的意思? – Chin