0

I'm trying to parse data returned from $.post().

[{"id":"1","text":"USD"},
 {"id":"2","text":"CNY"},
 {"id":"3","text":"PHP"},
 {"id":"4","text":"AUD"},
 {"id":"5","text":"SGD"},
 {"id":"6","text":"JPY"}]

using this approach Jquery, looping over a json array

I did something like this:

$.post(
    base_url+'cgame/currency',
    { gameID: gameID },
    function(data) {
        $(this).html();
        $.each(data,function(idx, obj) {
            $(obj).each(function(key, value) {
                console.log(key + ": " + value);
            });
        });
    }
);

but it gives me the error:

Uncaught TypeError: Cannot use 'in' operator to search for '120' in [{"id":"2","text":"CNY"},{"id":"3","text":"PHP"},{"id":"4","text":"AUD"},{"id":"5","text":"SGD"},{"id":"6","text":"JPY"}]

I also tried:

$.post(
    base_url+'cgame/currency',
    { gameID: gameID },
    function(data) {
        $(this).html();
        $(data).each(function(idx, obj) {
            $(obj).each(function(key, value) {
                console.log(key + ": " + value);
            });
        });
    }
);

but it also gives me the error:

Uncaught Error: Syntax error, unrecognized expression: [{"id":"1","text":"USD"},{"id":"6","text":"JPY"}]

How should I be doing this?

1
  • 1
    I guess you need to parse the JSON-string (data) to a JSON object before you iterate over it. Try JSON.parse(data) and iterate over that instead. Commented Oct 21, 2013 at 7:44

4 Answers 4

2

Your data comes in the form of array so you need to loop through each index and then fetch the values. So you can replace this :-

 $(obj).each(function(key, value) {
     console.log(key + ": " + value);
 });

with :-

 $.each( obj, function( key) {
     console.log( obj[key].id + ':' + obj[key].text);
 });

or you can do this :-

 $.each( obj, function( key, value ) {
     console.log( value.id + ':' + value.text);
 });

key is the index of array. It will return 0,1,2..

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

1 Comment

yeah, thanks.. your second answer was also the thing i've found out from eggward and fibbe suggestions.
1

I think the argument passed to your success callback function is of type string. Change it to:

 function(data) {
var parsedData = JSON.parse(data);
    $(this).html();
    $.each(parsedData ,function(idx, obj) {
        $(obj).each(function(key, value) {
            console.log(key + ": " + value);
        });
    });
}

2 Comments

thank you for your effort :D ive already found the answer based on yours and the comment above my post.
this gives the output of 0: [object Object] grid.js:159 0: [object Object] grid.js:159 0: [object Object] grid.js:159 0: [object Object] grid.js:159 0: [object Object] grid.js:159 0: [object Object]. There is no error bu still values are not cuaght properly. So what i did is to remove the second loop of your code.
1
 function(data) {
    var value = JSON.parse(data);
    $.each(value ,function(idx, obj) {
       console.log("id : "+obj.id+" "+text:"+obj.text);
    });
 }

Comments

0

try this

$.post(base_url+'cgame/currency',{ gameID: gameID },
 function(data) {
    $(this).html(); //<-- becareful $(this) might not be the element you think it is
    $.each(data,function(idx, obj) {
        console.log( "id : " + obj.id);
        console.log( "text: " + obj.text);
   });
 },'JSON');

1 Comment

im sorry but still gives the second error on my post.but thanks for the help :D

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.