简体   繁体   中英

nested json parsing in javascript

I am using the below code for REST OUTBOUND call and this is working as expected. but i am trying to parse the second response body but i am unable to retrieve the values under objects.

try { 
 var r = new sn_ws.RESTMessageV2('test', 'post');

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
 gs.print(response.getBody());
 gs.print(response.getStatusCode());
 var JsonObject = JSON.parse(responseBody);
 var sid = JsonObject.sid; 
 gs.print(sid);

 var r1 = new sn_ws.RESTMessageV2('x_257605_test.gateways', 'POST' );
 r1.setRequestHeader("X-chkp-sid",sid );
 var response1 = r1.execute();
 var responseBody1 = response1.getBody();
 var httpStatus = response1.getStatusCode()
 gs.print(response1.getBody());
 var JsonObject1 = JSON.parse(responseBody1);
 gs.print(JsonObject1.objects.uid);
}
catch(ex) {
 var message = ex.message;
}

please see the sample json response and i need to extract the values under objects like uid/name/cluster-member-names

the objects property is an array of objects. This line should change from:

gs.print(JsonObject1.objects.uid);

to:

gs.print(JsonObject1.objects[0].uid);

To grab data from the first object's properties, or your could iterate over them if you need to perform an operation on all the objects returned.

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