简体   繁体   English

在JavaScript中嵌套json解析

[英]nested json parsing in javascript

I am using the below code for REST OUTBOUND call and this is working as expected. 我正在使用以下代码进行REST OUTBOUND调用,并且此功能按预期工作。 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 请参阅示例json响应,我需要提取uid / name / cluster-member-names等对象下的值

the objects property is an array of objects. 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. 要从第一个对象的属性中获取数据,或者如果需要对所有返回的对象执行操作,则可以对其进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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