简体   繁体   中英

Unable to get specific data from a JSON object

I am trying to extract specific data from a json response using org.json.JSONObject library Heres is my json response :

{
"facets": {
"application": [
  {
    "name": "38",
    "distribution": 1
  }
],
"node": [
  {
    "name": "frstlwardu03_05",
    "distribution": 1
  }
],
"area": [
  {
    "name": "x",
    "distribution": 1
  }
],
"company": [
  {
    "name": "war001",
    "distribution": 1
  }
]
},
"duObjects": [
 {
  "id": "TASK|TSK(ZRM760J)(000)(ZRM760JU00)(000)|ZSRPSRM000",
  "name": "TSK(ZRM760J)(000)(ZRM760JU00)(000)",
  "mu": "ZSRPSRM000",
  "label": "",
  "session": "ZRM760J|000",
  "sessionLabel": "SAP SRM Achats frais generaux execution",
  "uprocHeader": "ZRM760JU00|000",
  "uprocHeaderLabel": "Header for SRM760J",
  "uprocHeaderType": "CL_INT",
  "domain": "M",
  "domainLabel": "",
  "application": "38",
  "applicationLabel": "magasin",
  "highlightResult": {
    "name": "name",
    "word": "TSK"
  }
}
],
"totalCount": 1,
"pageSize": 10,
"pageCurrent": 1,
"pageNb": 1
}

Here is the method I used to convert the URL call to a jsonobject :

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException    
{    
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-     
8")));
  String jsonText = readAll(rd);
  JSONObject json = new JSONObject(jsonText);
  return json;
} finally {
  is.close();
}
}

When I call this method I am able to get the data in teh Duobject :

public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://frstmwarwebsrv.orsyptst.com:9000/duobject?      
searchString=TSK(ZRM760J)(000)(ZRM760JU00)
(000)&filterchecks=nameJob,nameWF,nameSWF,application,domain&p.index=0&p.size=10");
System.out.println(json.getJSONArray("duObjects"));


} 

Is there anyway I can extract only the name field of the DuObjects?

You can use

System.out.println(json.getJSONArray("duObjects").getJSONObject(0).getString("name"));

to get the name.

1 : your complete response is a JSON OBJECT

2 : if any element is written like

"some key name " : { " some value " }

this is a JSON Object

3 : if any element is writen like

 "some key name " :  " some value " 

this is value inside you json object which you can get by

jsonObject.getString("key name")

4 : if any element is writen like

"some key name " : [ " some value " ]

then this is a JSON Array and you have to take it in to a JSON ARRAY and then traverse its elements by

jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")

and then you can traverse the elements of the JSON ARRAY by

`jsonArrayObj.get(0);`

You can use Jackson libraries to covert to java. Jackson api provides annotation level and it automatically converts json to pojo object and object to json vice versa . refer this link. you can get good idea about this

http://wiki.fasterxml.com/JacksonSampleSimplePojoMapper

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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