JSon基于应用于其他节点的条件选择节点?

时间:2013-07-28 08:25:45

标签: java json solr jackson

我有这个Json:

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "fl":"solr_url, solr_date",
      "indent":"true",
      "q":"solr_body:party",
      "wt":"json"}},
  "response":{"numFound":19,"start":0,"docs":[
      {
        "solr_date":"2013-06-19T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/urbanissues"]},
      {
        "solr_date":"2013-07-27T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/outsideedge"]},
      {
        "solr_date":"2013-07-27T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/outsideedge/entry/in-defense-of-advani"]},
      {
        "solr_date":"2013-07-25T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/serendipity"]},
      {
        "solr_date":"2013-07-26T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/Ragtime"]},
      {
        "solr_date":"2013-07-24T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/SilkStalkings"]},
      {
        "solr_date":"2013-07-28T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/RaisinaWatch"]},
      {
        "solr_date":"2013-07-25T13:48:02Z",
        "solr_url":["http://blogs.economictimes.indiatimes.com/Cursor/entry/the-unbearable-lightness-of-advani-s-rebellion"]},
      {
        "solr_date":"2013-07-30T13:48:02Z",
        "solr_url":["http://blogs.reuters.com/great-debate/2013/06/17/the-real-irs-scandal/"]},
      {
        "solr_date":"2013-07-29T13:48:02Z",
        "solr_url":["http://blogs.reuters.com/johncabell"]}]
  }}

我正在使用杰克逊进行解析。现在的问题是我想根据solr_date选择solr_url。这在DOM解析中听起来很简单,但在Json中发现它真的很难。假设如果日期大于7月18日(我已经完成了日期逻辑),请选择solr_url。你能帮我解决这个问题吗?

  ObjectMapper mapper = new ObjectMapper();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
      JsonNode node = mapper.readTree(new URL("json output url"));

1 个答案:

答案 0 :(得分:1)

你也可以这样做:

var docs=json.response.docs;
var max=json.response.numFound;
var urls=[];

for(var i=0;i<max;i++){
    if(myDateLogicFunc(docs[i].solr_date))
            urls[]=docs[i].solr_url[0];
}

您的日期逻辑匹配的网址现在存储在网址数组中。

它并不比dom解析困难,“难点”是处理数组或对象。