[英]How extract array elements in json
I want to extract the array elements starting from "results" as shown in the following JSON response: 我想从“结果”开始提取数组元素,如以下JSON响应所示:
{
"bossresponse":{
"responsecode":"200",
"web":{
"start":"0",
"count":"42",
"totalresults":"89200000",
"results":[
{
"date":"",
"clickurl":"http:\/\/www.yahoo.com\/",
"url":"http:\/\/www.yahoo.com\/",
"dispurl":"www.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>!",
"abstract":"Welcome to <b>Yahoo<\/b>!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and ..."
},
{
"date":"",
"clickurl":"http:\/\/mail.yahoo.com\/",
"url":"http:\/\/mail.yahoo.com\/",
"dispurl":"mail.<b>yahoo<\/b>.com",
"title":"Sign in to <b>Yahoo<\/b>!",
"abstract":"Your World. Your Way. <b>Yahoo<\/b>! makes it easy to enjoy what matters most in your world. Best in class <b>Yahoo<\/b>! Mail, breaking local, national and global news, finance ..."
},
{
"date":"",
"clickurl":"http:\/\/groups.yahoo.com\/?ch=web&pub=fp-us&t=fp&sec=link&slk=defaultrough",
"url":"http:\/\/groups.yahoo.com\/?ch=web&pub=fp-us&t=fp&sec=link&slk=defaultrough",
"dispurl":"groups.<b>yahoo<\/b>.com\/?ch=web&pub=fp-us&t=fp&sec=link&slk=...",
"title":"<b>Yahoo<\/b>! Groups - Join or create groups, clubs, forums ...",
"abstract":"<b>Yahoo<\/b>! Groups offers free mailing lists, photo & file sharing, group calendars and more. Discuss hot topics, share interests, join online communities."
},
{
"date":"",
"clickurl":"http:\/\/homes.yahoo.com\/",
"url":"http:\/\/homes.yahoo.com\/",
"dispurl":"homes.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Homes - Home for Sale and Rent, Makeovers, Design and ...",
"abstract":"Some <b>Yahoo<\/b>! Homes information is provided by our trusted partners. However, <b>Yahoo<\/b>! does not guarantee the accuracy or completeness of any third party content."
},
{
"date":"",
"clickurl":"http:\/\/en.wikipedia.org\/wiki\/Yahoo",
"url":"http:\/\/en.wikipedia.org\/wiki\/Yahoo",
"dispurl":"en.wikipedia.org\/wiki\/<b>Yahoo<\/b>",
"title":"<b>Yahoo<\/b>! - Wikipedia, the free encyclopedia",
"abstract":"<b>Yahoo<\/b>! Inc. (NASDAQ: YHOO) is an American multinational internet corporation headquartered in Sunnyvale, California, United States. The company is best known for its ..."
},
{
"date":"",
"clickurl":"http:\/\/taxes.yahoo.com\/",
"url":"http:\/\/taxes.yahoo.com\/",
"dispurl":"taxes.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Finance - Personal Finance | Taxes",
"abstract":"Taxes - Read latest articles and guides, try our comprehensive calculators, tools, and get advice at <b>Yahoo<\/b>! Finance"
},
{
"date":"",
"clickurl":"http:\/\/omg.yahoo.com\/",
"url":"http:\/\/omg.yahoo.com\/",
"dispurl":"omg.<b>yahoo<\/b>.com",
"title":"omg! Celebrity gossip, news photos, babies, couples, hotties ...",
"abstract":"The latest celebrity gossip from <b>Yahoo<\/b>! omg! Get the latest news and gossip on the Hollywood celebrities with videos and photos."
},
{
"date":"",
"clickurl":"http:\/\/uk.yahoo.com\/",
"url":"http:\/\/uk.yahoo.com\/",
"dispurl":"uk.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! UK",
"abstract":"Welcome to <b>Yahoo<\/b>!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and ..."
},
{
"date":"",
"clickurl":"http:\/\/smallbusiness.yahoo.com\/?s_fptrough=ysb_acq_smallbiz_20081001",
"url":"http:\/\/smallbusiness.yahoo.com\/?s_fptrough=ysb_acq_smallbiz_20081001",
"dispurl":"smallbusiness.<b>yahoo<\/b>.com\/?s_fptrough=ysb_acq_smallbiz...",
"title":"<b>Yahoo<\/b>! Small Business: Web Hosting, Domain Registration, E ...",
"abstract":"<b>Yahoo<\/b>! Small Business provides reliable web hosting for your website, domain name registration, web site design templates, e-commerce solutions for your ..."
},
{
"date":"",
"clickurl":"http:\/\/cricket.yahoo.com\/",
"url":"http:\/\/cricket.yahoo.com\/",
"dispurl":"cricket.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Cricket - Live Cricket Score | Cricket News | Cricket ...",
"abstract":"Check out live cricket scores, cricket news headlines, cricket schedules & results and more from <b>Yahoo<\/b>! Cricket Includes ICC World Cricket League Championship 2012 ..."
},
{
"date":"",
"clickurl":"http:\/\/messenger.yahoo.com\/?i=1",
"url":"http:\/\/messenger.yahoo.com\/?i=1",
"dispurl":"messenger.<b>yahoo<\/b>.com\/?i=1",
"title":"<b>Yahoo<\/b>! Messenger - Chat, Instant message, SMS, Video Call, PC ...",
"abstract":"<b>Yahoo<\/b>! Messenger - Chat with friends for free and enjoy stealth settings, video calls, PC calls, file sharing, photo sharing, SMS, emoticons, & more."
},
{
"date":"",
"clickurl":"http:\/\/uk.my.yahoo.com\/",
"url":"http:\/\/uk.my.yahoo.com\/",
"dispurl":"uk.my.<b>yahoo<\/b>.com",
"title":"My <b>Yahoo<\/b>! - <b>Yahoo<\/b>! UK",
"abstract":"My <b>Yahoo<\/b>! is a customisable web page from <b>Yahoo<\/b>! UK. Personalise your page by adding news, stock quotes, weather, email, social updates and much more."
},
{
"date":"",
"clickurl":"http:\/\/search.yahoo.com\/",
"url":"http:\/\/search.yahoo.com\/",
"dispurl":"search.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Search - Web Search",
"abstract":"Learn about the new <b>Yahoo<\/b>! Axis. A better way to search. Way better. Get it Now! >>"
},
{
"date":"",
"clickurl":"http:\/\/news.yahoo.com\/",
"url":"http:\/\/news.yahoo.com\/",
"dispurl":"news.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! News - Latest News & Headlines",
"abstract":"The latest news and headlines from <b>Yahoo<\/b>! News. Get breaking news stories and in-depth coverage with videos and photos."
},
{
"date":"",
"clickurl":"http:\/\/au.yahoo.com\/",
"url":"http:\/\/au.yahoo.com\/",
"dispurl":"au.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>!7",
"abstract":"The <b>Yahoo<\/b>! for Australia offering local & global search, free email, instant messaging, news, sport, travel, shopping, movies & music."
},
{
"date":"",
"clickurl":"http:\/\/ca.yahoo.com\/",
"url":"http:\/\/ca.yahoo.com\/",
"dispurl":"ca.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Canada",
"abstract":"Welcome to <b>Yahoo<\/b>! Canada, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest ..."
},
{
"date":"",
"clickurl":"http:\/\/everything.yahoo.com\/",
"url":"http:\/\/everything.yahoo.com\/",
"dispurl":"everything.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>!",
"abstract":"<b>Yahoo<\/b>! International ... © 2012 <b>Yahoo<\/b>! Inc. All rights reserved"
},
{
"date":"",
"clickurl":"http:\/\/football.fantasysports.yahoo.com\/?ovchn=YAH&ovcpn=Front+Page&ovcrn=Front+page+P+Link+Nav+button&ovrfd=YAH&ovtac=AD",
"url":"http:\/\/football.fantasysports.yahoo.com\/?ovchn=YAH&ovcpn=Front+Page&ovcrn=Front+page+P+Link+Nav+button&ovrfd=YAH&ovtac=AD",
"dispurl":"football.fantasysports.<b>yahoo<\/b>.com\/?ovchn=YAH&ovcpn=Front...",
"title":"It's Game Time! - <b>Yahoo<\/b>! Sports Fantasy Football",
"abstract":"<b>Yahoo<\/b>! Fantasy Football. Create or join a NFL league and manage your team with FREE live scoring, stats, scouting reports, news, and expert advice."
},
{
"date":"",
"clickurl":"http:\/\/music.yahoo.com\/",
"url":"http:\/\/music.yahoo.com\/",
"dispurl":"music.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Music - Exclusive New Music and Music Videos",
"abstract":"Find all the new music and exclusive video performances on <b>Yahoo<\/b>! Music. Get the latest news in our exclusive music blogs, listen to our online music radio and catch ..."
},
{
"date":"",
"clickurl":"http:\/\/uk.messenger.yahoo.com\/",
"url":"http:\/\/uk.messenger.yahoo.com\/",
"dispurl":"uk.messenger.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Messenger - Chat, Instant message, Video Call, PC Calls",
"abstract":"<b>Yahoo<\/b>! Messenger - Chat with friends for free and enjoy stealth settings, video calls, PC calls, file sharing, photo sharing, emoticons, & more."
},
{
"date":"",
"clickurl":"http:\/\/in.yahoo.com\/",
"url":"http:\/\/in.yahoo.com\/",
"dispurl":"in.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! India",
"abstract":"Welcome to <b>Yahoo<\/b>!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and ..."
},
{
"date":"",
"clickurl":"http:\/\/companion.yahoo.com\/",
"url":"http:\/\/companion.yahoo.com\/",
"dispurl":"companion.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Toolbar - Stay in touch with your world.",
"abstract":"Toolbar for <b>Yahoo<\/b>! search, alerts for <b>Yahoo<\/b>! Mail and an option to save bookmarks online."
},
{
"date":"",
"clickurl":"http:\/\/movies.yahoo.com\/photos\/",
"url":"http:\/\/movies.yahoo.com\/photos\/",
"dispurl":"movies.<b>yahoo<\/b>.com\/photos",
"title":"Movie Pictures | Movie Posters - <b>Yahoo<\/b>! Movies",
"abstract":"View movie pictures and stills from the latest releases on <b>Yahoo<\/b>! Movies. Find movie posters, red carpet photos and pictures from upcoming movies."
},
{
"date":"",
"clickurl":"http:\/\/local.yahoo.com\/",
"url":"http:\/\/local.yahoo.com\/",
"dispurl":"local.<b>yahoo<\/b>.com",
"title":"Redmond City Pages on <b>Yahoo<\/b>! Local. Find Businesses, Services ...",
"abstract":"<b>Yahoo<\/b>! Local has Redmond business reviews, top rated services, and events near Redmond, WA. Use interactive maps, driving directions reviews and ratings to find the ..."
},
{
"date":"",
"clickurl":"http:\/\/yahooligans.yahoo.com\/",
"url":"http:\/\/yahooligans.yahoo.com\/",
"dispurl":"<b>yahoo<\/b>ligans.<b>yahoo<\/b>.com",
"title":"Kids Games, Kids Movies, Kids Music, and More - <b>Yahoo<\/b>! Kids",
"abstract":"<b>Yahoo<\/b> Kids! is the ultimate web guide for kids! Features fun and educational resources for kids with games, animals, music, jokes, movies, news, astrology, TV ..."
},
{
"date":"",
"clickurl":"http:\/\/m.yahoo.com\/",
"url":"http:\/\/m.yahoo.com\/",
"dispurl":"m.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Mobile",
"abstract":"Welcome to <b>Yahoo<\/b>! Mobile. Stay up to date on the go with business, sports, and entertainment news."
},
{
"date":"",
"clickurl":"http:\/\/tv.yahoo.com\/",
"url":"http:\/\/tv.yahoo.com\/",
"dispurl":"tv.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! TV",
"abstract":"<b>Yahoo<\/b>! TV is your guide to everything about TV online, from TV show listings and episode guides, to the latest TV show photos, clips, news, recaps, cast & credits ..."
},
{
"date":"",
"clickurl":"http:\/\/shopping.yahoo.com\/",
"url":"http:\/\/shopping.yahoo.com\/",
"dispurl":"shopping.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Shopping - Online Shopping with great products, prices ...",
"abstract":"<b>Yahoo<\/b>! Shopping is the best place to read user reviews, explore great products and buy online."
},
{
"date":"",
"clickurl":"http:\/\/dir.yahoo.com\/",
"url":"http:\/\/dir.yahoo.com\/",
"dispurl":"dir.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Directory",
"abstract":"Help us improve the <b>Yahoo<\/b>! Directory - Share your ideas"
},
{
"date":"",
"clickurl":"http:\/\/overview.mail.yahoo.com\/",
"url":"http:\/\/overview.mail.yahoo.com\/",
"dispurl":"overview.mail.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Mail",
"abstract":"Your favorite email is now: FASTER: 2x faster. How great is that? So great. SAFER: Even better spam protection. Yes! EASIER: Clean, simple design that makes email a ..."
},
{
"date":"",
"clickurl":"http:\/\/travel.yahoo.com\/",
"url":"http:\/\/travel.yahoo.com\/",
"dispurl":"travel.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Travel - Cheap flights, hotels, travel guides & car rental",
"abstract":"Find cheap flights, hotels and car rentals with <b>Yahoo<\/b>! Travel. Plan your trip with travel guides, personalized recommendations, articles, deals and more."
},
{
"date":"",
"clickurl":"http:\/\/omg.yahoo.com\/news\/",
"url":"http:\/\/omg.yahoo.com\/news\/",
"dispurl":"omg.<b>yahoo<\/b>.com\/news",
"title":"Celebrity News | Celebrity Gossip - <b>Yahoo<\/b>! omg!",
"abstract":"Get the latest celebrity news from <b>Yahoo<\/b>! omg! Find the latest news about the Hollywood celebrities with videos and photos"
},
{
"date":"",
"clickurl":"http:\/\/games.yahoo.com\/",
"url":"http:\/\/games.yahoo.com\/",
"dispurl":"games.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Games - Play Free Online Games | Download Games",
"abstract":"Play the latest online games on <b>Yahoo<\/b>! Games. Play and download single and multiplayer games from a wide selection of arcade, board and puzzle games."
},
{
"date":"",
"clickurl":"http:\/\/omg.yahoo.com\/photos\/",
"url":"http:\/\/omg.yahoo.com\/photos\/",
"dispurl":"omg.<b>yahoo<\/b>.com\/photos",
"title":"Celebrity Photos | Pictures of Celebrities - <b>Yahoo<\/b>! omg!",
"abstract":"View the latest celebrity photos on <b>Yahoo<\/b>! omg! Get the latest red carpet pictures and photos from Hollywood."
},
{
"date":"",
"clickurl":"http:\/\/finance.yahoo.com\/q?s=",
"url":"http:\/\/finance.yahoo.com\/q?s=",
"dispurl":"finance.<b>yahoo<\/b>.com\/q?s=",
"title":"Quotes & Info- <b>Yahoo<\/b>! Finance",
"abstract":"View the basic stock chart on <b>Yahoo<\/b>! Finance. Change the date range, chart type and compare against other companies."
},
{
"date":"",
"clickurl":"http:\/\/movies.yahoo.com\/",
"url":"http:\/\/movies.yahoo.com\/",
"dispurl":"movies.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Movies - Upcoming Movies, Trailers and Showtimes",
"abstract":"View the latest trailers and movie reviews on <b>Yahoo<\/b>! Movies. Find everything about movies online, from upcoming releases, movie ratings, trailers and movie news"
},
{
"date":"",
"clickurl":"http:\/\/screen.yahoo.com\/",
"url":"http:\/\/screen.yahoo.com\/",
"dispurl":"screen.<b>yahoo<\/b>.com",
"title":"Watch videos online - <b>Yahoo<\/b>! Screen",
"abstract":"Watch videos online for free on <b>Yahoo<\/b>! Screen. Find sports, kids and funny videos including the latest news video clips."
},
{
"date":"",
"clickurl":"http:\/\/privacy.yahoo.com\/",
"url":"http:\/\/privacy.yahoo.com\/",
"dispurl":"privacy.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Privacy Policy",
"abstract":"WHAT THIS PRIVACY POLICY COVERS <b>Yahoo<\/b>! takes your privacy seriously. Please read the following to learn more about our privacy policy. The federal ..."
},
{
"date":"",
"clickurl":"http:\/\/news.yahoo.com\/us\/",
"url":"http:\/\/news.yahoo.com\/us\/",
"dispurl":"news.<b>yahoo<\/b>.com\/us",
"title":"U.S. News Headlines - <b>Yahoo<\/b>! News",
"abstract":"Get the latest U.S. news headlines from <b>Yahoo<\/b>! News. Find breaking U.S. news, including analysis and opinion on top U.S. stories."
},
{
"date":"",
"clickurl":"http:\/\/fantasysports.yahoo.com\/",
"url":"http:\/\/fantasysports.yahoo.com\/",
"dispurl":"fantasysports.<b>yahoo<\/b>.com",
"title":"Fantasy on <b>Yahoo<\/b>! Sports - News, Scores, Standings, Rumors ...",
"abstract":"Comprehensive Fantasy news, scores, standings, fantasy games, rumors, and more"
},
{
"date":"",
"clickurl":"http:\/\/finance.yahoo.com\/",
"url":"http:\/\/finance.yahoo.com\/",
"dispurl":"finance.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>! Finance - Business Finance, Stock Market, Quotes, News",
"abstract":"At <b>Yahoo<\/b>! Finance, you get free stock quotes, up to date news, portfolio management resources, international market data, message boards, and mortgage ..."
},
{
"date":"",
"clickurl":"http:\/\/world.yahoo.com\/",
"url":"http:\/\/world.yahoo.com\/",
"dispurl":"world.<b>yahoo<\/b>.com",
"title":"<b>Yahoo<\/b>!: <b>Yahoo<\/b>! Everywhere",
"abstract":"<b>Yahoo<\/b>! USA (en Español) ... © 2012 <b>Yahoo<\/b>! Inc. All rights reserved"
}
]
}
}
}
I used the following lines: 我使用了以下几行:
JsonParser jsonParser = new JsonParser();
JsonObject linksObj = jsonParser.parse(jsonReader).getAsJsonObject();
JsonArray linksArray = jsonParser.parse(linksObj.get("results").toString()).getAsJsonArray();
But I get an error: 但是我得到一个错误:
Exception in thread "main" java.lang.NullPointerException
at parseResponse.main(parseResponse.java:35)
When I tried to remove the text prior to "results"
, my code could successfully parse the JSON response. 当我尝试删除"results"
之前的文本时,我的代码可以成功解析JSON响应。 Can any body explain how can I deal with this problem. 谁能解释我该如何处理这个问题。 The program should be able to extract the array elements without I manually remove the lines as I did to debug. 该程序应该能够提取数组元素,而无需像调试那样手动删除行。
Try this code to get parse your JSON response. 尝试使用此代码来解析您的JSON响应。 you can obtain array at resultArray, 您可以在resultArray处获得数组,
try{
JSONObject obj = new JSONObject(JSONresponse);
JSONObject bossObj = obj.getJSONObject("bossresponse");
JSONObject webObj = obj.getJSONObject("web");
JSONArray resultArray obj = obj.getJSONArray("results");
for(i=0;i<=resultArray.length();i++){
JSONObject resultObject = resultArray.getJSONObject(i);
String date = resultObject.getString("date");
String clickurl = resultObject.getString("clickurl");
String url = resultObject.getString("url");
String dispurl= resultObject.getString("dispurl");
String title = resultObject.getString("title");
String abstract = resultObject.getString("abstract");
}
} catch (Exception ex) {
ex.printStackTrace();
}
It seems you need to read results from web attribute, not just wrapper top object, So: this is your linksObject
: 似乎您需要从Web属性读取结果,而不仅仅是包装顶层对象,因此:这是您的linksObject
:
JsonObject linksObj = jsonParser.parse(jsonReader).getAsJsonObject();
It should have bossresponse
attribut, and it is another object. 它应该具有bossresponse
属性,这是另一个对象。 from bossresponse
object you will have web
object from web
you will have results
object 来自bossresponse
对象,您将拥有web
对象,来自web
您将拥有results
对象
bossresponse->web->results
now you are kind a reading results
from first level. 现在,你是怎样的一个阅读results
从第一级。 I guess It would work if json is 我想如果json是可以的
{
results:[
{
//here attrs
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.