我知道如何解析json并从java中获取json数组的结果,但我有一个json,其中我无法找出哪个是数组,以及如何解析它,在这个json中我想要'州'和'base_url'。有人可以帮助我如何做到这一点。
{
"tree": [{
"data": {
"title": "/clusterstate.json",
"attr": {
"href": "zookeeper?detail=true&path=%2Fclusterstate.json"
}
}
}],
"znode": {
"data": "{\n \"cmn2\":{\n \"shards\":{\n \"shard1\":{\n \"range\":\"80000000-ffffffff\",\n \"state\":\"active\",\n \"replicas\":{\n \"core_node1\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"cmn2_shard1_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\"},\n \"core_node3\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"cmn2_shard1_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\",\n \"leader\":\"true\"}}},\n \"shard2\":{\n \"range\":\"0-7fffffff\",\n \"state\":\"active\",\n \"replicas\":{\n \"core_node2\":{\n \"state\":\"active\",\n \"base_url\":\"mydomainmyurlforjson:8080/solr\",\n \"core\":\"cmn2_shard2_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\"},\n \"core_node4\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"cmn2_shard2_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\",\n \"leader\":\"true\"}}}},\n \"maxShardsPerNode\":\"1\",\n \"router\":{\"name\":\"compositeId\"},\n \"replicationFactor\":\"1\"},\n \"elect\":{\n \"shards\":{\n \"shard1\":{\n \"range\":\"80000000-ffffffff\",\n \"state\":\"active\",\n \"replicas\":{\n \"core_node3\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"elect_shard1_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\",\n \"leader\":\"true\"},\n \"core_node5\":{\n \"state\":\"active\",\n \"base_url\":\"mydomainmyurlforjson:8080/solr\",\n \"core\":\"elect_shard1_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\"}}},\n \"shard2\":{\n \"range\":\"0-7fffffff\",\n \"state\":\"active\",\n \"replicas\":{\n \"core_node4\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"elect_shard2_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\"},\n \"core_node6\":{\n \"state\":\"active\",\n \"base_url\":\"myurlforjson\",\n \"core\":\"elect_shard2_replica1\",\n \"node_name\":\"myurlforjson:8080_solr\",\n \"leader\":\"true\"}}}},\n \"maxShardsPerNode\":\"1\",\n \"router\":{\"name\":\"compositeId\"},\n \"replicationFactor\":\"1\"}}",
"prop": {
"pzxid": 4294967540,
"czxid": 4294967540,
"mzxid": 12884915656,
"mtime": "Thu Feb 20 09:00:48 UTC 2014 (1392886848018)",
"ephemeralOwner": 0,
"children_count": 0,
"cversion": 0,
"ctime": "Thu Dec 05 10:19:10 UTC 2013 (1386238750279)",
"aversion": 0,
"dataLength": 2388,
"version": 1119
},
"path": "/clusterstate.json"
}
}
这是我的代码,让我知道我在哪里解析
URL url = new URL("http://myipaddress:8080/solr/zookeeper? detail=true&path=%2Fclusterstate.json");
System.out.println(url);
java.net.URLConnection connection = url.openConnection();
//String ref = "http://ajax.googleapis.com";
//connection.addRequestProperty("Referer", ref);
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
//System.out.println(json);
JSONArray entries = json.getJSONArray("tree");
for (int i = 0; i < entries.length(); i++) {
JSONObject row = entries.getJSONObject(i);
bean test = new bean();
test.setRss(row.getString("data"));
System.out.println(row.getString("data"));
}
}
catch (Exception e) {
// TODO: handle exception
}
return "cloud";
}
答案 0 :(得分:0)
JSONObject znode = json.getJSONObject("znode");
String data = znode.getJSONObject("data");
JSONObject dataJson = new JSONObject(data);
从那里你自己 - 它看起来像cmn2 - &gt; shards -shard1 - &gt; state,et al,但是在这种形式下阅读JSON有点困难。我建议记录dataJson,这样你就可以更清楚地看到它。