3

I am trying to submit some JSON to my web app and I want the JSON to be like this:

{ 
  "thing1" : 
  {
    "something" : "hello"
  },

  "list_of_things" :
  [
   {
     "item1" : "hello"
   },
   {
     "item2" : "hello"
   }
  ]
}

Here I have one JSON object and a JSON array that holds JSON objects. When I create the data to submit in Javascript I do:

form = {
  "thing1" : {
    "something" : somethingVariable
  },
  "list_of_things" : listArray
}

Here 'listArray' is a Javascript Array object of Javascript hash objects. I submit this using jQuery's ajax method but instead of javascript array displaying as the JSON array desired it converts it to a series of JSON objects like this:

{ "1" : { "thing1" : "something" }, "2" : { "thing2" : "something" }...

How can I get the array to be submitted as an array rather than be converted into a series of JSON objects with the array indexes as keys?

EDIT#1: 'listArray' is a simple Javascript array that is defined like so:

var listArray = new Array();
listArray.push({ "thing1" : "something" });
listArray.push({ "thing2" : "something" });

EDIT#2: 'form' is sent to the server with the following call:

$.ajax({
  type: 'POST',
  url: '/url',
  dataType: "json",
  data: form,
  success: function(data) {
    /* success code here */
  }
});
seadowg
  • 4,215
  • 6
  • 35
  • 43
  • 1
    Please show us what `listArray` is, how is it defined? – Felix Kling Jul 11 '11 at 16:28
  • 4
    ( *obligatory SO troll response* ) There is no such thing as a JSON object, JSON is the notation spec. You're creating javascript object literals. – jondavidjohn Jul 11 '11 at 16:29
  • 2
    @jondavidjohn: Actually in this context the terminology was correct. Of course, in JSON there are objects which can be called JSON objects. It is just wrong to refer to JS objects as JSON objects, but if you read carefully than the OP is not doing that (although it could be written in a clearer way). – Felix Kling Jul 11 '11 at 16:31
  • 1
    @jondavidjohn - Nope, I think here the OP is actually talking about JSON - the concern is that what's being created *in JSON* is an object representation, not an array. – nrabinowitz Jul 11 '11 at 16:33
  • 1
    @Felix Kling - Not really seeing what you mean, JSON is the Notation standard which was *derived* from the way javascript defines an object literal, but `form` in this case is a object literal, not a "JSON object"... maybe I'm missing something. – jondavidjohn Jul 11 '11 at 16:34
  • @jondavidjohn: Yes but he is never referring to that as JSON object. I'm pretty sure the preceding sentence describes the JSON data before. – Felix Kling Jul 11 '11 at 16:38
  • The first code section is the (pretty) JSON I want to post the second is the javascript variable that I post using ajax.post in its 'data' argument and the third is the a section of the JSON I see in my rails log for the post (the section that should be the array). – seadowg Jul 11 '11 at 16:41
  • The mismatched quotes are killing me. Can you please show the code you use to process this "form" object? – Jamie Treworgy Jul 11 '11 at 16:41
  • Yeah just noticed them (and fixed). Will update with the code that posts the form. – seadowg Jul 11 '11 at 16:42
  • @Oetzi: `listArray` looks actually fine... how does the data look like in the request? Have a look at it in the browser developer tools and not in the rais log. – Felix Kling Jul 11 '11 at 16:42
  • 1
    @Felix Kling - the term "JSON object" is never correct unless you're talking about the json2.js parser class. In all other cases it is a "JSON representation of an object". (now I'm just arguing to argue) – jondavidjohn Jul 11 '11 at 16:43
  • Just to add fuel to the fire: http://www.json.org/javadoc/org/json/JSONObject.html – Jamie Treworgy Jul 11 '11 at 16:45
  • Don't you want `data: $.toJSON(form)`? Does jQuery ajax perform this conversion automatically if its not a string? – Jamie Treworgy Jul 11 '11 at 16:46
  • This function actually doesn't work on form it appears and causes a TypeError. I have submitted JSON using the above method before and it has worked. I have never tried it with arrays though. – seadowg Jul 11 '11 at 16:51
  • Oh, oops - it's a plugin, i've never not used it so I totally forgot. http://code.google.com/p/jquery-json/ I am pretty sure you need to serialize it as JSON yourself. Jquery has no way to know your intent when you pass it an object, and from my reading converts it to just name/value pairs which will be decoded at the other end in whatever manner the decoder treats that. – Jamie Treworgy Jul 11 '11 at 16:55

1 Answers1

3

Have a look here. If you are truly trying to post JSON you will need to send the string, not an object literal. You could use JSON.stringify (or a more supported JSON solution) on form.

$.ajax({
    url: "/url",,
    dataType: "json",
    type: "POST",
    processData: false,
    contentType: "application/json",
    data: JSON.stringify(form)
});
Community
  • 1
  • 1
Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67