首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gson for google search api创建Json数组

使用Gson for google search api创建Json数组
EN

Stack Overflow用户
提问于 2011-07-13 15:50:47
回答 1查看 1.6K关注 0票数 0

首先,很抱歉发了这么长的帖子,现在

这是我的类结构,不知道它是对是错

代码语言:javascript
复制
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;

        }

这是反序列化的代码

代码语言:javascript
复制
Gson gson = new Gson();
GoogleResponse data[] = gson.fromJson(s, GoogleResponse[].class);\\s is the JSON string

在这个程序中,我只想提取高保真的和未转义的the,这就是为什么我从类中省略了其余的内容。

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

编辑:

代码语言:javascript
复制
{
    "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"
        }
    ]
}

这是有效的,所以我想我必须自己编写方法才能得到这个内容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-13 16:00:07

当我执行System.out.print(数据)时,日志;我在

中什么也得不到

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

关于解析JSON,不幸的是,原始问题中列出的JSON是无效的,这可能会让人们猜测一下。Google's own search API documentation page上的示例JSON也是无效的(尽管方式不同) --它转义了'‘和'’字符,但是JSON规范不允许转义这些字符。

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

代码语言:javascript
复制
{
    "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数据结构,然后检索两个目标数据元素。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6675714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档