1

I get the following string:

[{"Key":1,"Value":"correct"},{"Key":2,"Value":"incorrect"},{"Key":3,"Value":"incorrect"},{"Key":4,"Value":"correct"},{"Key":5,"Value":"incorrect"}]

I want to change the background color of my TR based on whether the ID of that TR has a value of "correct" or "incorrect" in the JSON.

How can I get the value of an individual item from JSON? I have tried:

            success: function (result) {
                // update with results
                //alert(JSON.stringify(result));
                //$('#myDiv').html(JSON.stringify(result));

                // non of these work
                alert(result[0]);
                alert(result[key]);
            },
1
  • What output do you get ? Or what error ? Commented Oct 8, 2012 at 16:22

5 Answers 5

2

You can use $.each() to iterate the Array of Objects.

$.each(result, function(i, item) {
    alert(item.Key);
});

In the callback to $.each(), item will be a reference to the current item in the Array being iterated.

Then you just use normal property access to get the value for the Key property.


Of course you could also use a traditional for statement to loop the Array.

for (var i = 0; i < result.length; i++) {
    var item = result[i];
    alert(item.Key);
}

All of this assumes your response has the proper Content-Type setting, or you've give dataType:"json" to $.ajax().

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

Comments

1

You can convert your JSON format to Object, in your situation it will be array, so you can use forEach to check what you want.

Try this

var obj= JSON.parse( '[{"Key":1,"Value":"correct"},{"Key":2,"Value":"incorrect"},{"Key":3,"Value":"incorrect"},{"Key":4,"Value":"correct"},{"Key":5,"Value":"incorrect"}]' );



obj.forEach(function(i){alert(i.Key);alert(i.Value) })

Comments

1

Have a look at this: http://goessner.net/articles/JsonPath/.I have not used it but looks like and good way to get values from a Json structure.

Comments

0

You don't specify, but assuming that JSON string is what your ajax code is receiving as the response, then you're actually re-JSONing that text, so it becomes a double-encoded string.

jquery can auto-decode that back to a native structure for you, if you tell is that you're expecting json as a response, e.g.

$.ajax({
   dataType: 'json',
   etc...
});

then you'd simply have

alert(result[0]['key']);

or

data = jquery.parseJSON(result);
alert(data[0]['key']);

2 Comments

Then the ajax call is failing, and your success code block is never called. add an error block to catch that and see why things aren't working.
No, the success block is called because I am outputting the entire JSON string via $('#myDiv').html(JSON.stringify(result));. The problem is with the code inside of the success block.
0

Make sure you specify .. dataType:'json' in your Ajax Request

Try

alert(result[0]["key"]);  // For the first

//

$.each(result , function(i) {
    console.log( 'Key is - ' + result[i]["Key"] + ' -- Value is - ' + result[i]["Value"]);
});

Check FIDDLE

3 Comments

I tried using alert(result[0]["key"]); and alert(result[0]["Key"]); but it just says, "undefined."
Try console.log(result) .. What do you see
I am rushed so I will have to continue with the working solution of using $.each but thanks for your help.

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.