I want to create a dictionary containing array and send it to GAE using jquery's ajax request.
something like- {'a':'text', 'b':'othertext','c':['texta','textb']} which I'm creating manually.
I am able to receive and process a and b using self.request.get on GAE but not the c.
Is there any other way to create JSON object in js? Please suggest whats wrong in this method.
You're not really sending JSON to the server. When you pass the object to jQuery.ajax (or the get/post wrappers), it will be serialized into GET or POST variables, not sent as JSON, so your object would be converted into something like this:
a=text&b=othertext&c[]=texta&c[]=textb
If you want to pass the entire object as JSON, you can convert it yourself by calling JSON.stringify (you will need to include json2.js for browsers that don't support the JSON object natively). Then you can wrap the JSON-encoded string in a map with whatever name you want:
jQuery.post(url, { json: JSON.stringify({a:'text', ...}) }, ...);
On the server side, you can access the JSON text by calling self.request.get("json"). Then you would have to parse the JSON string to extract the values. I don't know much about Python, but apparently you just need to import django.utils.simplejson and call simplejson.loads(json).
Presumably, GAE's self.request.get is not able to serialize a complex object like a string array into a GET request format (?a=text&b=othertext...).
One workaround, although perhaps not a very neat one, would be to serialize the value to JSON yourself, and pass that:
var jsonObj = {
'a':'text',
'b':'othertext',
'cJSON': JSON.stringify(['texta', 'textb'])
};
... and then of course you'd have to deserialize cJSON at the receiving end.
You're actually sending urlencoded request parameters, not JSON. JQuery is probably encoding the list as multiple values for the same parameter, which means you can access it using self.request.get_all, which will return a list - self.request.get will only return the first value.
Related
I am saving array of javascript objects in the html data attribute using JSON.stringify(). When I try to retrieve the data using jquery .data() function, I do not get deserialized array of javascript objects back, but gives me plain json string. I read, jquery .data() function deserializes Json string as quoted in documentation
When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string;
it must follow valid JSON syntax including quoted property names. If
the value isn't parseable as a JavaScript value, it is left as a
string
I think, mine is a valid json string, because, if I try $.parseJSON on that, it gives me back array of javascript objects.
Please help!
You don't need to stringify objects to store them using jQuery.data(). Just store the object like so:
var myobject = { "name":"john", "age":30};
jQuery('#dataholder').data('theobject',myobject);
console.log(jQuery('#dataholder').data('theobject'));
console.log(jQuery('#data_attribute_method').data('theobject'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dataholder"></div>
<div id="data_attribute_method" data-theobject='{ "name":"jim", "age":31}'></div>
jQuery will parse (as objects) hard coded data attributes at rendering time, but any data attribute you set as string afterwards, will be stored as text no matter if wrapped in curly braces or not.
I am attempting to parse the incoming JSON from XBMC (Eden Beta v3), an example string from XBMC is as follows:
{"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"item":{"type":"movie"},"player":{"playerid":1,"speed":1},"title":""},"sender":"xbmc"}}
I am using json2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js to give me the JSON.parse and JSON.stringify functions in my application.
I am using the parse function in my code like this
var temp = JSON.parse(data);
Which is working fine for the first two properties, "jsonrpc" and "method"... so I can access those like this
temp.method
returns "Player.OnPlay"
temp.jsonrpc
returns "2.0"
However, if you look at the string sent by XBMC, it also contains what they refer to as "extended parameters" or "params", I cannot seem to track down what the JSON parser is doing with the rest of the message, or how I can access them in similar ways as the first two properties. If I try
params = temp.params
or any other way of accessing the params property, I get an Undefined error as there is no such property in the JSON object... I hope this is clear enough for someone to take a stab at helping me out. Will provide any extra info needed...
Thanks
The value of params in the JSON data is an object, so you have to access the sub-properties of that.
temp.jsonrpc
temp.method
temp.params["data"]["item"]["type"]
temp.params["data"]["player"]["playerid"]
temp.params["data"]["player"]["speed"]
temp.params["data"]["item"]["title"]
temp.params["sender"]
I need a function to return the current instance as a json text, because I will send the values with an ajax request to server side script.
I don't know where to use the "this" keyword in a jquery selector
function Actor(){
this.input=function(pname,ppassword){
this.name=pname;
this.password=ppassword;
}
//I need a function to return the current instance as a json text here
//I will send the values with an ajax request to server side script with a function
}
I've found out that jquery doesn't support encoding as JSON and I need to use JSON2.js for serializing as JSON string.
New browsers have native JSON support so you can use JSON.stringify without including JSON2.js
You may need to be more specific in your question but if you need this in the input function to be a JSON string, you can use:
function Actor(){
this.input=function(pname,ppassword){
this.name=pname;
this.password=ppassword;
return JSON.stringify(this);//this will return pname and ppassword in json object string
}
}
See here for JSON libraries for older browsers that don't support JSON object natively: https://github.com/douglascrockford/JSON-js
I have a JSON var response (See below) but I can't retreive the value of "reputation" using response.users[0].reputation. I am in Dashcode and it says in error Result of expression response.users[unknown] is not an object. What is the correct syntax?
edit: The variable is dynamically loaded from a XMLHttpRequest. A static var with same json is working.
I guess from XMLHttpRequest you recieve string but not json object, so you need parse it first in order to get json object, for example using JSON.Parse or jQuery.parseJSON and response.users[0].reputation should work.
The JSON is fine. I just checked it and "response.users[0].reputation". (Second pair of eyes and all that.)
I would be more concerned about the "unknown" in "response.users[unknown]". It doesn't look like you are requesting the 0th index of the array "users". Something's going wrong there.
Personnally when i test the syntax of your Json, it is correct :
http://www.jsonlint.com
I have just deleted the comment "//need to get its value".
Your code works for me as you have done it. Maybe you are not assigning the variable correctly (var response = {"total": 1, etc...) or have a typo somewhere
Hello i have an url in my jsp and i want to pass an array of string in this url to recover in my ActionForm
If you are dealing with something simple like a list of numeric ids, i would just run through the check boxes, create a comma separated list, and assign it to a query string parameter. On the other side i would split the string.
If the values are more complex you have to consider escape characters. Also if you are dealing with a long list, the url is not the best way to pass this data.
You can use 'standard' html way of passing arrays of data: http://mywebsite/mypage?myarray=value1&myarray=value2&myarray=value3. Then you can fetch all values of parameter myarray from request object (if framework doesn't provide more elegant ways of handling arrays).
But seeing your comment, I would recommend to leave JavaScript and just declare a form for it.
If you need a link (not button), you can always submit form from it. Something like ...
Try Json encode
http://code.google.com/p/json-simple/
Check this