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
その両方を取得するにはどうすればよいですか?