gson--> “类 xyz 的无参数构造函数”带数组

发布于 2024-10-04 02:27:04 字数 1763 浏览 3 评论 0原文

我尝试在 gson 的帮助下反序列化 json 字符串。虽然 gson.fromJson 我收到以下错误:

类 xyz 的无参数构造函数;不存在。向 Gson 注册该类型的 InstanceCreator 即可解决此问题

我尝试使用 InstanceCreate 但没有运行它。 我希望你能帮助我。

JSON String

[
{
    "prog": "Name1",
    "name": "Name2",
    "computername": "Name3",
    "date": "2010-11-20 19:39:55"
},
{
    "prog": "Name1",
    "name": "Name2",
    "computername": "Name3",
    "date": "2010-11-20 12:38:12"
}

]

根据 gson 我必须剪切第一个和最后一个字符(“[”和“]”) 根据 http://www.jsonlint.com/ 字符串的字符正确...:? :

代码看起来像这样:

    public class License {
   public String prog;
   public String name;
   public String computername;
   public String date;

   public License() {
      this.computername = "";
      this.date = "";
      this.name = "";
      this.prog = "";
       // no-args constructor
   }
}

               String JSONSerializedResources = "json_string_from_above"
           try
         {
              GsonBuilder gsonb = new GsonBuilder();
              Gson gson = gsonb.create();

              JSONObject j;

              License[] lic = null;
              j = new JSONObject(JSONSerializedResources);

              lic = gson.fromJson(j.toString(), License[].class);

               for (License license : lic) {
                  Toast.makeText(getApplicationContext(), license.name + " - " + license.date, Toast.LENGTH_SHORT).show();
            }
         }
         catch(Exception e)
         {
            Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
             e.printStackTrace();
         }

问候克里斯

i try to deserialize a json string with the help of gson. While gson.fromJson I get the following error:

No-args constructor for class xyz; does not exist. Register an InstanceCreator with Gson for this type to fix this problem

I tried to work with an InstanceCreate but I didn't get this running.
I hope you can help me.

JSON String

[
{
    "prog": "Name1",
    "name": "Name2",
    "computername": "Name3",
    "date": "2010-11-20 19:39:55"
},
{
    "prog": "Name1",
    "name": "Name2",
    "computername": "Name3",
    "date": "2010-11-20 12:38:12"
}

]

according to gson I have to cut the first and last chars ("[" and "]")
according to http://www.jsonlint.com/ the string is with the chars correct... :?:

the code looks like that:

    public class License {
   public String prog;
   public String name;
   public String computername;
   public String date;

   public License() {
      this.computername = "";
      this.date = "";
      this.name = "";
      this.prog = "";
       // no-args constructor
   }
}

               String JSONSerializedResources = "json_string_from_above"
           try
         {
              GsonBuilder gsonb = new GsonBuilder();
              Gson gson = gsonb.create();

              JSONObject j;

              License[] lic = null;
              j = new JSONObject(JSONSerializedResources);

              lic = gson.fromJson(j.toString(), License[].class);

               for (License license : lic) {
                  Toast.makeText(getApplicationContext(), license.name + " - " + license.date, Toast.LENGTH_SHORT).show();
            }
         }
         catch(Exception e)
         {
            Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
             e.printStackTrace();
         }

regards Chris

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

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

发布评论

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

评论(3

小兔几 2024-10-11 02:27:04

尝试将构造函数公开,以便 gson 可以实际访问它。

public License() {
  this.computername = "";
  this.date = "";
  this.name = "";
  this.prog = "";
  // no-args constructor
}

但由于 Java 创建了一个默认构造函数,您只需使用:

public class License {
    public String prog = "";
    public String name = "";
    public String computername = "";
    public String date = "";
}

更新:

这确实非常简单:JSONObject 需要一个 Json 对象“{..}”。您应该使用需要“[...]”的 JSONArray

我尝试过并且有效。您仍然应该如上所述更改 License 类。

Try making your constructor public, so that gson can actually access it.

public License() {
  this.computername = "";
  this.date = "";
  this.name = "";
  this.prog = "";
  // no-args constructor
}

but since Java creates a default constructor, you could just use:

public class License {
    public String prog = "";
    public String name = "";
    public String computername = "";
    public String date = "";
}

Update:

It's really quite trivial: JSONObject expects a Json object "{..}". You should use JSONArray which expects "[...]".

I tried it and it works. You should still change License class as noted above.

神也荒唐 2024-10-11 02:27:04

令人难以置信...

现在我尝试了不是这样

j = new JSONObject(JSONSerializedResources);
lic = gson.fromJson(j.toString(), License[].class);

而是直接

lic = gson.fromJson(JSONSerializedResources, License[].class);

使用我的旧原始 json 字符串以“[”和结尾“[”& “]”

并且它有效

it is unbelievable...

now I tried it not so

j = new JSONObject(JSONSerializedResources);
lic = gson.fromJson(j.toString(), License[].class);

but directly so

lic = gson.fromJson(JSONSerializedResources, License[].class);

with my old original json string with starting and ending "[" & "]"

and it works

笑看君怀她人 2024-10-11 02:27:04

如果不需要,请尝试删除构造函数
我尝试使用两个类
A 类具有 B 类列表,

删除了所有构造函数,对我来说工作得很好。

Try removing the constructors if you don't need
I tried using two classes
Class A having list of Class B

Removed all the constructors and just worked fine for me.

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