如何在JAVA中解析经过验证的JSON

时间:2014-12-09 06:23:08

标签: java json

我正在尝试解析以下JSON字符串,但我无法弄清楚如何获取标题nd href字符串。

任何人都可以帮助我吗?

{  
   "status":{  
      "code":200,
      "http":"Fetched (ring) 200 450 and parsed 1/10 entries",
      "nextFetch":1418084814,
      "entriesCountSinceLastMaintenance":1,
      "period":450,
      "lastFetch":1418084364,
      "lastParse":1418084364,
      "lastMaintenanceAt":1418017613,
      "feed":"http://www.ncataggies.com/rss.dbml?db_oem_id=24500&media=results"
   },
   "permalinkUrl":"http://www.ncataggies.com/",
   "standardLinks":{  
      "alternate":[  
         {  
            "title":"North Carolina A&T  Results",
            "rel":"alternate",
            "href":"http://www.ncataggies.com/",
            "type":"text/html"
         }
      ],
      "self":[  
         {  
            "title":"North Carolina A&T  Results",
            "rel":"self",
            "href":"http://www.ncataggies.com/rss.dbml?db_oem_id=24500&media=results",
            "type":"application/rss+xml"
         }
      ]
   },
   "title":"North Carolina A&T  Results",
   "updated":1418077800,
   "id":"north-carolina-a-t-results-2014-12-8-22",
   "items":[  
      {  
         "id":"http://www.ncataggies.com//SportSelect.dbml?DB_OEM_ID=24500&SPID=74519&SPSID=593466&Q_SEASON=2014&SCH_ID=5710847",
         "published":1418077800,
         "updated":1418077800,
         "title":"Women's Basketball: vs UMES (12/08/2014) - L (43-58)",
         "summary":"<p>at Princess Anne, Md. - 5:30 PM -\n\t\t\t\t\n\t\t\t\t</p>",
         "standardLinks":{  
            "self":[  
               {  
                  "title":"Women's Basketball: vs UMES (12/08/2014) - L (43-58)",
                  "rel":"self",
                  "href":"http://www.ncataggies.com//SportSelect.dbml?DB_OEM_ID=24500&SPID=74519&SPSID=593466&Q_SEASON=2014",
                  "type":"application/rss+xml"
               }
            ]
         }
      }
   ]
}

我对JSON没有多少经验,这是我到目前为止的JAVA:

    JSONObject obj = new JSONObject(json);
    JSONArray items = (JSONArray) obj.get("items");
    System.out.println(items.get(1).toString());

1 个答案:

答案 0 :(得分:0)

import org.json.*;

    JSONObject obj = new JSONObject(json);
    JSONArray items = (JSONArray) obj.get("items");

    for(int i=0;i<items.length;i++)
    {
      JSONObject item = items.getJSONObject(i);
      JSONObject standardLinks = item.getJSONObject("standardLinks");
      JSONArray self = standardLinks.getJSONArray("self");
      JSONObject selfItem = self.getJSONObject(0);
      System.out.println(selfItem.getString("href"));
      System.out.println(selfItem.getString("title"));  
    }

试试这个。我没有编译代码。如果有的话,请更正语法错误。