1

I'm currently getting a json response which is:

[{"id":1,"title":"Test 1 "},{"id":2,"title":"Test 2"}]

and I want to convert it to a javascript array called 'events' like below so I can return it: e.g:

  return {
     events : [
        {
           "id":1,
           "title":"Test 1"
        },
        {
           "id":2,
           "title":"Test 2"
        }
     ]
  };

I'm getting the response from a jquery ajax call:

    jQuery.ajax({
        type: "POST",
        url: "calendar.aspx/get_all_events",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            document.write(msg.d);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('Error getting events from database.');
        }
    });

Anyone know how I can convert the msg or msg.d into what is needed?

Thanks,

3
  • 1
    What is msg.d? You response seems to be already in format you need. Commented Dec 15, 2012 at 13:31
  • Can't you use eval(msg.d) ? Commented Dec 15, 2012 at 13:31
  • you can't use document.write after page has already been loaded Commented Dec 15, 2012 at 13:51

4 Answers 4

2

You can use:

return {events: JSON.parse(msg.d)};

or, for better compatibility:

eval("var result = "+msg.d);
return {events: result};

or jQuery solution

return {events: $.parseJSON(msg.d)};
Sign up to request clarification or add additional context in comments.

Comments

0

if msd.d is

 [{"id":1,"title":"Test 1 "},{"id":2,"title":"Test 2"}] 

then you could return it like this:

return {
     events : msg.d
  };

But you would have to do a callback in order to return it. Because ajax is asynchronous. I added the return because that is what you wanted. The alternative would be to make the ajax call synchronous.

Comments

0

I think you just need this:

    success: function (msg) {
        // The event object is what you require
        var eventObj = {events:msg.d};
        document.write(eventObj);
    },

Comments

0
function doXHR( callback ) {
   jQuery.ajax( {
        type: "POST",
        url: "calendar.aspx/get_all_events",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function ( event ) {
            callback && callback( event );
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('Error getting events from database.');
        }
    });
}

doXHR( function( event ) {
    var data = { events: event.d };
    // Do other stuff with data here (processing, displaying, etc.).
    // You can call other functions here and feed them with data.
} );

Note that XHR requests are asynchronous. You cannot simply return XHR request data. You have to put all your further code into callback function so it can process your array when it's available.

Comments

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.