[英]Json array to Java class (deserializing with Gson)
I have Json response (named as jsonResultAsText): 我有Json响应(名为jsonResultAsText):
{
"Title": "Hi all",
"IsSecret": false,
"Agents": [
{
"ID": 3,
"Name": "John F"
},
{
"ID": 7,
"Name": "Jack D"
}
]
}
I want to deserialize it using Google's gson library. 我想使用Google的gson库反序列化它。
I have classes like: 我有类似的课程:
public class JsonResponse {
public String Title;
public boolean IsSecret;
public List<Agent> Agents;
}
public class Agent {
public int ID;
public String Name;
}
I want to get json data into JsonResponse class. 我想将JSON数据导入JsonResponse类。
It's the code that I try to achieve: 这是我尝试实现的代码:
JsonResponse response =
new Gson().fromJson(jsonResultAsText, JsonResponse.class);
But my Android application gives that classic error: Unfortunately, < app_name > has stopped. 但是我的Android应用程序给出了经典错误:不幸的是,<app_name>已停止。
I know that there are plenty examples about serializing/deserializing via gson class, but I could not find similar problem/example. 我知道有很多关于通过gson类进行序列化/反序列化的示例,但是我找不到类似的问题/示例。
Thanks in advance. 提前致谢。
I solve the problem. 我解决了问题。
I had forgotten to specify this line on json text I provide (on each element of Agents array): 我忘记在我提供的json文本上指定此行(在Agents数组的每个元素上):
"Since": "2012-01-07T19:11:01.719"
and in my Agent class: 在我的特工类中:
public class Agent {
public int ID;
public String Name;
public DateTime Since;
}
Sure I have that import: import org.joda.time.DateTime;
当然,我已经导入了: import org.joda.time.DateTime;
When I change data type of Since variable from DateTime to String, problem gone. 当我将Before变量的数据类型从DateTime更改为String时,问题就消失了。
Now I can deserialize it. 现在,我可以反序列化它。
I am just going to learn how to send DateTime
from Asp.Net Web Api
to Java
via Json.
我将学习如何通过Json.
将DateTime
从Asp.Net Web Api
发送到Java
Json.
Thanks for comments. 感谢您的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.