使用Gson为谷歌搜索api表达Json数组

时间:2011-07-13 07:50:47

标签: android json gson

首先对不起真的很长的帖子,现在

这是我的班级结构,不知道是对还是错

public class GoogleResponse {
           public ResponseDate responseData;
           public String responseDetails;
           public String responseStatus;
        }

    public class ResponseData {
           public List<Result> results;
           //public Cursor cursor;
        }
    public class Result {
           public String titleNoFormatting;
           public String unescapedUrl;

        }

这是反序列化的代码

Gson gson = new Gson();
GoogleResponse data[] = gson.fromJson(s, GoogleResponse[].class);\\s is the JSON string

在这个程序中,我只想提取titlenoformating和unescapedurl,这就是为什么我遗漏了课堂上的其他内容。

我不知道这是对还是错,但是当我做System.out.print(数据)时; 我在logcat中什么也得不到,我不知道如何访问data []中存储的数据。 我想要的是使用titleNoFormating填充列表视图,并通过意图点击任何结果打开相应的unescapedurl。

编辑:

{
    "results": [
        {
            "GsearchResultClass": "GwebSearch",
            "unescapedUrl": "http://www.mediafire.com/?zcnqy5mmwmj",
            "url": "http://www.mediafire.com/%3Fzcnqy5mmwmj",
            "visibleUrl": "www.mediafire.com",
            "cacheUrl": "http://www.google.com/search?q=cache:f6cE2lmmCioJ:www.mediafire.com",
            "title": "Redman Funk From <b>Hell</b> 2010.zip",
            "titleNoFormatting": "Redman Funk From Hell 2010.zip",
            "content": "Redman Funk From <b>Hell</b> 2010.zip. <b>...</b> Share “Redman Funk From <b>Hell</b> 2010.zip”. Info  . Facebook/Twitter. Email. Share by IM. Embed. HTML Embed Code. Sharing URL <b>...</b>",
            "clicktrackUrl": "//www.google.com/url?q=http://www.mediafire.com/?zcnqy5mmwmj&sa=T&usg=AFQjCNGhKqruZDyj614zfvjuitABOJFrNQ&ei=BUQdTtbGLeWTmQWElOHzBw&ved=0CAQQFjAA"
        },
        {
            "GsearchResultClass": "GwebSearch",
            "unescapedUrl": "http://www.mediafire.com/?ymto5mjznwz",
            "url": "http://www.mediafire.com/%3Fymto5mjznwz",
            "visibleUrl": "www.mediafire.com",
            "cacheUrl": "http://www.google.com/search?q=cache:aXARYHERXiQJ:www.mediafire.com",
            "title": "This Routine is <b>Hell</b> - The Verve Crusade.zip - This, Routine, is <b>...</b>",
            "titleNoFormatting": "This Routine is Hell - The Verve Crusade.zip - This, Routine, is ...",
            "content": "Debut full-length The Verve Crusade by hardcore punk band This Routine is <b>Hell</b>   from the Netherlands. Released by Shield Recordings in 2010.",
            "clicktrackUrl": "//www.google.com/url?q=http://www.mediafire.com/?ymto5mjznwz&sa=T&usg=AFQjCNGd4xVGQkOlb8TMCdpH5tEIn2Ln5A&ei=BUQdTtbGLeWTmQWElOHzBw&ved=0CAYQFjAB"
        }
    ]
}

这变得有效所以我想我必须用mu自己的方法来制作这个内容

1 个答案:

答案 0 :(得分:1)

  

当我做System.out.print(数据);我在logcat中什么都没得到

使用android.util.Log。(),而不是System.out.println();

关于解析JSON,遗憾的是原始问题中列出的JSON是无效的,这留下了可能有助于猜测的人。 Google's own search API documentation page上的示例JSON也无效(虽然以不同的方式) - 它会转义'['和']'字符,但JSON规范不允许转义这些字符。

以下是Google搜索API文档中示例JSON的更正版本。

{
    "responseData": {
        "results": [
            {
                "GsearchResultClass": "GwebSearch",
                "unescapedUrl": "http://en.wikipedia.org/wiki/Paris_Hilton",
                "url": "http://en.wikipedia.org/wiki/Paris_Hilton",
                "visibleUrl": "en.wikipedia.org",
                "cacheUrl": "http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org",
                "title": "<b>Paris Hilton</b> - Wikipedia, the free encyclopedia",
                "titleNoFormatting": "Paris Hilton - Wikipedia, the free encyclopedia",
                "content": "[1] In 2006, she released her debut album..."
            },
            {
                "GsearchResultClass": "GwebSearch",
                "unescapedUrl": "http://www.imdb.com/name/nm0385296/",
                "url": "http://www.imdb.com/name/nm0385296/",
                "visibleUrl": "www.imdb.com",
                "cacheUrl": "http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com",
                "title": "<b>Paris Hilton</b>",
                "titleNoFormatting": "Paris Hilton",
                "content": "Self: Zoolander. Socialite <b>Paris Hilton</b>..."
            }
        ],
        "cursor": {
            "pages": [
                {
                    "start": "0",
                    "label": 1
                },
                {
                    "start": "4",
                    "label": 2
                },
                {
                    "start": "8",
                    "label": 3
                },
                {
                    "start": "12",
                    "label": 4
                }
            ],
            "estimatedResultCount": "59600000",
            "currentPageIndex": 0,
            "moreResultsUrl": "http://www.google.com/search?oe=utf8&ie=utf8..."
        }
    },
    "responseDetails": null,
    "responseStatus": 200
}

这是一个示例程序,使用Gson将此JSON反序列化为Java数据结构,然后检索两个目标数据元素。

import java.io.FileReader;
import java.math.BigInteger;
import java.util.List;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Response response = gson.fromJson(new FileReader("input.json"), Response.class);
    for (Result result : response.responseData.results)
    {
      System.out.println("titleNoFormatting: " + result.titleNoFormatting);
      System.out.println("unescapedUrl: " + result.unescapedUrl);
    }
    // output:
    // titleNoFormatting: Paris Hilton - Wikipedia, the free encyclopedia
    // unescapedUrl: http://en.wikipedia.org/wiki/Paris_Hilton
    // titleNoFormatting: Paris Hilton
    // unescapedUrl: http://www.imdb.com/name/nm0385296/
  }
}

class Response
{
  ResponseData responseData;
  String responseDetails;
  int responseStatus;
}

class ResponseData
{
  List<Result> results;
  Cursor cursor;
}

class Result
{
  String GsearchResultClass;
  String unescapedUrl;
  String url;
  String visibleUrl;
  String cacheUrl;
  String title;
  String titleNoFormatting;
  String content;
}

class Cursor
{
  List<Page> pages;
  BigInteger estimatedResultCount;
  int currentPageIndex;
  String moreResultsUrl;
}

class Page
{
  int start;
  int label;
}