Phrasing Json Array Using Gson For Google Search Api
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
Solution 1:
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;
publicclassFoo
{
publicstaticvoidmain(String[] args)throws Exception
{
Gsongson=newGson();
Responseresponse= gson.fromJson(newFileReader("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/
}
}
classResponse
{
ResponseData responseData;
String responseDetails;
int responseStatus;
}
classResponseData
{
List<Result> results;
Cursor cursor;
}
classResult
{
String GsearchResultClass;
String unescapedUrl;
String url;
String visibleUrl;
String cacheUrl;
String title;
String titleNoFormatting;
String content;
}
classCursor
{
List<Page> pages;
BigInteger estimatedResultCount;
int currentPageIndex;
String moreResultsUrl;
}
classPage
{
int start;
int label;
}
Post a Comment for "Phrasing Json Array Using Gson For Google Search Api"