Java Complex JSON 解析(变量字段)



我有一个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":["=&quot;mailto:hello@hellodesign.com&quot;  title=&quot;<b>hello</b>@hellodesign.com&quot;&gt;<b>hello</b>@hellodesign.com&lt;/a&gt;"]},
    "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&#8217;t they look familiar. Vaguely so. &#8220;<b>Hello</b>..&#8221; 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":[" - &lt;a href=&quot;http://insanityprone.tumblr.com/&quot;  title=&quot;<b>Hello</b>...&quot;&gt;<b>Hello</b>...&lt;/a&gt; - &lt;a href=&quot;http://insanityprone.tumblr.com/ask&quot;  title=&quot;Yes?&quot;&gt;Yes?&lt;/a&gt; - &lt;a href=&quot;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: &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <b>Hello</b> &#160;Contributor Story None &#160;From &#160; ... &#160;Comments by &#160; John &#160;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: &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <b>Hello</b> &#160;Contributor Story None &#160;Legal Advisory The photos below are copyright protected. You"]},
    "http://seoul-arts.123.st/f268-hello":{
      "title":["<b>HELLO</b>!"],
      "fulltxt":["<b>HELLO</b>! &#160; &#10085;S-A-U&#160;::&#160; ANNYEONG!&#160;::&#160; <b>HELLO</b>! &#160;! &amp; !&#160; &#160;Atsakymai&#160; &#160;Autorius&#160; &#160;Per&#382;i&#363;r&#279;jo&#160; &#160;Paskutinis prane&#353;imas&#160; !: MEET&#160; [ 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&#363;s negalite ra&#353;yti nauj&#371; prane&#353;im&#371; &#303; &#353;&#303; forum&#261; J&#363;s negalite atsakin&#279;ti &#303; prane&#353;imus &#353;iame forume &#160;&#10085;S-A-U&#160;::&#160;ANNYEONG!&#160;::&#160;<b>HELLO</b>! Vartotojai",
        ", per&#382;i&#363;rintys &#353;&#303; forum&#261;: Nieko &#160;&#10085;S-A-U&#160;::&#160;ANNYEONG!&#160;::&#160;<b>HELLO</b>! Pereiti &#303;:&#160;Pasirinkite forum&#261;||--ANNYEONG!|&#160;&#160;&#160;|--<b>HELLO</b>!|&#160;&#160;&#160;|--TAISYKL&#278;S|&#160;&#160;&#160;|--INFORMACIN&#278; D&#278;&#381;UT&#278;|&#160;&#160;&#160;|--NAUJOKO MENIU!|&#160;&#160;&#160;|--&#352;vieslent&#279;|&#160;&#160;&#160;|--B&#363;kim"],
      "relatedLinks":[" - &lt;a href=&quot;http://seoul-arts.123.st/forum&quot;  title=&quot;&#10085;S-A-U&quot;&gt;&#10085;S-A-U&lt;/a&gt; - &lt;a href=&quot;http://seoul-arts.123.st/c1-annyeong&quot;  title=&quot;ANNYEONG!&quot;&gt;ANNYEONG!&lt;/a&gt; - &lt;a href=&quot;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 &#160; <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":[" - &lt;a href=&quot;http://www.edugeek.net/groups/south-west-techs/178-<b>hello</b>/?groupid=21?&amp;styleid=38&quot;  title=&quot;Full Theme&quot;&gt;Full Theme&lt;/a&gt; - &lt;a href=&quot;http://www.edugeek.net/forums/&quot;  title=&quot;Forum&quot;&gt;Forum&lt;/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 &amp; 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",
        "&#160;::&#160;Earth Realm&#160;::&#160;Introductions Page 1 of 2&#160;&#8226;&#160;Share&#160;&#8226;&#160; Page 1 of 2 &#8226; 1, 2&#160;&#160; &#160;&#160; <b>Hello</b> !! &#160;by&#160;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":[" - &lt;a href=&quot;http://www.gatewaytoroleplay.com/t1271-<b>hello</b>&quot;  title=&quot;<b>Hello</b> !!&quot;&gt;<b>Hello</b> !!&lt;/a&gt; - &lt;a href=&quot;http://www.gatewaytoroleplay.com/forum&quot;  title=&quot;Gateway to Role-Play&quot;&gt;Gateway to Role-Play&lt;/a&gt; - &lt;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 &gt; Forum &gt; SAY <b>HELLO</b>! &gt; <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 &#160; Kat Evans Server Start Ltd 0 Member Since: 11th Nov 2013"],
      "relatedLinks":["&gt; - &lt;a href=&quot;http://www.4networking.biz/forum/ViewForum/4&quot;  title=&quot;SAY <b>HELLO</b>!&quot;&gt;SAY <b>HELLO</b>!&lt;/a&gt; - &lt;a href=&quot;http://www.4networking.biz/Members/Details/102151&quot;  title=&quot;Matt Reeds&quot;&gt;Matt Reeds&lt;/a&gt; - &lt;a href"]},
    "http://www.sysaid.com/Sysforums/posts/list/9104.page":{
      "title":["<b>Hello</b>"],
      "fulltxt":["<b>Hello</b> &#160;&#160;&#160;&#160; Home &#160;&#160; Forums &#160;&#160; Feature Requests &#160;&#160; Beta Issues &#160;&#160; SysAid Resources &#160;&#160; Documentation &#160;&#160; Support <b>Hello</b> Guest,&#160; Login &#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DOWNLOAD FREE EDITION &#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160; Recent Topics &#160;&#160; Hottest",
        " Topics &#160;&#160; Online Members &#160;&#160; Member Listing &#160;&#160; Advanced Search <b>Hello</b> &#160; Forum Index &#187; General IT Discussions &#160; Author Message 10/12/2012 15:28:32 &#160;&#160; &#160;Subject: <b>Hello</b> Sveta Bondarenko SysAider Joined: 06/12"],
      "relatedLinks":["=396CBD1CE052DE09A50AB73C38C2D542&quot;  title=&quot;<b>Hello</b>&quot;&gt;<b>Hello</b>&lt;/a&gt; - &lt;a href=&quot;http://www.sysaid.com/Sysforums/forums/list.page;jsessionid=396CBD1CE052DE09A50AB73C38C2D542&quot;  title=&quot;Forum Index&quot;&gt;Forum Index&lt;/a&gt; - &lt;a href=&quot;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

对应于

responseHeaderinnerObject没有名为title的字段。生产线上会发生什么innerObject.getJSONArray("title");

最新更新