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
Add a ment  | 

4 Answers 4

Reset to default 1

I 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