Java 如何使用GSON获取解析数据
所以我遵循这个,它有这个方法Java 如何使用GSON获取解析数据,java,android,arrays,json,gson,Java,Android,Arrays,Json,Gson,所以我遵循这个,它有这个方法 new AsyncTask<Void,Void,Void>(){ @Override protected Void doInBackground(Void... voids) { Reader reader=API.getData("http://beta.json-generator.com/api/json/get/DiIRBM4"); Type listType
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... voids) {
Reader reader=API.getData("http://beta.json-generator.com/api/json/get/DiIRBM4");
Type listType = new TypeToken<ArrayList<DoctorBean>>(){}.getType();
beanPostArrayList = new GsonBuilder().create().fromJson(reader, listType);
postList=new StringBuffer();
for(DoctorBean post: beanPostArrayList){
postList.append("\n heroName: "+post.getHeroName()+"\n realName: "+post.getRealName()+"\n\n");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.d("JSON Result ", postList.toString());
}
}.execute();
这是我的数据模型
import com.google.gson.annotations.SerializedName;
public class DoctorBean {
@SerializedName("heroName")
private String heroName;
@SerializedName("realName")
private String realName;
public DoctorBean(String heroName, String realName) {
this.heroName = heroName;
this.realName = realName;
}
public String getHeroName() {
return heroName;
}
public void setHeroName(String heroName) {
this.heroName = heroName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
}
这是我的API类
public class API {
private static Reader reader=null;
public static Reader getData(String SERVER_URL) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVER_URL);
HttpResponse response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
reader = new InputStreamReader(content);
} else {
// Log.e("error:", "Server responded with status code: "+ statusLine.getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return reader;
}
}
我注意到日志结果显示了3行,因此我认为它能够正确获得数组的长度。但是对于数据,所有数据都是空的。试试这个。
从响应创建数组:
DoctorBean[] doctorBeanArray = new Gson().fromJson(response, DoctorBean[].class); // Where response is your string response
然后创建一个ArrayList:
ArrayList<DoctorBean> doctorBeanList = new ArrayList<DoctorBean>(Arrays.asList(doctorBeanArray));
ArrayList doctorBeanList=新的ArrayList(Arrays.asList(doctorBeanArray));
试试这个。
从响应创建数组:
DoctorBean[] doctorBeanArray = new Gson().fromJson(response, DoctorBean[].class); // Where response is your string response
然后创建一个ArrayList:
ArrayList<DoctorBean> doctorBeanList = new ArrayList<DoctorBean>(Arrays.asList(doctorBeanArray));
ArrayList doctorBeanList=新的ArrayList(Arrays.asList(doctorBeanArray));
根据您的给定,来自链接的响应如下:
[
{
"date":"11/8/2014",
"auther":"nirav kalola",
"description":"json object parsing using gson library is easy",
"post_name":"json object parsing"
},
{
"date":"12/8/2014",
"auther":"nirav kalola",
"description":"json array parsing using gson library",
"post_name":"json array parsing"
},
{
"date":"17/8/2014",
"auther":"nirav kalola",
"description":"store json file in assets folder and get data when required",
"post_name":"json parsing from assets folder"
}
]
因此,您需要在GSONBuilder的POJO类下面进行尝试。将您的姓名替换为BeanPost
import com.google.gson.annotations.SerializedName;
public class BeanPost {
@SerializedName("post_name")
private String post_name;
@SerializedName("auther")
private String auther;
@SerializedName("date")
private String date;
@SerializedName("description")
private String description;
public BeanPost(String post_name, String auther, String date, String description) {
this.post_name = post_name;
this.auther = auther;
this.date = date;
this.description = description;
}
public String getPost_name() {
return post_name;
}
public void setPost_name(String post_name) {
this.post_name = post_name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
尝试按照pojo类arraylist上面的方法创建BeanPStarrayList。然后尝试您的代码并从中获取适当的字段
我希望这对你有帮助 根据您的给定,来自链接的响应如下:
[
{
"date":"11/8/2014",
"auther":"nirav kalola",
"description":"json object parsing using gson library is easy",
"post_name":"json object parsing"
},
{
"date":"12/8/2014",
"auther":"nirav kalola",
"description":"json array parsing using gson library",
"post_name":"json array parsing"
},
{
"date":"17/8/2014",
"auther":"nirav kalola",
"description":"store json file in assets folder and get data when required",
"post_name":"json parsing from assets folder"
}
]
因此,您需要在GSONBuilder的POJO类下面进行尝试。将您的姓名替换为BeanPost
import com.google.gson.annotations.SerializedName;
public class BeanPost {
@SerializedName("post_name")
private String post_name;
@SerializedName("auther")
private String auther;
@SerializedName("date")
private String date;
@SerializedName("description")
private String description;
public BeanPost(String post_name, String auther, String date, String description) {
this.post_name = post_name;
this.auther = auther;
this.date = date;
this.description = description;
}
public String getPost_name() {
return post_name;
}
public void setPost_name(String post_name) {
this.post_name = post_name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
尝试按照pojo类arraylist上面的方法创建BeanPStarrayList。然后尝试您的代码并从中获取适当的字段
我希望这对你有帮助 您试图获取json数据的链接将不同的json作为compair提供给您的json。因此,您需要根据真实的json响应创建POJO类。您试图获取json数据的链接是将不同的json作为compair提供给您的json。因此,您需要根据真实的json响应创建POJO类。