【发布时间】:2016-10-17 12:11:19
【问题描述】:
我有这样的 json/object
{city: "bangalore", other:"other data", pic: "bangalore.jpg",city: "delhi", pic: "delhi.jpg"}
我有 2 个城市及其图片。我想将它们附加到单独的 2 div 中。
这里我只是想遍历每个城市?但失败了我该怎么做?
$.ajax({
url: "mul.php",
type: "POST",
data: {
a: a
},
dataType: "text",
success: function(data) {
var obj = $.parseJSON(data);
console.log(obj.city); // i'm getting only delhi , what about bangalore?
$.each(obj.city, function(i, city) {
console.log(city); // i'm getting error
})
}
});
【问题讨论】:
-
在单个对象中,多个键不能具有相同的名称。像这样创建对象数组
[ { "city": "bangalore", "other": "other data", "pic": "bangalore.jpg" }, { "city": "delhi", "pic": "delhi.jpg" } ] -
好的,在那种情况下我如何循环它们
-
你可以知道哪个城市映射到哪个图片。您需要有一系列城市来为每个城市做一个