I have some JSON data that I need to parse and show in a table. I can not get it to parse the data though. I can use Firebug and see that the javascript is returning the JSON data but it's not parsing it and adding it to my simple unordered list.
JSON Layout:
[{"id":111,"Name":"This is a test","Availability":null,"Description":"Some description here...","URL":"http://example.url/","Vendor":null},{"id":111,"Name":"This is a test","Availability":"yes","Description":"Some description here...","URL":"http://example.url/","Vendor":"Joe's Books"}]
This is what I have so far with my HTML/Javascript:
<html>
<head>
<title>DB API Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "https://10.1.1.100/api/db/",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
type: "GET",
data: { },
success: function (data) {
data = data.sort(function (a, b) {
return a.Name > b.Name;
});
var liStr = "";
$.each(data, function (i, item) {
liStr += "<li>" + item.Name + "</li>";
});
$("#Results").html(liStr);
}
});
});
</script>
</head>
<body>
<p>This is a test.</p>
<ul id="Results"></ul>
</body>
</html>
I have some javascript in there to alphabetize the list before spitting it out. Not sure if it's correct either. I just keep getting a blank list.
I did a sample testing in Firebug with your code modified, and its working,
check the code below and modify ur code approrpiately:
var data = [{"id":111,"Name":"This is a test","Availability":null,"Description":"Some description here...","URL":"http://example.url/","Vendor":null},{"id":111,"Name":"It is a test","Availability":"yes","Description":"Some description here...","URL":"http://example.url/","Vendor":"Joe's Books"}];
function getSorted(arr) {
arr = arr.sort(function (a, b) {
return a.Name[0] > b.Name[0];
});
return arr;
}
var sortedData = getSorted(data);
sortedData; //or console.log()
maddtechwf
Member
17 Points
147 Posts
Javascript not parsing my JSONP data like it should.
Feb 22, 2013 08:11 PM|LINK
I have some JSON data that I need to parse and show in a table. I can not get it to parse the data though. I can use Firebug and see that the javascript is returning the JSON data but it's not parsing it and adding it to my simple unordered list.
JSON Layout:
[{"id":111,"Name":"This is a test","Availability":null,"Description":"Some description here...","URL":"http://example.url/","Vendor":null},{"id":111,"Name":"This is a test","Availability":"yes","Description":"Some description here...","URL":"http://example.url/","Vendor":"Joe's Books"}]This is what I have so far with my HTML/Javascript:
<html> <head> <title>DB API Test</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $.ajax({ url: "https://10.1.1.100/api/db/", contentType: "application/json; charset=utf-8", dataType: "jsonp", type: "GET", data: { }, success: function (data) { data = data.sort(function (a, b) { return a.Name > b.Name; }); var liStr = ""; $.each(data, function (i, item) { liStr += "<li>" + item.Name + "</li>"; }); $("#Results").html(liStr); } }); }); </script> </head> <body> <p>This is a test.</p> <ul id="Results"></ul> </body> </html>I have some javascript in there to alphabetize the list before spitting it out. Not sure if it's correct either. I just keep getting a blank list.
raju dasa
Star
14412 Points
2452 Posts
Re: Javascript not parsing my JSONP data like it should.
Feb 24, 2013 05:48 AM|LINK
Hi,
I did a sample testing in Firebug with your code modified, and its working,
check the code below and modify ur code approrpiately:
var data = [{"id":111,"Name":"This is a test","Availability":null,"Description":"Some description here...","URL":"http://example.url/","Vendor":null},{"id":111,"Name":"It is a test","Availability":"yes","Description":"Some description here...","URL":"http://example.url/","Vendor":"Joe's Books"}]; function getSorted(arr) { arr = arr.sort(function (a, b) { return a.Name[0] > b.Name[0]; }); return arr; } var sortedData = getSorted(data); sortedData; //or console.log()rajudasa.blogspot.com || blog@opera