Androidアプリケーションを開発していて、JSONを返すRESTfullWebサービスにアクセスしています。このJSONをPOJOに入れたいのですが、機能しないため、何かが足りないと思います。
再調整されたJSONは次のとおりです。
[{"CategoryName": "Food"、 "Id":1}、{"CategoryName": "Car"、 "Id":2}、{"CategoryName": "House"、 "Id":3}、{ "CategoryName": "Work"、 "Id":4}]
これは応答変数で返されます
String response = client.getResponse();
そして今、私は次のことを試みます:
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
JSONObject j;
MainCategories cats = null;
try
{
j = new JSONObject(response);
cats = gson.fromJson(j.toString(), MainCategories.class);
}
catch(Exception e)
{
e.printStackTrace();
}
私が得るエラーは次のとおりです。
09-02 07:06:47.009:WARN / System.err(568):org.json.JSONException:Value [{"Id":1、 "CategoryName": "Food"}、{"Id":2、 "タイプorg.json.JSONArrayのCategoryName":" Car "}、{" Id ":3、" CategoryName ":" House "}、{" Id ":4、" CategoryName ":"Work"}]は変換できませんJSONObjectへ09-0207:06:47.029:WARN / System.err(568):org.json.JSON.typeMismatch(JSON.java:107)
これがPOJOオブジェクトMainCategories.javaです。
public class MainCategories {
private List<CategoryInfo> category;
public List<CategoryInfo> getCategory() {
if (category == null) {
category = new ArrayList<CategoryInfo>();
}
return this.category;
}
}
CategoryInfo.java
public class CategoryInfo {
public String categoryName;
public Integer id;
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String value) {
this.categoryName = ((String) value);
}
public Integer getId() {
return id;
}
public void setId(Integer value) {
this.id = value;
}
}
Webサービスにアクセスするには、次のクラスを使用します:http: //lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
私は2日間立ち往生していて、続行する方法がわからないので、私を助けてください。私はここでいくつかの主題を見つけましたが、それでも回避策を見つけられませんでした。どうもありがとうございます。