gson 到 POJO 对象,无法正常工作

发布于 2024-09-17 00:02:35 字数 2038 浏览 5 评论 0 原文

我正在开发一个 Android 应用程序,并访问返回 JSON 的 RESTfull Web 服务。我想将这个 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: 警告/系统错误(568): org.json.JSONException:值 [{"Id":1,"CategoryName":"食品"},{"Id":2,"CategoryName":"汽车"},{"Id":3,"CategoryName":"房子"},{ "Id":4,"CategoryName":"工作"}] org.json.JSONArray 类型不能是 转换为 JSONObject 09-02 07:06:47.029: 警告/系统错误(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 天现在不知道如何继续。我在这里找到了一些主题,但仍然没有找到解决方法。非常感谢。

I am developing an Android application and I access a RESTfull web service that returns a JSON. This JSON I want to put it in POJOs but I think I am missing something as it doesn't work.

The JSON retuned is as follow:

[{"CategoryName":"Food","Id":1},{"CategoryName":"Car","Id":2},{"CategoryName":"House","Id":3},{"CategoryName":"Work","Id":4}]

this is returned in response variable

  String response = client.getResponse();

And now I try the following:

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

The error I get is:

09-02 07:06:47.009:
WARN/System.err(568):
org.json.JSONException: Value
[{"Id":1,"CategoryName":"Food"},{"Id":2,"CategoryName":"Car"},{"Id":3,"CategoryName":"House"},{"Id":4,"CategoryName":"Work"}]
of type org.json.JSONArray cannot be
converted to JSONObject
09-02 07:06:47.029:
WARN/System.err(568): at
org.json.JSON.typeMismatch(JSON.java:107)

Here are the POJO objects
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;
 }

}

To access the web service I use the class from: http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

Please help me as I am stuck for 2 days now and can't figure out how to continue. I found some subjects here but still didn't found a way around. Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2024-09-24 00:02:35

当您尝试将其解析为对象时,JSON 字符串中的顶级实体是 JSONArray 而不是 JSONObject。从字符串创建一个数组并使用它。

JSONArray array = new JSONArray(response);

Top level entity in your JSON string is JSONArray not JSONObject, while you're trying to parse it as object. Create an array from the string and use that.

JSONArray array = new JSONArray(response);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文