开发者

Phrasing Json Array using Gson for google search api

开发者 https://www.devze.com 2023-03-20 03:21 出处:网络
First of all Sorry for the really long post, now And this is my class structure, no idea if it\'s right or wrong

First of all Sorry for the really long post, now

And this is my class structure, no idea if it's right or wrong

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;

        }

And this is the code for deserialization

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

In this program i just want to extract titlenoformating and unescapedurl, that's why i left out rest of the content from the class's.

I don't know if this is right or wrong, but when i do System.out.print(data); I get nothing in logcat, i don't know how to access the data that is stored in data[]. What i want is to populate a listview using the titleNoFormating and open the corresponding unescapedurl on clicking any results via intent.

EDIT:

{
    "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"
        }
    ]
}

This becomes valid so i guess i'll have t开发者_JAVA百科o make up mu own method to get out this content


when i do System.out.print(data); I get nothing in logcat

Use android.util.Log.(), not System.out.println();

Concerning parsing the JSON, unfortunately the JSON listed in the original question is invalid, which leaves folks that might help guessing a bit. And the example JSON on Google's own search API documentation page is also invalid (though in a different way) -- it escapes the '[' and ']' characters, but the JSON spec does not allow those characters to be escaped.

Following is a corrected version of the example JSON from the Google search API documentation.

{
    "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
}

And here is an example program using Gson to deserialize this JSON to a Java data structure, and then retrieving the two target data elements.

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消