admin管理员组文章数量:1394167
I have a json value converted from array in ajax response.
{"Text":"Please provide a value","Email":"Please provide a value"}
I need the response json to be extracted in a div using $(div).html():
Text-Please provide a value
Email-Please provide a value
I tried it using $.each():
var json=JSON.stringify({"Text":"Please provide a value","Email":"Please provide a value"});
var arr = [];
var obj = jQuery.parseJSON(json);
$.each(json,function(key,value)
{
arr.push('<li>'+value+'</li>');
});
var arrval =arr.join('');
console.log(arrval);
But I got output as undefined. I know there is some logical mistake I have done while extracting? What have I done wrong in here??
Note: The array key value pair in json can be dynamic ie., it may be one, two or more..
I have a json value converted from array in ajax response.
{"Text":"Please provide a value","Email":"Please provide a value"}
I need the response json to be extracted in a div using $(div).html():
Text-Please provide a value
Email-Please provide a value
I tried it using $.each():
var json=JSON.stringify({"Text":"Please provide a value","Email":"Please provide a value"});
var arr = [];
var obj = jQuery.parseJSON(json);
$.each(json,function(key,value)
{
arr.push('<li>'+value+'</li>');
});
var arrval =arr.join('');
console.log(arrval);
But I got output as undefined. I know there is some logical mistake I have done while extracting? What have I done wrong in here??
Note: The array key value pair in json can be dynamic ie., it may be one, two or more..
Share Improve this question asked Sep 12, 2013 at 9:05 Ganesh BabuGanesh Babu 3,67011 gold badges35 silver badges67 bronze badges 2-
6
You are iterating over the original string. You should write
$.each(obj, ...)
instead of$.each(json, ...)
. – Frédéric Hamidi Commented Sep 12, 2013 at 9:07 - @FrédéricHamidi Thanks for showing correct way. Your suggestion worked... – Ganesh Babu Commented Sep 12, 2013 at 9:18
4 Answers
Reset to default 1I think this might be helpful
var json = {"Text":"Please provide a value","Email":"Please provide a value"};
var arr = [];
$.each(json,function(key,value){
arr.push('<li>'+value+'</li>');
});
var arrval =arr.join('');
console.log(arrval);
see this if it helps !!
var jsonData = {"Text":"Please provide a value","Email":"Please provide a value"};
var arr = [];
for(var j in jsonData){
var sub_key = j;
var sub_val = eval("jsonData."+j);
arr.push('<li>'+sub_key+": "+sub_val+'</li>');
}
$(obj).each(function(index,value)
{
arr.push('<li>'+value.Text+'</li>');
});
your json have to look like:
[{"Text":"Please provide a value","Email":"Please provide a value"}]
json is an array of objects, thus has a length property that you can use to iterate its elements.
var result;
for(var i=0;i<json.data.length;i++)
{
result += json.data[i].text+ ', ';
}
Most debugger consoles support displaying objects directly. Just check your result using
console.log(obj);
本文标签: javascriptHow to extract Array from JSON in ajax responseStack Overflow
版权声明:本文标题:javascript - How to extract Array from JSON in $.ajax response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744774198a2624525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论