Java Gson解码json

Java Gson解码json,java,json,gson,Java,Json,Gson,我正在使用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

我正在使用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"
    }
]
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;
}
我使用Gson解析数据,如下所示:

[
    {
        "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"
    }
]
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;
}
公共类搜索{
公共搜索(字符串文本){
试一试{
//截取(从网站获取数据)
Gson json=new Gson();
Map events=json.fromJson(resultstring,new-TypeToken(){}.getType());
系统输出打印(events.get(“description”);
}捕获(IOEX异常){
Logger.getLogger(Search.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
班级活动{
私有字符串描述;
}
以下是我在尝试运行代码时收到的消息:

线程“AWT-EventQueue-0”com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_数组,但在第1行第3列为BEGIN_对象


如何循环每一项以获得
description
、或
title
或两者的值?

对您正在做的事情进行几处更正,您应该准备好:

class Event {
    private String description;
    private String title;
    @SerializedName("add_date") private String addDate;

   public getDescription() {
       return description;
   }
}


 public Search(String text){
    try{

        // Snipped (gets the data from the website)

        Gson json = new Gson();
        Event[] events = json.fromJson(resultstring, Event[].class);

        System.out.print(events[0].getDescription());

    }catch(IOException ex){
        Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
    }

}

我已经更正了您的bean类,并更改了您转换为的类型(事件数组,因为这是您实际从PHP服务中获得的)

工作正常,但我收到了以下错误:
找不到符号:class SerialzedName位置:class Event
。还有,为什么要序列化它?@ryannady这是一个注释,告诉您不要使用字段名和json属性的直接对应关系。但是,您需要导入注释。这就是你得到的错误