1

How to process JSON output in AJAX?

Reiterating on the same question asked yday(Above link),I seem to have more clarity now.

I get JSON output in 'data' var query = getDomainURL() + "/ProgramCalendar/GetJSONData";

  $.post(query, null, function (data) {...}

Now i Need to loop through the nodes and Identify 'Key' based on the names, and do some action for the 'val'

$.each(data, function(key, val) { ... } Can it be done using something similar with $.each, can you let me know the exact syntax?

Thanks, Adarsh

1 Answer 1

1

If the server returns a JSON array you could use $.each:

$.each(data, function(index, item) {
    // item represents the current element of the array
    // here you can access its properties like
    alert(item.EventText);
});
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I dont need to increment the loop or anythign like that ?
@user972480, no, jQuery does that. And it passes two arguments to the anonymous function: the index of the current element inside the array and the current element.
$.each(data, function(key, val) { Html.Raw("['" + val.EventDate + "'," + val.EventType + ",'" + val.EventText + "']" ); } I am doing something like this, Will this work?
@user972480, I don't know what Html.Raw is but the argument you are passing to it seems correct. This assumes that the JSON object has EventDate, EventType and EventText properties.
Yes, That's right. So just this $.each loop will do the processing for all the nodes in the result isn't it?
|

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.