gpt4 book ai didi

java - 如何使用GSON获取解析后的数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:19 27 4
gpt4 key购买 nike

所以我正在关注这个tutorial并且它有这个方法。

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();

日志结果只会显示这些值。

JSON 结果:

heroName: null realName: null

heroName: null realName: null

heroName: null realName: null

这是我的 JSON 数据

[
{
"heroName": "Dr. Strange",
"realName": "Stephen Strange"
},
{
"heroName": "Spider-Man",
"realName": "Peter Paker"
},
{
"heroName": "Captain America",
"realName": "Stever Rogers"
}
]

这是我的数据模型

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 行,所以我认为它能够正确获取数组的长度。但就数据而言,一切都是空的。

最佳答案

根据您给出的tutorial ,来自this链接响应如下:

    [
{
"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类数组列表的beanPostArrayList。并尝试您的代码并从中获取适当的字段。

希望对您有帮助。

关于java - 如何使用GSON获取解析后的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37920747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com