gpt4 book ai didi

java - 使用 Gson 解析列表对象 json

转载 作者:行者123 更新时间:2023-11-29 05:26:15 26 4
gpt4 key购买 nike

我正在尝试使用谷歌 Gson 库解析 Json,但目前我无法让它工作......

这是我应该得到的 Json:

{
"shoppingLists": [
{
"ShoppingList": {
"id": "51",
"name": "loool",
"created": "2014-03-22 13:03:22",
"modified": "2014-03-22 13:03:22"
},
"ShoppingItem": [
{
"id": "24",
"shopping_item_category_id": "19",
"name": "Biscuits",
"description": "",
"created": "2014-02-05 17:43:45",
"modified": "2014-02-05 17:43:45",
"category_name": "Confiseries \/ Gouters"
},
{
"id": "25",
"shopping_item_category_id": "19",
"name": "Snickers",
"description": "",
"created": "2014-02-05 17:44:08",
"modified": "2014-02-05 17:44:08",
"category_name": "Confiseries \/ Gouters"
},
{
"id": "26",
"shopping_item_category_id": "19",
"name": "C\u00e9reales",
"description": "",
"created": "2014-02-05 17:44:57",
"modified": "2014-02-05 17:44:57",
"category_name": "Confiseries \/ Gouters"
}
]
}
]
}

这是我的模型:

购物 list :

public class shoppingLists {
public ShoppingList ShoppingList;
public List<ShoppingItem> ShoppingItems;
}

购物 list :

public class ShoppingList {
public int id;
public String name;
public String created;
public String modified;
}

购物项目:

public class ShoppingItem {
public int id;
public int shopping_item_category_id;
public String name;
public String description;
public String created;
public String modified;
public String category_name;
}

这是我的 AsyncTask,它从服务器获取 Json:

public class APIRetrieveLists extends AsyncTask<APIRequestModel, Void, List<shoppingLists>>{

SQLHelper _sqlHelper = null;
Context _context;
ProgressBar _pb;

public APIRetrieveLists(Context context, ProgressBar pb){
this._context = context;
this._pb = pb;
}


@Override
protected void onPreExecute(){
}

@Override
protected void onPostExecute(List<shoppingLists> model){
this._sqlHelper = new SQLHelper(this._context);
if (model != null){
for (shoppingLists cn : model){
Log.i("infos", "list's name => " + cn.ShoppingList.name);
}
}else{
Log.i("infos", "model is null");
}
}

@Override
protected List<shoppingLists> doInBackground(APIRequestModel... arg0) {
APIRequestModel model = arg0[0];
try
{
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", model.getToken()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://apiurl/index.json");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String s = "";
String all = "";
while ((s = reader.readLine()) != null){
all += s;
}
Gson gson = new Gson();
List<shoppingLists> Obj = (List<shoppingLists>) gson.fromJson(all, new TypeToken<List<shoppingLists>>(){}.getType());
return (Obj);
}
catch (ClientProtocolException e) {
Log.i("infos", "first");
return (null);
}
}
catch (Exception e){
Log.i("infos", "second");
return (null);
}
}
}

异常“second”总是触发......Log.i("信息", "模型为空");也被执行。如果有人可以提供帮助:)谢谢 !问候。

json

最佳答案

向您的 shoppingLists 再添加一个包装器,因为您的 jsonObject 将 shoppingLists 作为键,将 ShoppingListItem 数组作为值。此外,您需要为 ShoppingItems 字段注释 @SerializedName,因为它与 Json 中的键不匹配。

在下面找到更新的类和解析器

Gson gson = new Gson();
FinalClass myObj = gson.fromJson(jsonString, FinalClass.class);

public class FinalClass {
public ArrayList<ShoppingListItem> shoppingLists;
}

public class ShoppingListItem {
public ShoppingList ShoppingList;
@SerializedName("ShoppingItem")
public List<ShoppingItem> ShoppingItems;
}

public class ShoppingList {
public int id;
public String name;
public String created;
public String modified;
}

public class ShoppingItem {
public int id;
public int shopping_item_category_id;
public String name;
public String description;
public String created;
public String modified;
public String category_name;
}

关于java - 使用 Gson 解析列表对象 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578967/

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