java - How to parse a complex json using gson or jackson -


the json string looks this:

[     {          "metric payload": [             {                 "metadata": [                     {                         "fieldname": "proc_proc_id",                         "datatype": "uint32",                         "label": "pid",                         "unit": "n/a"                     },                     {                         "fieldname": "proc_user_name",                         "datatype": "string",                         "label": "user name",                         "unit": "n/a"                     }                 ],                 "instances": [                     {                         "proc_interest": "m",                         "proc_cpu_total_util": "0.0",                         "gbl_active_cpu": "2"                                             }                 ]             }         ]     } ] 

i want parse , extract value "proc_cpu_total_util" present inside "instances" array. tried below code, fails exception saying, not json object.

    public string getmetrics(string jsonout) {             string result = null;             try {                  jsonelement jelement = new jsonparser().parse(jsonout);                          jsonobject  jobject = jelement.getasjsonobject();                 jobject = jobject.getasjsonobject("");                 jsonarray jarray = jobject.getasjsonarray("instances");                  jobject = jarray.get(0).getasjsonobject();                 result = jobject.get("gbl_mem_avail").tostring();                 system.out.println(result);                } catch (exception e) {                 // todo: handle exception                 system.err.println(e);             }             return result;           } 

the outer brackets misformated in example. i'm not sure that's root cause of problem json object encapsulated within twirly brackets. lose outermost square brackets start...


Comments