In my web application, why do JSON.stringify serialize my Activity's Trackpoints array as a string? Notice in the JSON representation on top that the trackpoints array is enclosed in quotes, rendering it as a string. The javascript object representation in the bottom shows clearly that trackpoints is an array.
When this serialized activity is passed as JSON in a POST to my Sinatra back-end, Ruby parses its trackpoints as a string instead of an array of Trackpoint objects. The end result is a 500 server error, which is not what I expect.
Precision for people unable to see the included image: "The trackpoints array is a property of a complex object. JSON.stringify encloses it in quotes while doing its job. When Ruby parses the whole serialized object, it interprets my array as a string instead of an array. That's why I want to avoid those quotes."
Related
In mysql database, "diagID" is saved as json_encoded(array). Now i need it to retrieve in ajax success.
How to convert JSON parse data into array, as it's showing string?
var ajaxResponse= {
"id": "123",
"diagID" : "['101','125','150','230']"
}
typeof(ajaxResponse.diagID)
= string
In javascript typeof(ajaxResponse.diagID) shows string. How to convert it into array?
Decoding it in php would make most sense
$diagID = json_decode($diagID, true);
Then when you json_encode() the whole response it won't have the extra wrapping quotes.
Note however that the strings in array have single quotes which are not valid json and need to be replaced with double quotes before they can be parsed in either language
hi im having trouble correctly adding to my json
here is the code.
When i console.log the string im trying to add is
{"type":"#","name":"wh2xogvi","list":[{"0":"background-color"},{"1":"border"},{"2":"width"}, {"3":"height"},{"4":"margin"}],"listvalues":[{"0":"#aaa"},{"1":"2px solid #000"},{"2":"1040px"},{"3":"50px"},{"4":"0 auto"}]}
it is valid json
var jsonltoload = JSON.stringify(eval("(" + jsonloadtostring + ")"));
console.log(jsonltoload); // this is the console log i was talking about higher up
fullJSON.styles.objectcss.push(jsonltoload);
But when i actually look at the json it is wrong ends up something like this
"{\"type\":\"#\",\"name\":\"unkd42t9\",\"list\":[{\"0\":\"background-color\"},{\"1\":\"border\"},{\"2\":\"width\"},{\"3\":\"height\"},{\"4\":\"clear\"}],\"listvalues\":[{\"0\":\"#ddd\"},{\"1\":\"2px solid #000\"},{\"2\":\"100%\"},{\"3\":\"50px\"},{\"4\":\"both\"}]}",
the fullJSON comes from JSON.parse(json); which comes from a file
You seem to confuse JSON, a textual, language-independent data representation, with JavaScript objects, a language-specific data type.
JSON.stringify returns a string (containing JSON), so jsonltoload is a string. I guess you simply want to parse the JSON and add the resulting object:
var obj = JSON.parse(jsonloadtostring);
fullJSON.styles.objectcss.push(obj);
I think the JSON string is trying to escape the double quotes character you have added to string, resulting in the string. try to enclose the whole string with single quotes rather than double quotes
I want to parse the following string (using Javascript):
color(alias(sumSeries(sys.mem.free.*),"memory (free)"),"#00AA88")
into an array of function names and arguments:
[["color", "#00AA88"],
["alias", "memory (free)"],
["sumSeries", ""]]
plus extract the innermost string
sys.mem.free.*
The string is actually the target parameter from graphite.
I don't want to write a parser myself (dealing with things like double quotes and brackets is hard to get right).
Is there a library which helps with basic parsing?
Since it's not JSON I cannot go for a JSON parser.
I am trying to parse this JSON:
var json = '{"material":"Gummislang 3\/4\" 30 m (utanp\u00e5liggande sk\u00e5p)"}'
I run JSON.parse(json) but i get the error SyntaxError: Unexpected number when doing so. I have tried this in Google Chrome. I don't know what the problem is since I can take the JSON string and put it in any JSON validator and it claims that the JSON is valid. Shouldn't the browser be able to parse it?
You are inserting a JSON object representation into a JavaScript string without properly escaping the representation.
To avoid having to do this, remove the quotes you are adding around the representation, and skip the JSON.parse(json) – the default output from PHP's json_encode() is valid JavaScript when used in this context.
For security, you should specify the JSON_HEX_TAG option if possible. This will prevent cross-site scripting in cases where the JSON might end up inside a document parsed as XML. (And for XML documents, the JSON should be inside a CDATA section as well.)
You're validating the string literal, which is a valid JSON string containing invalid JSON. You need to validate the value of the string, which is not valid JSON.
If you paste the string value into a JSON validator, you'll see that the error comes from this part:
"material": "Gummislang 3/4"30m
The " needs to be escaped.
I'm calling a service that returns UTF-16 json data.
My question is if the JSON object has UTF-16 strings as property names is there a simple way to reference these properties?
For example, here is how the response data looks like after calling JSON.stringify on it:
"{"C\u0000o\u0000n\u0000t\u0000e\u0000n\u0000t\u0000s\u0000":{ ...
In my code I'd like to do something like data['Contents']. Is there a simple way around this that avoids either hardcoding the strings with unicode escape sequences?
Update: changed to indicate strings are UTF-16.
Here's an example (Visual C++) of the call to generate the JSON output:
wchar_t* str = _T("Contents");
yajl_gen_string(g, (unsigned char*)str, wcslen(str) * sizeof(TCHAR));