如何使用gson解析这个嵌套的json

时间:2012-10-06 05:17:35

标签: java json parsing gson

我将此json作为字符串(这是来自Google自定义搜索)。我已经制作了需要解析它的等效java类。我使用gson并得到一个NullPointerException。

"items": [
{
"kind": "customsearch#result",
"title": "A Florida Road Trip - Articles | Travel + Leisure",
"htmlTitle": "A \u003cb\u003eFlorida\u003c/b\u003e Road Trip -             \u003cb\u003eArticles\u003c/b\u003e | Travel + Leisure",
"link": "http://www.travelandleisure.com/articles/paradise-usa",
"displayLink": "www.travelandleisure.com",
"snippet": "On a road trip across Florida, Karrie Jacobs goes in search of the   authentic amid   New Urbanist experiments in postmodern nostalgia.",
"htmlSnippet": "On a road trip across \u003cb\u003eFlorida\u003c/b\u003e, Karrie Jacobs goes in search of the authentic amid \u003cbr\u003e  New Urbanist experiments in postmodern nostalgia.",
"cacheId": "QrfYt6p-U3gJ",
"formattedUrl": "www.travelandleisure.com/articles/paradise-usa",
"htmlFormattedUrl":  "www.travelandleisure.com/\u003cb\u003earticles\u003c/b\u003e/paradise-usa",
"pagemap": {
"cse_image": [
  {
   "src":  "http://static0.travelandleisure.com/images/amexpub/0000/6261/200702_urban.jpg"
 }
],
"cse_thumbnail": [
 {
  "width": "160",
  "height": "200",
    "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcR_Jhmej45Lu7L3bQoKz1poITJ5ZeuVvKhH62myfBOj-1VEX9oqwIhvH74"
 }
],
"metatags": [
 {
  "fb:app_id": "145695458805001",
  "og:url": "http://www.travelandleisure.com/articles/paradise-usa",
  "og:site_name": "Travel + Leisure",
  "og:image": "http://static0.travelandleisure.com/images/amexpub/0000/6261/200702_urban.jpg",
  "og:type": "article",
  "og:title": "Driving: Florida’s New Urbanist Experiments",
  "og:description": "On a road trip across Florida, Karrie Jacobs goes in search of the authentic amid New Urbanist exper..."
 }
]
}
}

我的等效java类如下:

public Class Mapper
{
       public Items[] items;
}

public Class Items
{
   public String title;
   public String htmlSnippet;
   public String link;
   public Pagemap pagemap;
}

public Class Pagemap
{
  public Cse_image[] image;
}
public Class Cse_image
{
  public String src;
}
public Class Parser
{
  public static void main(String[] args)
  {
    //assume json is the string representation of the above json
     Gson gson = new GsonBuilder().create();
     Mapper mapper= gson.fromJson(json,Mapper.class);
     System.out.println("src here"+mapper.items[0].pagemap.image[0].src);
  }
}

上面的代码抛出了一个NullPointerException,但是如果我打印到页面地图,我会得到一个带有有效哈希码的对象。有什么指针吗?

2 个答案:

答案 0 :(得分:0)

每当处理JSON时,最好使用LINT软件来验证JSON的内容。我建议使用jsonlint.com。 JSON列表显示JSON字符串中的多个错误。在这里运行一些REPL循环是更正的JSON。

{
    "items": [
        {
            "kind": "customsearch#result",
            "title": "AFloridaRoadTrip-Articles|Travel+Leisure",
            "htmlTitle": "A<b>Florida</b>RoadTrip-<b>Articles</b>|Travel+Leisure",
            "link": "http: //www.travelandleisure.com/articles/paradise-usa",
            "displayLink": "www.travelandleisure.com",
            "snippet": "OnaroadtripacrossFlorida,KarrieJacobsgoesinsearchoftheauthenticamidNewUrbanistexperimentsinpostmodernnostalgia.",
            "htmlSnippet": "Onaroadtripacross<b>Florida</b>,KarrieJacobsgoesinsearchoftheauthenticamid<br>NewUrbanistexperimentsinpostmodernnostalgia.",
            "cacheId": "QrfYt6p-U3gJ",
            "formattedUrl": "www.travelandleisure.com/articles/paradise-usa",
            "htmlFormattedUrl": "www.travelandleisure.com/<b>articles</b>/paradise-usa",
            "pagemap": {
                "cse_image": [
                    {
                        "src": "http: //static0.travelandleisure.com/images/amexpub/0000/6261/200702_urban.jpg"
                    }
                ],
                "cse_thumbnail": [
                    {
                        "width": "160",
                        "height": "200",
                        "src": "https: //encrypted-tbn1.gstatic.com/images?q=tbn: ANd9GcR_Jhmej45Lu7L3bQoKz1poITJ5ZeuVvKhH62myfBOj-1VEX9oqwIhvH74"
                    }
                ],
                "metatags": [
                    {
                        "fb: app_id": "145695458805001",
                        "og: url": "http: //www.travelandleisure.com/articles/paradise-usa",
                        "og: site_name": "Travel+Leisure",
                        "og: image": "http: //static0.travelandleisure.com/images/amexpub/0000/6261/200702_urban.jpg",
                        "og: type": "article",
                        "og: title": "Driving: Florida’sNewUrbanistExperiments",
                        "og: description": "OnaroadtripacrossFlorida,KarrieJacobsgoesinsearchoftheauthenticamidNewUrbanistexper..."
                    }
                ]
            }
        }
    ]
}

答案 1 :(得分:0)

首先将您的Cse_Image类重命名为更传统的Java名称(可能是CseImage?),缩略图类也是如此。

然后你可以尝试genson library http://code.google.com/p/genson/。 将@JsonProperty(“cse_image”)注释放在图像字段上(如果有的话,放在你的setter / getter上)并且它可以工作:

 Genson genson = new Genson();
 Mapper mapper= genson.deserialize(json,Mapper.class);
 System.out.println("src here"+mapper.items[0].pagemap.image[0].src);