JSON结果仅包含一项 [英] JSON result containing only one item
问题描述
我可能会用json和javascript丢失某些内容.
I'm likely missing something with json and javascript.
[{"commentText":"Testing 123","userPosted":"maxfridbe"},
{"commentText":"Testing 23","userPosted":"maxfridbe"}]
有时候我会收到多个与此代码兼容的响应:
Sometimes I get multiple responses which works with this code:
function(data)
{
var sel = this;
jQuery.each(data,
function()
{
sel.append("<li>"+ this.userPosted+ "-" + this.commentText + "</li>");
});
};
有时我只会收到一个破坏上面代码的响应:
Sometimes I only get one response which breaks the above code:
[{"commentText":"another test again welcom","userPosted":"maxfridbe"}]
我知道这是因为响应与列表的处理方式有所不同.
I know this is because the response is being treated differently than a list.
寻找答案,我得到了一些解决方法.任何解决方案将不胜感激.
Looking for the answer to this I get a bit of a runaround. Any solution would be greatly appreciated.
推荐答案
使用CMS解决方案使我意识到数据只是某种程度上的字符串:
Playing around with CMS's solution made me realize that data was just a string somehow so:
if (!jQuery.isArray(data)) data = eval(data);
之所以起作用,是因为那时数据是一个对象.不知道为什么有多个结果会为您评估.
worked because then the data was an object. Not sure why when there are multiple results it does an eval for you.
这篇关于JSON结果仅包含一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!