gson--> “类 xyz 的无参数构造函数”带数组
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将构造函数公开,以便 gson 可以实际访问它。
但由于 Java 创建了一个默认构造函数,您只需使用:
更新:
这确实非常简单:
JSONObject
需要一个 Json 对象“{..}”。您应该使用需要“[...]”的JSONArray
。我尝试过并且有效。您仍然应该如上所述更改
License
类。Try making your constructor public, so that gson can actually access it.
but since Java creates a default constructor, you could just use:
Update:
It's really quite trivial:
JSONObject
expects a Json object "{..}". You should useJSONArray
which expects "[...]".I tried it and it works. You should still change
License
class as noted above.令人难以置信...
现在我尝试了不是这样
而是直接
使用我的旧原始 json 字符串以“[”和结尾“[”& “]”
并且它有效
it is unbelievable...
now I tried it not so
but directly so
with my old original json string with starting and ending "[" & "]"
and it works
如果不需要,请尝试删除构造函数
我尝试使用两个类
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.