views:

78

answers:

1

When sending an array in a JSON object in a query string, is each array element supposed to have the same key? For example, if I have this JSON object:

{"sodas[]": ["coke", "sprite", "fanta"]}

Should the query string look like this, with all the keys exactly the same (sodas%5B%5D)?

sodas%5B%5D=coke&sodas%5B%5D=sprite&sodas%5B%5D=fanta

Or should the query strings have an index value in them or something (sodas%5B0%5D, sodas%5B1%5D, etc)?

sodas%5B0%5D=coke&sodas%5B1%5D=sprite&sodas%5B2%5D=fanta
+1  A: 

The first statement without the square braces for the "sodas" key would work. I'm not sure which languages you are using but here is an example with HTML, jQuery, and PHP.

HTML (file: y.html)


<!DOCTYPE html>
<html>
<head>
<title>XYZ</title>
<script
  type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
</head>
<body>
<input type="button" id="send" value="Send">
<hr noshade>
<div id="output"></div>
<script type="text/javascript">
var $output = $('#output');
$('#send').click(function(e){
  e.preventDefault();
  var $json = '{"sodas":["coke","pepsi","fanta"]}';
  $.ajax({url:"/so/y.php",type:"post",dataType:"html",data:'json='+escape($json),
    success:function(obj){
      $output.html(obj);
    }
  });
});
</script>
</body>
</html>

The JavaScript escape() function formatted the json POST parameter as follows (taken from Firebug.)

json=%7B%22sodas%22%3A%5B%22coke%22%2C%22pepsi%22%2C%22fanta%22%5D%7D

PHP (file: y.php)


<?php
$json = json_decode(stripslashes($_POST['json']));
var_dump($json);

The browser output displays the var_dump()'d string representation of a PHP object, single-keyed associative array with the value being an array of three soda brands.

object(stdClass)#1 (1) { ["sodas"]=>  array(3) { [0]=>  string(4) "coke" [1]=>  string(5) "pepsi" [2]=>  string(5) "fanta" } } 
jtp
Thanks for the reply, JT. Instead of using a string as the `data` argument, I used an actual object: `var $json = {"sodas":["coke","pepsi","fanta"]};` I understand that this is supported. Is that right? So we should also do without the `escape` here. Using a native JavaScript object should mean I get it in a ready-to-use format on the server. If I do use a string, like you showed, then I'd have to unpack it myself on the server, which I'd like to avoid. I'm using a Python-based web framework (webpy). +1 since your solution is a valid workaround.
Mike M. Lin
Hmmm. I'm not sure how to get the JSON object over the wire other than to serialize it first. The general consensus on SO points to json2.js which provides a JSON.stringify() method and can be used on the JavaScript object directly whether created explicitly with var x={"sodas":["pepsi","coke","fanta"]}; or implicitly with var x = new Object();, etc. If using Python 2.6+, the json module can be used to decode it into a Python object (data structure without methods AFAIK.) I still ended up using escape() to translate the {} and [] though, although the CGI program accepted the POST either way.
jtp
OK. I'll accept serializing the JSON object as the correct answer. jQuery has the getJSON() method, which supports the optional `data` argument. The documentation says `data` is a map or string that is sent to the server with the request. The first query string above was generated by jQuery's getJSON() method using a JSON object (not a string) as the data. I figured that, in addition to a map, it could also handle a JSON object, but I guess that was just wishful thinking.
Mike M. Lin