0

I have the following Json data (Please see the image). I am trying to extract the data using the below jQuery code and it's not working. Please help.

var items = [];
$.each($.parseJSON(OnePointData), function (key, val) {
    items.push("<li id='" + key + "'>" + val + "</li>");
    $.each(key, function(key2, val2) {
        items.push("<li id='" + key2 + "'>" + val2 + "</li>");
    });
});
console.log(items);

Here is an image of my console:

enter image description here

And here is additional code from my implementation:

   $.getJSON("/Home/GetOnePointData", { Address: ui.item.value },function(OnePointData) {
       console.log(OnePointData);
       var items = [];
       var items2 = [];
       $.each($.parseJSON(OnePointData), function (key, val) {
            items.push("<li id='" + key + "'>" + val + "</li>");
            $.each(key, function (key2, val2) {
                items2.push("<li id='" + key2 + "'>" + val2 + "</li>");
            });
       });
       console.log(items2);
  });

If i run the loop once then i get the following result.

enter image description here

The data is in the following format:

{"GetQuestions":{"s1":"Q1,Q2","s2":"Q1,Q2,Q3","s3":"Q1,Q2,Q4","s4":"Q1,Q2,Q4,Q6"},
"GetQuestions2":{"s1":"Q1,Q2","s2":"Q1,Q2,Q3","s3":"Q1,Q2,Q4","s4":"Q1,Q2,Q4,Q6"}}
Ragnar
  • 4,393
  • 1
  • 27
  • 40
Asad Syed
  • 9
  • 4
  • 1
    Please provide enough data to replicate. Also if this is within ajax call show full context. Also what errors are thrown? – charlietfl Sep 21 '15 at 21:39
  • 1
    sounds like you are trying to parse an object and not a string. Show more code. – epascarello Sep 21 '15 at 21:40
  • I am returning Json data from MVC controller using the following code: return Json(f, JsonRequestBehavior.AllowGet); .... Added more code in the above section. – Asad Syed Sep 21 '15 at 21:49
  • You don't need to use $.parseJSON() function, because OnePointData is already an object. – Ragnar Sep 21 '15 at 21:53
  • `$.getJSON` calls `$.parseJSON` automatically. That's the whole difference between `$.get` and `$.getJSON`. – Barmar Sep 21 '15 at 21:57
  • When i removed this i got the following error message: Uncaught TypeError: Cannot use 'in' operator to search for '11' in GetQuestions – Asad Syed Sep 21 '15 at 22:10
  • `$.each(key` doesn't make sense. Providing real data would have also helped...easy enough to stringify to console – charlietfl Sep 21 '15 at 22:12

1 Answers1

0

As @Barmar says $.getJSON calls $.parseJSON automatically. So, just try removing $.parseJSON function:

$.getJSON("/Home/GetOnePointData", { Address: ui.item.value}, function(OnePointData) {
    console.log(OnePointData);
    var items = [];
    var items2 = [];
    $.each(OnePointData, function (key, val) {
        items.push("<li id='" + key + "'>" + val + "</li>");
        $.each(val, function (key2, val2) {
           items2.push("<li id='" + key2 + "'>" + val2 + "</li>");
        });
    });
    console.log(items2);
});

This is an example using data structure posted by you and it works.

var OnePointData = {"GetQuestions":{"s1":"Q1,Q2","s2":"Q1,Q2,Q3","s3":"Q1,Q2,Q4","s4":"Q1,Q2,Q4,Q6"},
                    "GetQuestions2":{"s1":"Q1,Q2","s2":"Q1,Q2,Q3","s3":"Q1,Q2,Q4","s4":"Q1,Q2,Q4,Q6"}};
var items = [];
var items2 = [];
$.each(OnePointData, function (key, val) {
    items.push("<li id='" + key + "'>" + val + "</li>");
    $.each(val, function (key2, val2) {
       items2.push("<li id='" + key2 + "'>" + val2 + "</li>");
    });
});
console.log(items);
console.log(items2);

Output:

["<li id='GetQuestions'>[object Object]</li>", "<li id='GetQuestions2'>[object Object]</li>"]

["<li id='s1'>Q1,Q2</li>", "<li id='s2'>Q1,Q2,Q3</li>", "<li id='s3'>Q1,Q2,Q4</li>", "<li id='s4'>Q1,Q2,Q4,Q6</li>", "<li id='s1'>Q1,Q2</li>", "<li id='s2'>Q1,Q2,Q3</li>", "<li id='s3'>Q1,Q2,Q4</li>", "<li id='s4'>Q1,Q2,Q4,Q6</li>"]
Ragnar
  • 4,393
  • 1
  • 27
  • 40
  • When i removed it, i got the following error message "Uncaught TypeError: Cannot use 'in' operator to search for '11' in GetQuestions" – Asad Syed Sep 21 '15 at 22:09
  • The error occurs in the second $.each() call? – Ragnar Sep 21 '15 at 22:13
  • Yes it occurs in the second call...When i use the first call it only grabs the Object "GetQuestions". I would like to get all values within that block....Please see the updated image for the results in the first call. – Asad Syed Sep 21 '15 at 22:19