sam*_*her 4 javascript arrays ajax jquery json
我有下面的代码,我通过json传递给jquery.
$string['msg']['1']['Name'] = "John Doe";
$string['msg']['1']['Said'] = "Hello there";
$string['msg']['2']['Name'] = "Jane King";
$string['msg']['2']['Said'] = "Hello there";
$string['errors']['1']['Person'] = "Jane King";
$string['errors']['1']['type'] = "Wrong Screwdriver";
$string['errors']['2']['Person'] = "John Doe";
$string['errors']['2']['type'] = "Wrong Spanner";
Run Code Online (Sandbox Code Playgroud)
JSON输出如下:
{"msg":{"1":{"Name":"John Doe","Said":"Hello there"},"2":{"Name":"Jane King","Said":"Hello there"}},"errors":{"1":{"Person":"Jane King","type":"Wrong Screwdriver"},"2":{"Person":"John Doe","type":"Wrong Spanner"}}}
Run Code Online (Sandbox Code Playgroud)
这是正在检索json的ajax代码.
$.ajax({
dataType : 'json',
url: 'polling.php',
type: 'post',
data: 'id=1',
success: function(data) {
//read json
});
Run Code Online (Sandbox Code Playgroud)
我试图按照数字顺序读取所有'msg'.. 1,2 ..并获得每个的'Name'和'Said'值.
然后为'错误'做同样的事情
但我无法正确检索任何值
根据您的给定输出:
试试这个:
$(function(){
var data = {"msg":{"1":{"Name":"John Doe","Said":"Hello there"},"2":{"Name":"Jane King","Said":"Hello there"}},"errors":{"1":{"Person":"Jane King","type":"Wrong Screwdriver"},"2":{"Person":"John Doe","type":"Wrong Spanner"}}};
$.each(data.msg, function(index, value) {
alert(index + ': ' + value.Name);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17361 次 |
| 最近记录: |