java - Parsing JSON by using gson -
{ "took": 6200, "timed_out": false, "_shards": { "total": 68, "successful": 68, "failed": 0 }, "hits": { "total": 110745094, "max_score": 1, "hits": [] }, "facets": { "pie": { "_type": "terms", "missing": 135, "total": 29349, "other": 26420, "terms": [ { "term": "165.130.136.210", "count": 390 }, { "term": "165.130.136.206", "count": 381 }, { "term": "205.138.114.8", "count": 359 }, { "term": "205.138.115.229", "count": 334 }, { "term": "165.130.136.208", "count": 331 }, { "term": "66.37.212.155", "count": 283 }, { "term": "209.67.71.137", "count": 279 }, { "term": "66.37.204.17", "count": 201 }, { "term": "64.28.92.213", "count": 193 }, { "term": "64.85.64.202", "count": 178 } ] } } }
i trying parse following, i've tried many api, using json in perl , json.simple, gson in java without luck.
what trying parse out facets=>pie=>terms=>term , count, can give me hint on how extract both values?
here have far
import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import com.google.gson.jsonarray; import com.google.gson.jsonelement; import com.google.gson.jsonobject; import com.google.gson.jsonparser; public class trueipmonitor { public static void main(string[] args) { string json = getjson(); system.out.println(json); system.out.println(parse(json)); } public static string parse(string jsonline) { jsonelement jelement = new jsonparser().parse(jsonline); jsonobject jobject = jelement.getasjsonobject(); jobject = jobject.getasjsonobject("facets"); jsonarray jarray = jobject.getasjsonarray("pie"); jobject = jarray.get(0).getasjsonobject(); string result = jobject.get("terms").tostring(); return result; } public static string getjson() { bufferedreader br; string json = ""; try { br = new bufferedreader(new filereader("script/curljson.log")); stringbuilder sb = new stringbuilder(); string line = br.readline(); while (line != null) { sb.append(line); sb.append('\n'); line = br.readline(); } json = sb.tostring(); try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return json; } } { "took" : 6200, "timed_out" : false, "_shards" : { "total" : 68, "successful" : 68, "failed" : 0 }, "hits" : { "total" : 110745094, "max_score" : 1.0, "hits" : [ ] }, "facets" : { "pie" : { "_type" : "terms", "missing" : 135, "total" : 29349, "other" : 26420, "terms" : [ { "term" : "165.130.136.210", "count" : 390 }, { "term" : "165.130.136.206", "count" : 381 }, { "term" : "205.138.114.8", "count" : 359 }, { "term" : "205.138.115.229", "count" : 334 }, { "term" : "165.130.136.208", "count" : 331 }, { "term" : "66.37.212.155", "count" : 283 }, { "term" : "209.67.71.137", "count" : 279 }, { "term" : "66.37.204.17", "count" : 201 }, { "term" : "64.28.92.213", "count" : 193 }, { "term" : "64.85.64.202", "count" : 178 } ] } } } exception in thread "main" java.lang.classcastexception: com.google.gson.jsonobject cannot cast com.google.gson.jsonarray @ com.google.gson.jsonobject.getasjsonarray(jsonobject.java:172) @ com.xxx.perf.monitor.trueipmonitor.parse(trueipmonitor.java:36) @ com.xxx.perf.monitor.trueipmonitor.main(trueipmonitor.java:28)
your json ok. here perl snippet prints out terms , counts:
...; $json = decode_json $input; $term (@{ $json->{facets}{pie}{terms} }) { printf "%15s: %s\n", @$term{qw/term count/}; }
output:
165.130.136.210: 390 165.130.136.206: 381 205.138.114.8: 359 205.138.115.229: 334 165.130.136.208: 331 66.37.212.155: 283 209.67.71.137: 279 66.37.204.17: 201 64.28.92.213: 193 64.85.64.202: 178
the problem in java code pie
entry not point json array, rather contains json object.
facets : object `- pie : object `- terms : array
your wanted (untested, , debatable style):
public static string parse(string jsonline) { jsonobject root = new jsonparser().parse(jsonline).getasjsonobject(); jsonarray terms = root.getasjsonobject("facets").getasjsonobject("pie").getasjsonarray("terms") jsonoject firstterm = terms.get(0).getasjsonobject(); string result = firstterm.get("terms").tostring(); return result; }
Comments
Post a Comment