简体   繁体   English

杰克逊JSON没有设置地图 <String, Object> 值

[英]Jackson JSON not setting Map<String, Object> Value

I am using Jackon Json library to parse JSON response returned by EclasticSearch. 我使用Jackon Json库来解析EclasticSearch返回的JSON响应。 Here is the JSON structure. 这是JSON结构。 You can see that "nodes" is a class and "mRyA6hF0QAqIvggAbFWBsw" is the NodeID which is dynamic and allocated by ES so I cannot make concrete nested class. 你可以看到“节点”是一个类,“mRyA6hF0QAqIvggAbFWBsw”是动态的NodeID,由ES分配,所以我不能制作具体的嵌套类。 I am using Map<String, Nodes> for it. 我正在使用Map<String, Nodes>

{
  "ok" : true,
  "cluster_name" : "elasticsearch",
  "nodes" : {
    "mRyA6hF0QAqIvggAbFWBsw" : {
      "name" : "Fagin",
      "transport_address" : "inet[/192.16.12.18:9300]",
      "hostname" : "anand-K53SD",
      "version" : "0.90.9",
      "http_address" : "inet[/192.16.12.18:9200]",
      "settings" : {
        "path.home" : "/home/anand/logmanagementtools/elasticsearch/elasticsearch-0.90.9",
        "name" : "Fagin",
        "cluster.name" : "elasticsearch",
        "path.logs" : "/home/anand/logmanagementtools/elasticsearch/elasticsearch-0.90.9/logs"
      },
      "os" : {
        "refresh_interval" : 1000,
        "available_processors" : 4,
        "cpu" : {
          "vendor" : "Intel",
          "model" : "Core(TM) i3-2350M CPU @ 2.30GHz",
          "mhz" : 2300,
          "total_cores" : 4,
          "total_sockets" : 4,
          "cores_per_socket" : 16,
          "cache_size" : "3kb",
          "cache_size_in_bytes" : 3072
        },
        "mem" : {
          "total" : "3.6gb",
          "total_in_bytes" : 3912163328
        },
        "swap" : {
          "total" : "5gb",
          "total_in_bytes" : 5369749504
        }
      },
      "process" : {
        "refresh_interval" : 1000,
        "id" : 5291,
        "max_file_descriptors" : 4096,
        "mlockall" : false
      },
      "jvm" : {
        "pid" : 5291,
        "version" : "1.7.0_45",
        "vm_name" : "Java HotSpot(TM) 64-Bit Server VM",
        "vm_version" : "24.45-b08",
        "vm_vendor" : "Oracle Corporation",
        "start_time" : 1390103541852,
        "mem" : {
          "heap_init" : "256mb",
          "heap_init_in_bytes" : 268435456,
          "heap_max" : "990.7mb",
          "heap_max_in_bytes" : 1038876672,
          "non_heap_init" : "23.1mb",
          "non_heap_init_in_bytes" : 24313856,
          "non_heap_max" : "130mb",
          "non_heap_max_in_bytes" : 136314880,
          "direct_max" : "990.7mb",
          "direct_max_in_bytes" : 1038876672
        }
      },
      "thread_pool" : {
        "generic" : {
          "type" : "cached",
          "keep_alive" : "30s"
        },
        "index" : {
          "type" : "fixed",
          "min" : 4,
          "max" : 4,
          "queue_size" : "200"
        },
        "get" : {
          "type" : "fixed",
          "min" : 4,
          "max" : 4,
          "queue_size" : "1k"
        },
        "snapshot" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 2,
          "keep_alive" : "5m"
        },
        "merge" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 2,
          "keep_alive" : "5m"
        },
        "suggest" : {
          "type" : "fixed",
          "min" : 4,
          "max" : 4,
          "queue_size" : "1k"
        },
        "bulk" : {
          "type" : "fixed",
          "min" : 4,
          "max" : 4,
          "queue_size" : "50"
        },
        "optimize" : {
          "type" : "fixed",
          "min" : 1,
          "max" : 1
        },
        "warmer" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 2,
          "keep_alive" : "5m"
        },
        "flush" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 2,
          "keep_alive" : "5m"
        },
        "search" : {
          "type" : "fixed",
          "min" : 12,
          "max" : 12,
          "queue_size" : "1k"
        },
        "percolate" : {
          "type" : "fixed",
          "min" : 4,
          "max" : 4,
          "queue_size" : "1k"
        },
        "management" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 5,
          "keep_alive" : "5m"
        },
        "refresh" : {
          "type" : "scaling",
          "min" : 1,
          "max" : 2,
          "keep_alive" : "5m"
        }
      },
      "network" : {
        "refresh_interval" : 5000,
        "primary_interface" : {
          "address" : "192.16.12.18",
          "name" : "wlan0",
          "mac_address" : "00:08:CA:F3:F6:AB"
        }
      },
      "transport" : {
        "bound_address" : "inet[/0:0:0:0:0:0:0:0:9300]",
        "publish_address" : "inet[/192.16.12.18:9300]"
      },
      "http" : {
        "bound_address" : "inet[/0:0:0:0:0:0:0:0:9200]",
        "publish_address" : "inet[/192.16.12.18:9200]",
        "max_content_length" : "100mb",
        "max_content_length_in_bytes" : 104857600
      },
      "plugins" : [ {
        "name" : "paramedic",
        "description" : "No description found for paramedic.",
        "url" : "/_plugin/paramedic/",
        "jvm" : false,
        "site" : true
      }, {
        "name" : "bigdesk",
        "description" : "No description found for bigdesk.",
        "url" : "/_plugin/bigdesk/",
        "jvm" : false,
        "site" : true
      }, {
        "name" : "head",
        "description" : "No description found for head.",
        "url" : "/_plugin/head/",
        "jvm" : false,
        "site" : true
      }, {
        "name" : "HQ",
        "description" : "No description found for HQ.",
        "url" : "/_plugin/HQ/",
        "jvm" : false,
        "site" : true
      } ]
    }
  }
}

Here is my code to parse this JSON 这是我解析这个JSON的代码

package com.esmon.pojo;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;

public class NodeInfo {
    private String ok;
    private String cluster_name;
    private Nodes nodes;

    public Nodes getNodes() {
        return nodes;
    }
    public void setNodes(Nodes nodes) {
        this.nodes = nodes;
    }
    public String getOk() {
        return ok;
    }
    public void setOk(String ok) {
        this.ok = ok;
    }
    public String getCluster_name() {
        return cluster_name;
    }
    public void setCluster_name(String cluster_name) {
        this.cluster_name = cluster_name;
    }

    @SuppressWarnings("rawtypes")
    public void printInfo() {
        System.out.println("------Nodes Information------");
        System.out.println("Ok: " + ok);
        System.out.println("Cluster Name: " + cluster_name);
        nodes.printnfo();
    }
}

class Nodes {
private Map<String , NodeObject> allNodes = new HashMap<String , NodeObject>();

    @JsonAnyGetter
    public Map<String , NodeObject> any() {
        return allNodes;
    }

    @JsonAnySetter
    public void set(String name, NodeObject value) {
        allNodes.put(name, value);
    }

    @SuppressWarnings("rawtypes")
    public void printnfo() {
        System.out.println(allNodes.size());
        for (Map.Entry entry: allNodes.entrySet()) {
            System.out.println("Node ID: " + entry.getKey());
        }
    }
}
class NodeObject {
    private String name;
    private String transport_address;
    private String hostname;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTransport_address() {
        return transport_address;
    }
    public void setTransport_address(String transport_address) {
        this.transport_address = transport_address;
    }
    public String getHostname() {
        return hostname;
    }
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }
}

When i check the Map size its returning 0. Please point where I am doing wrong. 当我检查地图大小时它返回0.请指出我做错了。

The problem is that your Java Nodes class contains a field that is a Map<String, NodeObject> . 问题是您的Java Nodes包含一个Map<String, NodeObject> 字段 Your JSON defines "nodes" as an object ( Map<String, NodeObject> ). 您的JSON将“节点”定义对象( Map<String, NodeObject> )。

Your NodeInfo class should define: 您的NodeInfo类应该定义:

private Map<String, NodeObject> nodes;

And you can scrap the Nodes class. 你可以废弃Nodes类。 Once you've done that, the following will work 完成后,以下内容将起作用

ObjectMapper mapper = new ObjectMapper();
NodeInfo info = mapper.readValue(jsonString, NodeInfo.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM