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 */
  }
});
15
  • 1
    Please show us what listArray is, how is it defined? Jul 11, 2011 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. Jul 11, 2011 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). Jul 11, 2011 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. Jul 11, 2011 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. Jul 11, 2011 at 16:34

1 Answer 1

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)
});
1
  • I would recommend using jquery-json plugin, as JSON.stringify is spotty across commonly used browsers. Jul 11, 2011 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.