AS3 URLVariables Unescaping Data - actionscript-3

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

Related

Sending Json looking formatted plain text to be de-serialized

If you send plain Json formatted un-serialized text from a web api and have it be de-serialized in a C# function using the jsonConvert.DeserializeObject functional? I have a co-worker who created a web api in the cloud and he sends plain text formatted to look like un-serialized Json which I try to use C# functionality to Deserialize but when I try to convert what is sent into Model classes it fails. I am telling him that the C# JsonConvert.SerializeObject must be used or it won't work. Can someone help clarify this with me?
Your colleague is right. JSON strings need to be de-serialized to turn them into objects.
Moreover, the things you call "plain text formatted to look like JSON" are JSON strings. JSON is plain text (in UTF8, with syntax rules).
Let's say you have some data structure in your program that you want to send over the network. The network can only send series of bytes, so whatever your structure was, you need to turn it into that -- you need to serialize it. JSON is one way of doing that, e.g.
'{"example": "some data"}'
is a string containing JSON. It is serialized, it's just a string of bytes to send over the network.
On the receiving end, you need to deserialize it back into some data structure, some type of hash map or dictionary or whatever it's called in C# probably.
If what you try "fails", you could ask a much more specific question showing what you tried with what data and how exactly it failed.

What is the type of the key in this JSON object {yourVariable: "nothing yet"}

Chrome Developer Console is console.loging this:
Your JSON sent is>> {yourVariable: "nothing yet"}
So i know the value "nothing yet" in the {yourVariable: "nothing yet"} JSON object is a string. But how do I know the type of the key yourVariable?
Is there a way how to find that out using the Chrome console only?
All object keys are strings with quotes or without quotes. Try this way to see it. May be you are confused with console print because console print it without quotes and we usually write with quotes.
var jsonObj = {person:"me","age":"30", 123:"123"};
Object.keys(jsonObj).forEach(function(key){
console.log(typeof key)}
);
I have a slightly different take on this question which does not invalidate the comments or answer that you received, but is worth considering.
Because you are talking about JSON, there is no intrinsic datatype in your example. As stated on the JSON.org page:
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. It is easy for humans to read and write. It is easy for
machines to parse and generate.
The point is, that there is a difference between JSON, which is a representation of javascript objects, arrays etc., and variables of those types in javascript.
If you remind yourself that JSON is a form of serialization it makes a lot more sense. Javascript objects for example, can include functions, but a javascript function is not a portable thing, so in rendering some JSON from a javascript object, it is up to the language creating the JSON to do whatever it it needs to in order to convert the data it needs to represent, and that may include simplification and in many cases removal of elements that are incompatible JSON.
The other thing to keep in mind, is that all modern languages have functions or libraries that can parse JSON and turn them into variables or objects that work in those languages. In doing so, they have arguments that can completely change the way the JSON is converted back into instance variables.
For example, in PHP you can choose to have the JSON create one or more PHP objects, or an array of php variables.
In summary, JSON doesn't have variables with datatypes at all. It is a representation of data, that is portable across languages, but the languages must decode the JSON and create objects or variables that are valid in their own runtime.

JSON parsing in NODE

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.

Why does SJCL report "this is not JSON" when trying to decode this JSON snippet?

I'm using SJCL, and it works fine with small ASCII strings.
But when I try to decode this piece of JSON (the result of the encryption of an HTML page) I get a "this is not JSON!" error.
The JSON has been produced by SJCL, and while I did encode it and decode it using LZW and base64 I don't get this error for small strings with the same workflow.
I tracked the error message origin to the decode function. I assume the regexes are failing but I don't understand why as this seems to be a perfectly formed JSON string to me.
However, I can be wrong as if I do a JavaScript eval on it it fails on a syntax error. But if I dump it in a file Python parse it fine.
The json that is at your this piece of json link starts and ends with a double-quote character. Is that actually part of the contents of the json? If it is, I believe that is your problem. Otherwise, it looks like valid json to me.
Ok I made a double passed base64 encoding. One before encryption, and one after. It seems that removing the first pass make it work.

Check Format for Json in Lua

How can one check to see if a string is properly formatted in json? I am running into situations where I receive a partial json string, and then when trying to decode the erroneous string with Lua's json.decode, my application crashes.
Thanks
Note that there is a difference between crash and crash in Lua.
If you see segfaults, throw away your JSON library and use something else (there are plenty).
If you see a Lua error, just wrap JSON processing code in pcall or xpcall. (But, better, again, throw away your library, and pick a better one).