When passing HTML back through a response in JSON format, does it need to get encoded?
Yes. You would pass the HTML code in a string, so any quotation marks and backslashes in the code would have to be encoded.
Example:
<div onclick="alert('Line 1\nLine2');">show</div>
would be encoded into a string like this:
"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"
and for example put in a JSON object representation like this:
{"html":"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"}
Simple answer "no" JSON does not need to be encoded when passed back in JSON. JSON object should be DIRECTLY parsable by a javascript engine. Check the following:
JSON Standard
JSON Lint
Related
I have a problem while parsing json which includes single quote. I am using JSONDecoder. I added response from API at below and I don't want to do any replacement or some regex operations. Are there any workaround for that?
"{\'value1\': true, \'value2\':\'2021-02-08\'}"
Your string is simply not valid JSON. Since it's not valid JSON there's no way to configure JSONDecoder to decode it.
If you aren't in charge of the service that outputs that string, the only thing you can do to use it with JSONDecoder is to modify the string to become valid JSON by, as you suggested, doing text replacement.
I'm using json plugin to get the response in json.
But I m getting the unwanted result:
Here is what I get:
{"data":"[[\"service\",\"webservices\",\"document\"],[\"validation\",\"adapters\",\"server\"]]","records":25,"recordsTotal":75}
originally the data var in my action class is like this:
[["service","webservices","document"],["validation","adapters","server"]]
but json plugin adds the backslash.
The wanted result is that:
{"data":[["service","webservices","document"],["validation","adapters","server"]],"records":25,"recordsTotal":75}
Is there a way to get the later result ?
Thanks
You're representing the data as a PHP string. " is obviously a reserved character in JSON, so your serialization library is dutifully escaping the quote using /.
If you set up the PHP variable so it's an array of arrays, instead of a string representing an array of arrays, then your JSON serialization will work fine.
Every time I make a call to Json, I need to use NSData and Json.deserialize.
Since Json is just a string, why cannot I just read it through rest and then use it at the other side, without using NSData, Json.Serialization, etc ?
JSON is a method for sending data through strings (not binary). Using as a string is possible, but you'd have to parse it yourself:
Here's a JSON string:
"{\"test\": \"foo\"}"
and it can be converted to
{test: "foo"}
and used like a Hash/Dictionary.
I mean, you can't get the use a string as a dictionary, can you?
"Nope.".get("Nope")
JSON is not a string. JSON is a binary format. That binary format is human readable but it's not a string. That's why we have to use NSData and why we have tools for JSON encoding and decoding instead of creating JSON data by appending strings together.
I need to send a json string in the json request and pass it on the backend to be unmarshalled as an object. ie a JSon request has a string attribute which can be converted to an object later. What's the format for that?
I tried {"name":"bob","address":{"state":"ny","city":"newport"}}. I dont want "address" : {...} to be unmarshalled here.. i want it to be sent as it is to backend as a String.
FWIW, i m using jackson , and I dont think that should matter.
Also, I dont want to escape the characters ie
"address":{\"state\":\"ny\",\"city\":\"newport\"}
I wouldn't try to manually encode json yourself, Use an appropriate Json encoder for whatever language you're using.
{"name":"bob","address":{"state":"ny","city":"newport"}} is not valid json. the entire string should be quoted. like:
"{\"name\":\"bob\",\"address\":{\"state\":\"ny\",\"city\":\"newport\"}}"
and it will arrive at the backend as a string. thats what a json string is, a string.
you need to decode it server side with an appropriate json decoder
if you dont want to escape the quotes then use a combination of single and double quotes like:
'{"name":"bob","address":{"state":"ny","city":"newport"}}'
Using php:
<?php
$a = json_encode(array("state"=>"NY", "city"=> "Newport"));
$b = array("name"=>bob, "address"=>$a);
print_r($a);
print "\n";
print_r(json_encode($b));
print "\n";
?>
Output
{"state":"NY","city":"Newport"}
{"name":"bob","address":"{\"state\":\"NY\",\"city\":\"Newport\"}"}
The last line is your answer - but making the code do it for you is always preferable.
Having created a text file with a JSON object (from an array) using json_encode, I'm now supposed to decode the same object. However, with json_decode, the unicode escape sequences don't seem to be properly converted back.
Here is the example of a string from the JSON file:
S\u00720066006f006cd industriomr\u00640065
After json_decoding, the text becomes:
Sr0066006f006cd industriomrd0065
Any idea what's going on here?
The decoding seems to work fine; the final text does indeed correspond with the encoded one. What was the object like before encoding?