views:

423

answers:

1

Hello I am a prototype newbie and am unclear on how to process multiple json objects returned. For example I would like to return a simple JSONObject map indicating the success/failure and also return a JSONArray that I can index and build an select:options from. Now each json object will be upt in the header with a different name. How do I parse that out client-side and alert on a failure name/value else build the select:option element? tia.

+1  A: 

Suppose your /someurl service sends a reply like this:

{
  "status": "ok",
  "data":["apples", "oranges", "bananas"]
}

What I have done here is to combine the two objects into a single object. In the client you can handle it something like this.

new Ajax.Request('/someurl', {
  method:'get',
  requestHeaders: {Accept: 'application/json'},
  onSuccess: function(transport){
    var json = transport.responseText.evalJSON();
    if(json.status != 'ok')
    {
       alert('status "'+json.status+'" not ok')
       return; // or throw a fit
    }
    json.data.each(function(elt){
      alert(elt); // or display it, whatever
    });
  }
});
Ewan Todd