Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable as identifier in a json array

I'm wondering if it is possible to use assigned variables as identifier in a json array. When I tried this, I was getting some unexpected results:

(Code is simplified, parameters are passed in a different way)

var parameter = 'animal';
var value = 'pony';

Util.urlAppendParameters(url, {
  parameter: value
});


Util.urlAppendParameters = function(url, parameters) {
  for (var x in parameters) {
    alert(x);
  }
}

Now the alert popup says: 'parameter' instead of 'animal'. I know I could use a different method (creating an array and assigning every parameter on a new line), but I want to keep my code compact.

So my question is: Is it possible to use a variable as an identifier in the json array, and if so, could you please tell me how?

Thanks in advance!

like image 251
Robbert van den Bogerd Avatar asked Apr 29 '10 14:04

Robbert van den Bogerd


People also ask

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

Can you create variables in JSON?

json has been loaded into a JSONObject named jsonData. Using data. json as your guide, create String variables named name, publisher, and language and set them to the appropriate values from data.

Can JSON array have different data types?

Yes, json arrays can contain any valid json string: objects with different key/value pairs, other arrays, numbers, strings, booleans all in the same array.


3 Answers

You will need to build your object in two steps, and use the [] property accessor:

var parameter = 'animal';
var value = 'pony';

var obj = {};
obj[parameter] = value;

Util.urlAppendParameters (url, obj);

I don't think JSON Array is the more correct term, I would call it Object literal.

like image 79
Christian C. Salvadó Avatar answered Nov 04 '22 07:11

Christian C. Salvadó


No, you can't use a variable as an identifier within an object literal like that. The parser is expecting a name there so you can't do much else but provide a string. Similarly you couldn't do something like this:

var parameter = 'animal';
var parameter = 'value'; //<- Parser expects a name, nothing more, so original parameter will not be used as name

The only work around if you really really want to use an object literal on a single line is to use eval:

Util.urlAppendParameters (url, eval("({" + parameter + " : value})");
like image 20
Bob Avatar answered Nov 04 '22 06:11

Bob


Depending on your needs you could also build your object with a helper function;

Util.createParameters = function(args) {
    var O = {};
    for (var i = 0; i < arguments.length; i += 2)
        O[arguments[i]] = arguments[i + 1];
    return O
}

Util.urlAppendParameters (url, Util.createParameters(parameter, value, "p2", "v2"));
like image 41
Alex K. Avatar answered Nov 04 '22 06:11

Alex K.