我收到这个来自ajax电话的回复。我的问题是如何获取对象的值?
{"countries":[{"country_id":730,"country":"El Salvador"},{"country_id":756,"country":"Guatemala"},{"country_id":767,"country":"Indonesia"}]}
我的ajax电话是这样的:
$.ajax({
type: "POST",
dataType:"json",
url:getCountriesPackages,
data: "transferUrl=countries?service_id="+serviceId,
success: function(data) {
$.each(data, function(k, v) {
console.log(k);
});
}
});
谢谢
答案 0 :(得分:0)
您已经拥有JSON,可以使用$.each()
遍历它:
var obj = {
"countries": [{
"country_id": 730,
"country": "El Salvador"
}, {
"country_id": 756,
"country": "Guatemala"
}, {
"country_id": 767,
"country": "Indonesia"
}]
};
$.each(obj, function(k, v) {
$.each(v, function(kk, kv) {
console.log("Country ID: " + kv.country_id);
console.log("Country: " + kv.country);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
您的回复是一个JavaScript对象。我们通过点运算符访问属性。我们返回的第一个属性是数组类型。您可以遍历数组并访问其数据。每个元素的属性。
key.get(use_cache=False)