我有一个JSON的输出,如下所示。我尝试过用很多程序解析它(程序也在下面),但都失败了,主要是因为我认为"突出显示"元素的子元素的名称不是固定的,它是一个可更改的 URL。URL 在该部分上方的"响应"字段中以相同的顺序显示。任何建议将不胜感激。
JSON代码 --
{
"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 newbusiness@hellodesign.com Press Inquiries press@hellodesign.com Career Inquiries careers@hellodesign.com General Inquiries <b>hello</b>@hellodesign.com"],
"relatedLinks":["="mailto:hello@hellodesign.com" 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 代码------
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=%sn", elementName, value);
}
System.out.println();
}
试试这个方式:
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=%sn", elementName, value);
}
System.out.println();
}
示例输出
http://www.hellodesign.com/>> 当前对象中的 3 个元素:相关链接名称=相关链接,值=["="mailto:hello@hellodesign.com" 标题="hello@hellodesign.com">hello@hellodesign.com"]标题名称=标题,值=["你好"]富特XTname=fulltxt, value=["您好,您需要最新的 Flash Player 才能查看此站点。点击这里下载闪存 点击这里,如果你知道你有闪存地址 10305杰斐逊大道。 卡尔弗城,CA 90232 电话 310-839-4885"," 传真 310-839-4886 电子邮件 新业务 newbusiness@hellodesign.com 新闻查询 press@hellodesign.com 职业查询 careers@hellodesign.com 一般查询 hello@hellodesign.com"]...
highlighting
不是responseHeader
的孩子,所以我从阅读的json中得到正确。稍后我使用 keySet()
方法获取 highlighting
中的 url
responseHeader
的innerObject
没有名为title
的字段。生产线上会发生什么innerObject.getJSONArray("title");
?