Reputation: 33
I have an output in JSON which comes as below. I have tried parsing it with a lot of programs (Program also below) but it all fails, Primarily because I think that the name of the child of "highlighting" element is not fixed, it is a changable URL. The URLs get displayed in the same order in the "response" field above that section..Any advice would be highly appreciated.
JSON code --
{
"responseHeader":{
"status":0,
"QTime":1386,
"params":{
"fl":"urlFull",
"indent":"true",
"q":"hello",
"wt":"json"}},
"response":{"numFound":2016,"start":0,"docs":[
{
"urlFull":"http://www.hellodesign.com/"},
{
"urlFull":"http://kadlot.com/"},
{
"urlFull":"http://insanityprone.tumblr.com/"},
{
"urlFull":"http://www.privatevoyeur.com/PSOctober13/am23043/index.html"},
{
"urlFull":"http://www.privatevoyeur.com/PSOctober13/am23043/qvindex.html"},
{
"urlFull":"http://seoul-arts.123.st/f268-hello"},
{
"urlFull":"http://www.edugeek.net/groups/south-west-techs/178-hello/"},
{
"urlFull":"http://www.gatewaytoroleplay.com/t1271-hello"},
{
"urlFull":"http://www.4networking.biz/forum/ViewPost/1078797"},
{
"urlFull":"http://www.sysaid.com/Sysforums/posts/list/9104.page"}]
},
"highlighting":{
"http://www.hellodesign.com/":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b> You need the Latest Flash Player to view this site. Click here to download Flash Click here if you know you have Flash Address 10305 Jefferson Blvd. Culver City, CA 90232 Telephone 310-839-4885",
" Fax 310-839-4886 Email New Business [email protected] Press Inquiries [email protected] Career Inquiries [email protected] General Inquiries <b>hello</b>@hellodesign.com"],
"relatedLinks":["="mailto:[email protected]" title="<b>hello</b>@hellodesign.com"><b>hello</b>@hellodesign.com</a>"]},
"http://kadlot.com/":{
"title":["<b>Hello</b>!"],
"fulltxt":["<b>Hello</b>! <b>Hello</b>! Sooo... I didn't have much time to give you a prezi with nice graphics, but I thought you could help me out* with the drawings at least! So I made this site you can draw on! Try it and"]},
"http://insanityprone.tumblr.com/":{
"title":["<b>Hello</b>..."],
"fulltxt":["<b>Hello</b>... <b>Hello</b>... ~ And welcome to my facilities. Yes, mine. Not Her's. Mine. If you're curious about anything, anything at all, feel free to ask me. I've been here for a while now; I've picked a few",
" they came into view. Well didn’t they look familiar. Vaguely so. “<b>Hello</b>..” he offered a thin grin, just a sliver of those white teeth, hoping to eminate a false sense of friendliness. While he did like"],
"relatedLinks":[" - <a href="http://insanityprone.tumblr.com/" title="<b>Hello</b>..."><b>Hello</b>...</a> - <a href="http://insanityprone.tumblr.com/ask" title="Yes?">Yes?</a> - <a href="http://insanityprone.tumblr.com/submit"]},
"http://www.privatevoyeur.com/PSOctober13/am23043/index.html":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b> EscapeTrail PrivateVoyeur InstantPost Archive CamFuze PrivateVoyeur Proudly Presents:               <b>Hello</b>  Contributor Story None  From   ...  Comments by   John  Legal Advisory The photos"]},
"http://www.privatevoyeur.com/PSOctober13/am23043/qvindex.html":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b> EscapeTrail PrivateVoyeur InstantPost Archive CamFuze PrivateVoyeur Proudly Presents:               <b>Hello</b>  Contributor Story None  Legal Advisory The photos below are copyright protected. You"]},
"http://seoul-arts.123.st/f268-hello":{
"title":["<b>HELLO</b>!"],
"fulltxt":["<b>HELLO</b>!   ❥S-A-U ::  ANNYEONG! ::  <b>HELLO</b>!  ! & !   Atsakymai   Autorius   Peržiūrėjo   Paskutinis pranešimas  !: MEET  [ Pereiti prie: 1, 2, ... , 5, 6] 83 KIM JAEJOONG 365 Today at 7:27 pm PARK BOM",
": Nieko Moderatorius: STARS Permissions in this forum: Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume  ❥S-A-U :: ANNYEONG! :: <b>HELLO</b>! Vartotojai",
", peržiūrintys šį forumą: Nieko  ❥S-A-U :: ANNYEONG! :: <b>HELLO</b>! Pereiti į: Pasirinkite forumą||--ANNYEONG!|   |--<b>HELLO</b>!|   |--TAISYKLĖS|   |--INFORMACINĖ DĖŽUTĖ|   |--NAUJOKO MENIU!|   |--Švieslentė|   |--Būkim"],
"relatedLinks":[" - <a href="http://seoul-arts.123.st/forum" title="❥S-A-U">❥S-A-U</a> - <a href="http://seoul-arts.123.st/c1-annyeong" title="ANNYEONG!">ANNYEONG!</a> - <a href="http://seoul-arts.123.st/f268-<b>hello</b>"]},
"http://www.edugeek.net/groups/south-west-techs/178-hello/":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b> Full Theme Forum Today's Posts Search   <b>Hello</b> jimmy_2k 19th June 2010 10:59 AM Though i'd say hi, i'm from a school in Bristol SW-ICT 18th November 2011 10:21 AM Same!! Started off in Plymouth"],
"relatedLinks":[" - <a href="http://www.edugeek.net/groups/south-west-techs/178-<b>hello</b>/?groupid=21?&styleid=38" title="Full Theme">Full Theme</a> - <a href="http://www.edugeek.net/forums/" title="Forum">Forum</a"]},
"http://www.gatewaytoroleplay.com/t1271-hello":{
"title":["<b>Hello</b> !!"],
"fulltxt":["<b>Hello</b> !! Gateway to Role-Play A friendly community with various genres and freedom in role-play! Member of the Month & Year: MethosArchaic Hey! Listen! If GtR has 100,000 overall posts on the board",
" by the end of this year, Leah7 will dye her hair to popular vote, HikariKuragari will dye her hair red, and Venom will dye his hair the color of popular vote on the forum! <b>Hello</b> !! Gateway to Role-Play",
" :: Earth Realm :: Introductions Page 1 of 2 • Share •  Page 1 of 2 • 1, 2      <b>Hello</b> !!  by Ange Tuteur on Mon Nov 11, 2013 3:56 am <b>Hello</b> I'm Ange Tuteur! I'll be honest I've never ever ever role played! So"],
"relatedLinks":[" - <a href="http://www.gatewaytoroleplay.com/t1271-<b>hello</b>" title="<b>Hello</b> !!"><b>Hello</b> !!</a> - <a href="http://www.gatewaytoroleplay.com/forum" title="Gateway to Role-Play">Gateway to Role-Play</a> - <a"]},
"http://www.4networking.biz/forum/ViewPost/1078797":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b>\n Remember Me | Password reminder | New Member? Login Member login Shiny new version of #4N Meeting Finder App out now for Apple and Android http://t.co/4KDSZdwZH5 Home What's 4Networking",
" forum Today's posts Last 24 hours Search forum Articles Meet the 4N Team FREE networking Home > Forum > SAY <b>HELLO</b>! > <b>Hello</b> <b>Hello</b> Matt Reeds Frost Group Limited 0 Member Since: 11th Nov 2013 Telephone",
": Today at 04:18 Quote <b>Hello</b>, I am also new is 4networking to find some new frined. View Profile Send Message Leave Testimonial Find Posts   Kat Evans Server Start Ltd 0 Member Since: 11th Nov 2013"],
"relatedLinks":["> - <a href="http://www.4networking.biz/forum/ViewForum/4" title="SAY <b>HELLO</b>!">SAY <b>HELLO</b>!</a> - <a href="http://www.4networking.biz/Members/Details/102151" title="Matt Reeds">Matt Reeds</a> - <a href"]},
"http://www.sysaid.com/Sysforums/posts/list/9104.page":{
"title":["<b>Hello</b>"],
"fulltxt":["<b>Hello</b>      Home    Forums    Feature Requests    Beta Issues    SysAid Resources    Documentation    Support <b>Hello</b> Guest,  Login             DOWNLOAD FREE EDITION           Recent Topics    Hottest",
" Topics    Online Members    Member Listing    Advanced Search <b>Hello</b>   Forum Index » General IT Discussions   Author Message 10/12/2012 15:28:32     Subject: <b>Hello</b> Sveta Bondarenko SysAider Joined: 06/12"],
"relatedLinks":["=396CBD1CE052DE09A50AB73C38C2D542" title="<b>Hello</b>"><b>Hello</b></a> - <a href="http://www.sysaid.com/Sysforums/forums/list.page;jsessionid=396CBD1CE052DE09A50AB73C38C2D542" title="Forum Index">Forum Index</a> - <a href="http"]}},
"spellcheck":{
"suggestions":[]}}
Java code ------
JSONObject outerObject = new JSONObject(json);
JSONObject innerObject = outerObject.getJSONObject("responseHeader");
JSONArray jsonArray = innerObject.getJSONArray("title");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
JSONObject objectInArray = jsonArray.getJSONObject(i);
// "...and get thier component and thier value."
String[] elementNames = JSONObject.getNames(objectInArray);
System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
for (String elementName : elementNames)
{
String value = objectInArray.getString(elementName);
System.out.printf("name=%s, value=%s\n", elementName, value);
}
System.out.println();
}
Upvotes: 3
Views: 355
Reputation: 43053
Try this way:
JSONObject outerObject = new JSONObject(json);
JSONObject innerObject = outerObject.getJSONObject("highlighting");
for(Object key : innerObject.keySet()) {
JSONObject o =innerObject.getJSONObject(key.toString());
String[] elementNames = JSONObject.getNames(o);
System.out.printf("%s >> %d ELEMENT(S) IN CURRENT OBJECT:\n", key, elementNames.length);
for (String elementName : elementNames) {
System.out.println(elementName);
String value = o.get(elementName).toString();
System.out.printf("name=%s, value=%s\n", elementName, value);
}
System.out.println();
}
SAMPLE OUTPUT
http://www.hellodesign.com/ >> 3 ELEMENT(S) IN CURRENT OBJECT: relatedLinks name=relatedLinks, value=["="mailto:[email protected]" title="[email protected]">[email protected]</a>"] title name=title, value=["Hello"] fulltxt name=fulltxt, value=["Hello You need the Latest Flash Player to view this site. Click here to download Flash Click here if you know you have Flash Address 10305 Jefferson Blvd. Culver City, CA 90232 Telephone 310-839-4885"," Fax 310-839-4886 Email New Business [email protected] Press Inquiries [email protected] Career Inquiries [email protected] General Inquiries [email protected]"] ...
highlighting
is not a child of responseHeader
, so I get right from the read json. Later I use the keySet()
method to get the urls inside highlighting
.
Upvotes: 1
Reputation: 472
innerObject
which corresponds to responseHeader
does not have a field called title
. What happens on the line innerObject.getJSONArray("title");
?
Upvotes: 0