I am working on a data list page that uses some filters (some select boxes with multiple value selection)
I need to be able to deep link this results so I need to pass all the params to the URL
if I pass a multilevel object (and not just key:value) pairs, I get [object Object] in the URL which is not good.
what would be the correct way to handle this? JSON.stringify?
Yes JSON.stringify and How to encode a query string so that it is the value of another query string in javascript? to make it a valid query value.
See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Related
I have a data object, and i want to get on of the value from it, when i try to print the data:
console.log(data);
i got an object like the image below :
the problem is i want to get the order[billing_address][country_id] which i think is an object, but i don't know how to fetch it. i've tried :
console.log(data.order); //didn't work
console.log(data.order[billing_address][country_id]);//didn't work
The name of the property is: "order[billing_address][country_id]"
To access its value try:
console.log(data['order[billing_address][country_id]'); // Should work
It appears that the values you are looking for have keys that are the whole string:
"order[billing_address][telephone]"
You can access these values like this:
data["order[billing_address][telephone]"] //"5"
You are currently trying this:
data.order[billing_address][country_id]
What you are trying doesn't work because there are no variables billing_address or country_id that are defined, and the object is not that deeply nested - just has the above mentioned long string for a key.
I have some JavaScript code that needs to be able to access fields of an array of objects that is contained within my model. I currently have this:
var model = #Html.Raw(Json.Encode(Model));
for(var i = 0; i < model.testobject.length; i++) {
console.log(model.testobject[i]);
}
Which prints out the fields within each object of testobject. But say I have a field, ID, in my testobject class. How do I then access that? Doing this:
console.log(model.testobject[i].ID);
Does not work. Do I have to somehow encode that specific instance of testobject before accessing it's fields?
And yes, before anyone says it I know this should be contained within the controller. As it currently stands though, that's not possible for this project.
This is the general structure of what is printed out:
Object {field: value}
Edit:
I attempted to use JSON.stringify on my model.IdentifiApprovalConfigurations and it seems I got a little close to reaching my solution. This is what it looks like now:
console.log(JSON.stringify(model.testobject[i]).ID);
However, this prints out undefined.
Edit 2:
Oops, seems the ID field I'm trying to access isn't being populated before I send them to my view which is my own issue. JSON.stringify works though, and I understand why it wasn't working earlier.
Final edit:
JSON.parse(JSON.stringify(model.testobject[i])).Value
I had to stringify and then parse my JSON to access the value.
As I'm still starting out with web development, I forgot that I needed to convert my object into a JSON object. This:
JSON.parse(JSON.stringify(model.testobject[i])).Value
Is the final piece of code that allows me to stringify an object, parse the JSON and then access fields within that object.
I have been working on a project that uses a object to maintain a conversation with the user. However if what has been asked by the user is not in this object. The user is prompted for what should the "AI" answer. The problem is that I can't save this object, so when the user closes the tab or reloads it everything that the "AI" has learned disappears. I've tried using localStorage.setItem('dictionary', JSON.stringify(dictionary)) (dictionary is the object) However it gave me "[object Object]". So i was wondering if there was anyway I could use cookies instead?
after you store your object you need to use it like this,
var dictonaryParsed = JSON.parse(localStorage.getItem('dictionary'));
this will fix your problem, this is because localStorage.setItem() will use the toString() method from the prototype chain, and you will get this [object Object].
This is because everything is converted to string before stored to the key,value pair database(localStorage).
With stringify method that you are using the object will be prepared for the JSON.parse() method if not used you will get something like this "{"a":10}", so you need to Parse your object before using it. If you try to parse not stringyfied object you will get error.
the localstorage you use has no problem,did you alert the object and you see the '[object Object]'?
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
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.