Newbie question

38 views
Skip to first unread message

el toro loco

unread,
Feb 26, 2010, 12:04:03 PM2/26/10
to google-gson
Good day all,

I'm a new user of Gson. I have a String (actually the output of a
request to a Google AJAX search API) that I need to manipulate into a
Map. I guess I have to write a custom deserialiser, right?

(If it helps at all, here's the String in question. Forgive my choice
of a dodgy old ABBA hit single as my input!)

{
"responseData": {
"results": [
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://beemp3.com/download.php?file
\u003d2448732\u0026song\u003dDancing+Queen",
"url": "http://beemp3.com/download.php%3Ffile
%3D2448732%26song%3DDancing%2BQueen",
"visibleUrl": "beemp3.com",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:daohdTzwLd4J:beemp3.com",
"title": "Abba - \u003cb\u003eDancing Queen\u003c/b
\u003e: \u003cb\u003eFree MP3\u003c/b\u003e Download",
"titleNoFormatting": "Abba - Dancing Queen: Free MP3
Download",
"content": "\u003cb\u003efree mp3\u003c/b\u003e
download, Gold: Greatest Hits, \u003cb\u003emp3\u003c/b\u003e, mp3s,
\u003cb\u003efree\u003c/b\u003e, \u003cb\u003emp3\u003c/b\u003e
download, \u003cb\u003emp3\u003c/b\u003e dowload, \u003cb\u003e...
\u003c/b\u003e 3. \u003cb\u003emp3\u003c/b\u003e: ABBA - \u003cb
\u003eDancing Queen\u003c/b\u003e Album: The Complete Singles
Collectio \u003cb\u003e...\u003c/b\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.last.fm/music/ABBA/_/
Dancing+Queen",
"url": "http://www.last.fm/music/ABBA/_/Dancing
%2BQueen",
"visibleUrl": "www.last.fm",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:KFRooDuqx88J:www.last.fm",
"title": "ABBA – \u003cb\u003eDancing Queen\u003c/b
\u003e – Video \u0026amp; \u003cb\u003efree\u003c/b\u003e listening at
Last.fm",
"titleNoFormatting": "ABBA – Dancing Queen – Video
\u0026amp; free listening at Last.fm",
"content": "Watch the video \u0026amp; listen to ABBA
– \u003cb\u003eDancing Queen\u003c/b\u003e (full track) for \u003cb
\u003efree\u003c/b\u003e. \u003cb\u003eDancing\u003c/b\u003e \u003cb
\u003eQueen\u003c/b\u003e appears on the album Gold: \u003cb\u003e...
\u003c/b\u003e Buy at Amazon \u003cb\u003eMP3\u003c/b\u003e (Search)
More options \u003cb\u003e...\u003c/b\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.ez-tracks.com/getsong-
songid-29509.html",
"url": "http://www.ez-tracks.com/getsong-
songid-29509.html",
"visibleUrl": "www.ez-tracks.com",
"cacheUrl": "http://www.google.com/search?q\u003dcache:
1PPfUm7ztf8J:www.ez-tracks.com",
"title": "\u003cb\u003eDancing Queen Mp3\u003c/b\u003e
Download, \u003cb\u003eFree\u003c/b\u003e Abba \u003cb\u003eDancing
Queen\u003c/b\u003e Download – EZ \u003cb\u003e...\u003c/b\u003e",
"titleNoFormatting": "Dancing Queen Mp3 Download, Free
Abba Dancing Queen Download – EZ ...",
"content": "Download Abba, \u003cb\u003eDancing Queen
mp3\u003c/b\u003e for \u003cb\u003eFree\u003c/b\u003e! Build a great
music collection without spending a penny - download \u003cb
\u003eDancing Queen\u003c/b\u003e from EZ-Tracks."
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.emp3world.com/mp3/24694/
ABBA/Dancing%20Queen",
"url": "http://www.emp3world.com/mp3/24694/ABBA/Dancing
%2520Queen",
"visibleUrl": "www.emp3world.com",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:uJaj5QfEs5MJ:www.emp3world.com",
"title": "ABBA - \u003cb\u003eDancing Queen Mp3\u003c/b
\u003e Download \u003cb\u003eFree\u003c/b\u003e",
"titleNoFormatting": "ABBA - Dancing Queen Mp3
Download Free",
"content": "Oct 23, 2009 \u003cb\u003e...\u003c/b
\u003e ABBA - \u003cb\u003eDancing Queen Mp3\u003c/b\u003e Download
\u003cb\u003eFree\u003c/b\u003e. \u003cb\u003e...\u003c/b\u003e
ringtones Send \u0026quot;\u003cb\u003eDancing Queen\u003c/b\u003e
\u0026quot; Ringtone to your Cell ringtones \u003cb\u003e...\u003c/b
\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.get-music.net/mp3-474161/
abba-dancing-queen.html",
"url": "http://www.get-music.net/mp3-474161/abba-
dancing-queen.html",
"visibleUrl": "www.get-music.net",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:APRs6Gv7faMJ:www.get-music.net",
"title": "Abba - \u003cb\u003eDancing Queen Mp3\u003c/b
\u003e Download \u003cb\u003eFree\u003c/b\u003e @ Get-Music.net",
"titleNoFormatting": "Abba - Dancing Queen Mp3
Download Free @ Get-Music.net",
"content": "Send \u0026quot;\u003cb\u003eDancing Queen
\u003c/b\u003e\u0026quot; Ringtone to your Cell \u0026middot; Download
More \u003cb\u003eMp3 Free\u003c/b\u003e Songs From Abba. User
Comments. You have to register to comment. \u003cb\u003e...\u003c/b
\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://mog.com/music/ABBA",
"url": "http://mog.com/music/ABBA",
"visibleUrl": "mog.com",
"cacheUrl": "http://www.google.com/search?q\u003dcache:
19w2Ys_HkQoJ:mog.com",
"title": "ABBA \u003cb\u003eFree MP3\u003c/b\u003e,
Song Download, Lyrics, Music Videos",
"titleNoFormatting": "ABBA Free MP3, Song Download,
Lyrics, Music Videos",
"content": "\u003cb\u003eFree\u003c/b\u003e ABBA
streaming \u003cb\u003eMP3\u003c/b\u003e download, music videos and
reviews on MOG. Posts about. \u003cb\u003e...\u003c/b\u003e \u003cb
\u003eDancing Queen\u003c/b\u003e. by ABBA. 32 Flavors. by Alana
Davis. Sweet Love \u003cb\u003e...\u003c/b\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.amazon.com/ABBA-Gold-
Greatest-Hits/dp/B000001DZO",
"url": "http://www.amazon.com/ABBA-Gold-Greatest-Hits/
dp/B000001DZO",
"visibleUrl": "www.amazon.com",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:smuAZWmaoF0J:www.amazon.com",
"title": "Amazon.com: ABBA - Gold: Greatest Hits:
ABBA: Music",
"titleNoFormatting": "Amazon.com: ABBA - Gold:
Greatest Hits: ABBA: Music",
"content": "\u003cb\u003eFREE\u003c/b\u003e 2-Day
Shipping, No Minimum Purchase: See details \u003cb\u003e...\u003c/b
\u003e Buy the \u003cb\u003eMP3\u003c/b\u003e album for $8.99 at the
Amazon \u003cb\u003eMP3\u003c/b\u003e Downloads store. \u003cb
\u003e....\u003c/b\u003e Likewise, you don\u0026#39;t have to be
Priscilla to swoon over \u0026quot;Mamma Mia\u0026quot; or \u0026quot;
\u003cb\u003eDancing Queen\u003c/b\u003e. \u003cb\u003e...\u003c/b
\u003e"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.directfreemp3.com/
mp3/420",
"url": "http://www.directfreemp3.com/mp3/420",
"visibleUrl": "www.directfreemp3.com",
"cacheUrl": "http://www.google.com/search?q
\u003dcache:bTbtc4LfGIMJ:www.directfreemp3.com",
"title": "Download Abba \u003cb\u003eDancing Queen
free mp3\u003c/b\u003e",
"titleNoFormatting": "Download Abba Dancing Queen free
mp3",
"content": "\u003cb\u003eFREE MP3\u003c/b\u003e
DOWNLOAD. Abba - \u003cb\u003eDancing Queen\u003c/b\u003e. File Size:
3.53 MB - Play Length: 0:00 - VBR kbit. » RIGHT CLICK HERE
\u0026amp; SAVE TARGET AS « \u003cb\u003e...\u003c/b\u003e"
}
],
"cursor": {
"pages": [
{
"start": "0",
"label": 1
},
{
"start": "8",
"label": 2
},
{
"start": "16",
"label": 3
},
{
"start": "24",
"label": 4
},
{
"start": "32",
"label": 5
},
{
"start": "40",
"label": 6
},
{
"start": "48",
"label": 7
},
{
"start": "56",
"label": 8
}
],
"estimatedResultCount": "163000",
"currentPageIndex": 0,
"moreResultsUrl": "http://www.google.com/search?oe
\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start
\u003d0\u0026safe\u003doff\u0026filter\u003d1\u0026hl\u003den\u0026q
\u003dmp3+free+dancing+queen"
}
},
"responseDetails": null,
"responseStatus": 200
}

Jason Buberel

unread,
Feb 26, 2010, 12:48:43 PM2/26/10
to googl...@googlegroups.com
Although I not sure I understand by what you mean by 'a Map', the straightforward approach would be to use the generic Gson deserialization (JSON text -> Gson Ojbect tree) to traverse the nodes and pull out the important data into a secondary structure (perhaps Map-based). Then use the standard Gson serialization support to convert that secondary structure back to JSON-formatted output as needed.

 
Jason L. Buberel
ja...@buberel.org



--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.


el toro loco

unread,
Mar 1, 2010, 10:39:05 AM3/1/10
to google-gson
Thanks for your reply, Jason.

I must be missing a basic step, because the following fails (at the
last line) with "java.lang.IllegalStateException: This is not a JSON
Object." My json variable is obviously not a JsonObject:


BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Gson gson = new Gson();
Type mapType = new TypeToken<HashMap<String, String>>()
{}.getType();
HashMap<String, String> map = new HashMap<String, String>();
String json = gson.toJson(builder.toString());
map = gson.fromJson(json, mapType);

Can you tell me what step I'm missing, please? Thank you.

> ...
>
> read more »

el toro loco

unread,
Mar 3, 2010, 5:43:25 AM3/3/10
to google-gson
[bump]

> ...
>
> read more »

Jason Buberel

unread,
Mar 3, 2010, 12:34:03 PM3/3/10
to googl...@googlegroups.com
Can you get something like the following to work in your environment:

JsonParser j = new JsonParser(builder.toString());
JsonElement e = j.parse(");
LOGGER.info("E: " + e);
for ( JsonElement je : ((JsonObject) ((JsonObject) e)).getAsJsonArray("results")) {
    LOGGER.info("JE " + je.getAsString());
}


Jason L. Buberel
ja...@buberel.org


Reply all
Reply to author
Forward
0 new messages