Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

javascript - 解析JSON输出并提取JavaScript中的值[重复](Parse JSON output and extract value in javascript [duplicate])

My json output from service now is(我现在从服务输出的json是)

[{"errno": "0","num_keys": "0"},
 {"errno": "1","num_keys": "2"},
 {"errno": "3","num_keys": "4"},
 {"errno": "5","num_keys": "6"}]

In this, i want to extract each value if keys errno and num_keys.(在这种情况下,如果键errno和num_keys,我想提取每个值。)

I am using the below code:(我正在使用以下代码:)

var request = new sn_ws.RESTMessageV2();
request.setEndpoint("url");
request.setHttpMethod('GET');
request.setRequestHeader('Content-Type','application/json');
request.setRequestHeader('X-IPM-Username','some name');
request.setRequestHeader('X-IPM-Password','some password');
var response = request.execute();
var result=response.getBody();

Here how to extract the values?(这里如何提取值?)

var result=response.json();
  ask by impika translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simple parse :(简单解析:)

var json = '{"myjsons":[{"errno": "0","num_keys": "0"},{"errno": "2","num_keys": "3"}]}';


var jsonData = JSON.parse(json);
for (var i = 0; i < jsonData.myjsons.length; i++) {
    var myjsons= jsonData.myjsons[i];
    console.log(myjsons.errno);
     console.log(myjsons.num_keys);
}

Also like this :(也像这样:)

var arr =  [{"errno": "0","num_keys": "0"},
 {"errno": "1","num_keys": "2"},
 {"errno": "3","num_keys": "4"},
 {"errno": "5","num_keys": "6"}];

arr.forEach(function(value){
  console.log('errno: ' + value.errno);
  console.log('num_keys: ' + value.num_keys);

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...