Javascript / JSON alert returning Undefined

I am having a problem trying to alert the json data.

Here is the code part:

$.getJSON("http://myurl/api/get_data", function(data){
    var output = '';
    $.each(data, function(index, value){
    alert(data); // returns object
    alert(value.myurl) //Here is the problem - returns undefined
});

Here is the json data it's reading:

{"myurl":"http://somedomain.com/"}

How can I get the alert to return the value?

Answers


You are thinking too complicated. JSON stands for JavaScript Object Notations. It's basically the javascript syntax representation of the object it contains.

The value passed to the success handler of getJSON is already the unpacked object. So you can just do this:

$.getJSON("http://myurl/api/get_data", function(data){
    alert(data.myurl);
});

Your JSON text consists of a plain object. There is no array. There is no nested data structure. Get rid of all the each stuff.

$.getJSON("http://myurl/api/get_data", function(data){
    alert(data.myurl);
});

Need Your Help

Output not printing without fflush(stdout)

c io stdout fflush

I don't understand why sometimes I need to use fflush() and sometimes not.

On an iOS single view app, when is the earliest time that self.view.bounds is set correctly?

ios uiview uiviewcontroller

I am trying to add a UITableView object that covers up the whole screen, by

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.