如何从JSON stringify中提取元素的值? 下面是示例JSON字符串。
{"resultCount":3,"results":[{"value":"abc","Content-Disposition":"form-data","name":"\"appKey\""},{"value":"123","Content-Disposition":"form-data","name":"\"userName\""},{"value":"test1","Content-Disposition":"form-data","name":"\"password\""}]}
我想从上面的json字符串中提取以下值 ABC 123 test1
答案 0 :(得分:1)
JSON.stringify的“对立面”是JSON.parse。它将返回一个具有该结构的JavaScript对象。为了得到你的字符串,你想做:
var data = JSON.parse("... your string ...");
data.results.map(function(obj) { return obj["value"]; });
答案 1 :(得分:0)
您可以获取数组中的值,也可以将它们放在单独的变量中。
$(document).ready(function() {
getLocation(function(data) {
var city = data.city;
getWeather(city).then(function(data) {
// ...
});
});
});
function getLocation(callback) {
$.getJSON('http://ipinfo.io', function(data) {
callback(data);
});
}
function getWeather(place) {
return $.getJSON('http://api.openweathermap.org/data/2.5/weather?q=' + place + '&units=imperial&APPID=90d625c068e3f3d7818b9e4237871e21');
}
答案 2 :(得分:0)
假设var x包含整个JSON stringify解析的值,如var x = JSON.parse [{},{}]
然后提取您可以使用的值:
for(i=0; i<x.results.length; i++){
console.log(x.results[i].value);
}