Why to use NSData to Json - json

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.

Related

Swift Json Single Quote Parse

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.

Convert doctrine array to JSON

Is there a way to read a column of doctrine type "simply_array" or "array" in json?
My doctrine database is approached from another api and I want to read data from that api. However there is a column of type doctrine array that I want to convert into JSON.
I am unsure if there is a preferred way of doing this or I need to hack my way around it.
Here is an example of what is stored in the database as a doctrine array:
"a:1:{i:0;a:3:{s:3:\u0022day\u0022;i:5;s:4:\u0022time\u0022;s:7:\u0022morning\u0022;s:12:\u0022availability\u0022;N;}}"
That looks like the format of PHP's serialize() function. And the literal double-quotes in the string have been converted to unicode escape sequences.
You could do the following:
Fetch the serialized string
Fix the \u0022 sequences (replace them with ")
unserialize() it to reproduce the array
Convert the array to JSON with json_encode().

How to send a json string in a json request?

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.

Does HTML need to be encoded when passed back in JSON?

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

Convert JSON to CSV or unstructured text

How might you take JSON output (e.g., from http://www.kinggary.com/tools/todoist-export.php) and strip the names to yield just the values from each pair, as CSV or human-friendly text? Want a more readable, human-editable backup of my friend's data on todoist.com
Your example site generates XML for me, not JSON. In either case I'd probably reach for Ruby:
require 'net/http'
require 'rexml/document'
xml = Net::HTTP.get_response(URI.parse("http://www.kinggary.com/tools/todoist-export.php?completed=incomplete&retrieval=view&submit=Submit&process=true&key=MYKEY")).body
data = REXML::Document.new(xml)
data.elements.each('//task/content') do |e|
puts e.text
end
there's a good discussion of how to do this with Python at How can I convert JSON to CSV?
Can you JSON decode it to an array and just iterate the array for values? A sample of the JSON output would be helpful.
What language? PHP has a json_decode() function that turns the JSON into an object or associative array. You could then loop through the array or get the values from the object to turn it into whatever format you like.