【发布时间】:2020-06-01 23:13:24
【问题描述】:
我正在使用 PHP 生成从数据库查询中获取的 Json。结果如下所示:
[
{
"title":"Title 1",
"description":"This is description 1",
"add_date":"2013-07-17 10:07:53"
},{
"title":"Title 2",
"description":"This is description 2",
"add_date":"2013-07-17 10:07:53"
}
]
我正在使用 Gson 来解析数据,如下所示:
public class Search{
public Search(String text){
try{
// Snipped (gets the data from the website)
Gson json = new Gson();
Map<String, Event> events = json.fromJson(resultstring, new TypeToken<Map<String, Event>>(){}.getType());
System.out.print(events.get("description"));
}catch(IOException ex){
Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Event {
private String description;
}
这是我在尝试运行代码时收到的消息:
线程“AWT-EventQueue-0”com.google.gson.JsonSyntaxException 中的异常:java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 3 列是 BEGIN_OBJECT
我将如何遍历每一个以获取 description 或 title 或两者的值?
【问题讨论】: