0

我有这个 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"));
4

1 回答 1

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];
}

您的 url 与您的日期逻辑匹配现在存储在 urls 数组中。

其实并不比dom解析难,“难点”就是处理数组或者对象。

于 2013-07-28T08:41:41.447 回答