首页 新闻 会员 周边

求助JSON遍历

0
悬赏园豆:10 [已解决问题] 解决于 2014-06-04 16:11

<div id="tbody1">
</div>

$.ajax({
asyc: false,
url: 'mytest.ashx',
type: 'Get',
crossDomain: true,
dataType: 'JSON',
cache: false,
success: function (data) {

//能够接收到返回数据就是这里遍历不对

$.each(data.comments, function (i, item) {
$("#tbody1").append(item.Title);

});
}
});

 

下面是json格式

[
{
"ID": 149714,
"Title": "Title",
"Url": "Url",
"Pic": "Pic",
"Contents": "Contents",
"Type": "Type",
"Time": "Time",
"UpdateTime": "/Date(1398648486453)/",
"Record_MD5_ID": "Record_MD5_ID",
"One": "One",
"Two": "Two",
"Three": "Three",
"pinglun": "pinglun",
"city": "city"
},
{
"ID": 149715,
"Title": " <P id=nv><A href=\"http://news.baidu.com\">新&nbsp;闻</A> <B>网&nbsp;页</B> <A href=\"http://tieba.baidu.com\">贴&nbsp;吧</A> <A href=\"http://zhidao.baidu.com\">知&nbsp;道</A> <A href=\"http://music.baidu.com\">音&nbsp;乐</A> <A href=\"http://image.baidu.com\">图&nbsp;片</A> <A href=\"http://v.baidu.com\">视&nbsp;频</A> <A href=\"http://map.baidu.com\">地&nbsp;图</A></P>",
"Url": null,
"Pic": null,
"Contents": null,
"Type": null,
"Time": null,
"UpdateTime": "/Date(1388104921050)/",
"Record_MD5_ID": null,
"One": null,
"Two": null,
"Three": null,
"pinglun": null,
"city": null
}
]

 

才学习的,求指教啊具体怎么遍历,在线等!谢谢了

You最温暖的港湾的主页 You最温暖的港湾 | 初学一级 | 园豆:103
提问于:2014-06-04 14:05
< >
分享
最佳答案
0

没猜错你在success里的data应该还是字符串而不是json对象,你需要把json字符串解析成json对象

收获园豆:10
吴瑞祥 | 高人七级 |园豆:29449 | 2014-06-04 14:21

是,我改了下降对象转换了,不过还不行啊

修改后:

$.ajax({
asyc: false,
url: 'mytest.ashx',
type: 'Get',
crossDomain: true,
dataType: 'JSON',
cache: false,
success: function (data) {
data = $.parseJSON(data);
$.each(data,function(item) {
$("#tbody1").append(item.Title);
});
}
});

You最温暖的港湾 | 园豆:103 (初学一级) | 2014-06-04 14:28

@You最温暖的港湾: 你断点在$.each(data,function(item) {

然后看上面的data是什么值.用的谷歌浏览器的话可以直接在控制台测试

吴瑞祥 | 园豆:29449 (高人七级) | 2014-06-04 14:30

@吴瑞祥:唉原来是我在一般应用程序里将查的数据转换成json格式了。我的

//data = $.parseJSON(data);不需要了。谢谢了

办法:

$.ajax({
asyc: false,
type: "Get",
url: "mytest.ashx",
dataType: "json",
data: { rows: 10 }, //参数
cache: false,
success: function (data) {
//data = $.parseJSON(data);不需要了我后台已经转成json了
$.each(data, function (index, item) {
$("#tbody1").append(item.Title); //遍历
})
}
});

You最温暖的港湾 | 园豆:103 (初学一级) | 2014-06-04 16:10
其他回答(2)
0


dataType: 'json',

刘宏玺 | 园豆:14020 (专家六级) | 2014-06-04 14:31
0

dataType: 'JSON',  data 应该是 json对象,不用转换,调试一下 就知道了

Qlin | 园豆:2403 (老鸟四级) | 2014-06-04 14:32

嗯。谢谢

支持(0) 反对(0) You最温暖的港湾 | 园豆:103 (初学一级) | 2014-06-04 16:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册