JSON parsing in NODE - json

I'm trying to figure out why my parsed request(a json from an jquery ajax, cross domain) is looking a bit strange.
GET /sendjsondata?callback=_testcb&{%22amount%22:1800,%22name%22:%22Vasy%20Jon%22,%22cnp%22:232323,%22coborrower%22:true,%22device%22:%22Desktop%22}&_=1415883870387 404 3.346 ms - 1303
I can create a function to retrieve the piece between braces and then applying a new polish to remove "%22", but I think that I'm dooing a mistake somewhere in my code and that's why I don't obtain a clean json object and maybe someone can tell me where is the issue.
Thank you.

URLs use a special encoding to represent special characters. %22 equals ". Whatever framework you are using should take care of decoding this. Otherwise look for urlencode()/urldecode() functions.

Related

Json Special Characters

I have a json object which contains some values that include some special chracter. For example {UserName":"UserTest","Password":"OImqNlK/tLwUzKnt1rA1OA=="}
I use it as an object parameter to call to web Api but I can't get it on Api server.
it works fine if I remove the special characters. For example:
{UserName":"UserTest","Password":"OImqNlKtLwUzKnt1rA1OA"}
Please help me fix it. Many thanks!
First page when I put "Json Special Characters" into google : here
To be concrete to your question, this is the way
{UserName":"UserTest","Password":"OImqNlK\/tLwUzKnt1rA1OA=="}

How to properly encode ATG commerce JSON taglib droplet response

I had a problem where I was doing an jquery $get for a list of schools. I was using an RQLForeach droplet to retrieve the list and specified the output to be JSON, I would then take the returned JSON and use jQuery template to render out the result.
Problem I was seeing the following in the output King'swhich should've been King's School.
I used the {{html schoolName}} Which was supposed to take care of the decoding correctly. But it did not....
The solution to the problem.
Problem: Was that encoding and decoding was happening twice. First the json:object that gets output from the droplet was already escaping XML. So would therefore encode the & of the apostrophe. i.e. it was reaching client side as
&amp#39; instead of '. Therefore the {{hmtl}} could not decode it correctly
Answer: set the <json:object escapeXml="false"> this meant that by the time it reaches client side it was in the correct format ' to be decoded by the jquery {{html }} tag.

Passing an HTML parameter in routes (Play framework)

Good day! How are HTML parameters expressed in the routes file? I am trying to pass an HTML but I don't know how. All I know are passing integers ((id: Integer)) and some data types. I tried (content: Html)and (content: Html). I also tried javax.swing.text.html.HTML but it says something about QueryStringBindable. Please help me. Thanks a lot.
Remember that all you pass by route's params will be included in the URL so what is the advantage of using HTML in this place ? GET params should use only simple data types like numerical types, booleans and strings - so you can pass some HTML part as a String (preferably url-encoded or even beter with Base64 encoding).
Much better option is sending it via POST, your URLs won't be terrible long - you won't hit any limitation of URL length, also after common serialization it won't break at special HTML chars.

How can I send HTML over JSON (in JS / Node)

I'm trying to return an html snippet from a service that can only return valid JSON.
I've tried some things like:
This gets me a bunch of character like \n\n\n\n\t\t\t\t
return JSON.stringify({html: $('body').html()});
or
return JSON.stringify($('body').html());
On the receiving end, I'd like to be able to parse that HTML via Cheerio, or jQuery or JSDom so I can then run queries like $(".some_selector") on that data.
What is the proper way of doing this? Any special libraries / methods that can handle the escaping for me? I've googled it, but haven't had any clear results...
Thanks.
On the receiving end, you need to simply undo the JSON serialization. That's it!
Your HTML will be in its regular format at obj.html, which you can then parse with whatever DOM parser you want.
Well, you are probably going to need to worry about quotes in the HTML (like with attributes) because the could interfere with the quotes that delimit your JSON values.
Here is similar question as well as this web page that explains some of what you need to consider.
Briefly looking at npmsj.org, I didn't see any reputable modules that might help you make HTML JSON compatible, but I think you can probably figure it out fairly easily on your own, given a large enough sample set of HTML. You can always run your JSON through this validator to check it. I suppose you could also simply do a JSON.parse(jsonContainingHtml) on it as well. You'll get an exception if the string is not valid JSON.

AS3 URLVariables Unescaping Data

I have a PHP file that is queried for information, and it passes a couple of variables back. One variable contains a JSON string with a variable in the object called message, which comes escaped to prevent it from causing issues if the message has an ampersand, single quote, etc in it.
&data={"message":"star%27s"}
Obviously the data sent is more complicated, this is just an example. After I take the data passed back by the PHP file and use URLVariables to decode it and access the "data" variable, it ends up looking like:
{"message":"star's"}
At this point I can't parse the JSON string, it will throw an error because of the single quote. Encoding it wouldn't work, it would encode more than just everything after the colon.
Is there a way to keep it from converting it? I was thinking I could manually parse the PHP returned string, but it seems unnecessary and I don't want risk running into issues later on because of it. I looked at the AS3 API and I couldn't find anything documenting this or how to disable it.
Any ideas or suggestions?
You try with
Actionscript API escape() and unescape() see for more details Escape and unescape
Also look at JSON.parse and JSON.stringify working-with-native-json-in-flash-player-11
JSON decode in actionscript see decode-json