0

Can anyone please tell me how I can access this JSON data with JavaScript? I tried it wit the $.each loop, tried accessing with [0] but everything returned undefined.

JSON data from controller:

{
    "workers": {
        "107": "Lisa",
        "96": "Marvin",
        "24": "Michael",
        "23": "Patrick",
        "116": "Peter"
    }
}

Ajax Call:

$.ajax({
    url: '/cake/workers/getWorkersAsJSON.json',
    async: true,
    success: function(data) {
        // process
    }
});

6 Answers 6

1

You need to add dataType: 'json'

$.ajax({
    url: '/cake/workers/getWorkersAsJSON.json',
    async: true,
    dataType: 'json',
    success: function(data) {
       $.each(data.workers, function(i, v) {
          console.log(i + ' : ' + v);
       })
    }
});

Specifying the datatype as JSON will then convert it to a JavaScript object and you can iterate through it with $.each or call out items directly by using a . as a separator. The example above works.

FYI: you don't need to specify async: true because it's true by default.

Sign up to request clarification or add additional context in comments.

1 Comment

I already specify the JSON extension when I call the method of the controller. Both is not necessary. Your solution worked, thanks for that! I am accepting it now!
1

There's actually a jQuery short-hand of the $.ajax datatype:json. The function is called getJSON

In your example, it would be implemented like this:

$.getJSON( "/cake/workers/getWorkersAsJSON.json", function( data ) {
 console.log(data);
});

That would get the JSON, and the proceed to log the retrieved data as an array into Console.

Source: http://api.jquery.com/jquery.getjson/

Comments

1

Hi please check the below code. You are sending object so have to access data using keys.

$.ajax({
    url: '/cake/workers/getWorkersAsJSON.json',
    async: true,
    success: function(data) {
        console.log(data.workers.107) // should show "Lisa"
        //if you need to use loop, try these
        var keys = Object.keys(data.workers);

        for(var key in keys){ 
          console.log(data.workers[key]);  
        }
    }
});

1 Comment

Thanks Sohel but this still showed undefined in the console.
0

Try

var obj = $.parseJSON(data);

in your success function.

Comments

0

It doesn't looks like an array so not sure it could be processed with 'each' loop (please see JSON array definition here). Most probably it would be accessible as

data.workers.107

If not simplest way to understand structure is to set breakpoint in ajax callback and check structure of JavaScript object.

1 Comment

This seems like the correct solution, if your server side is serving it with content type JSON, it should be automatically deserialized into data object as a JSON object.
0

Using the open source project jinqJs

You could make a sync call like this to get the JSON object:

var result = jinqJs().from('/cake/workers/getWorkersAsJSON.json').select();

an async way to access it:

var result;
    jinqJs().from('/cake/workers/getWorkersAsJSON.json', function(self) {
         result = self.select();
});

4 Comments

If he's already using jQuery, what would the benefits of using jinqJs be?
Because he can then make further LINQ expressions on it, like, groupBy(), .where(), etc...
Why would somebody not using .net care to use linq... Lol
The same reason why people use lodash and _underscore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.