Convert loaded string to Object - actionscript-3

In AS3, I want lo load a file text with URLLoader. In the file text I have the following string:
{a:1,b:"string",c:["one","two"]}
Is it possible (once loaded) to convert it to an Object?

There is no intrinsic deserializer built into the language, no. But if your text file sticks to the JSON standard, then you could use a JSON parser to do the conversion for you: http://code.google.com/p/as3corelib/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fadobe%2Fserialization%2Fjson
Or, if you cannot adhere to JSON, you could always write your own deserializer.

What you need is to eval the string to create the object.
This is done natively in javascript and AS2. AS3 however does not support this function.
But all is not lost. The people at Hurlant have created a library that does this "almost" as good as native JavaScript.
Here is a good example.
And another library example using d.eval
I would like to point out though that if you have accept to the source of the object string that you create a JSON object out of it. The JSON libraries are usually much easier and more reliable to use then the libraries that do Eval.

Your string is a sting with JSON format. Use JSONDecoder to decode it to an Object, like this:
var dc:JSONDecoder = new JSONDecoder("{a:1,b:'string',c:['one','two']}");
var ob:Object = dc.getValue();

Related

JsonConverter in Manatee Json

I am using Manatee Json for de-/serialization. I want to do use the similar functionality which is available in Netwonsoft where one can override JsonConverter Read and Write Json. Do we have an example on how to do that in Manatee?
Manatee.Json works a bit differently than Json.Net.
With Json.Net, you implement a reader or writer where you translate directly between the JSON and the object.
With Manatee.Json, you don't have to worry about the JSON so much. Instead you translate to a JsonValue.
You have a couple primary options on how you can do this:
If you own the object (you have the .cs file for it), the best approach is to have it implement IJsonSerializable. The serializer prioritizes for this.
If you don't own the object, you can create an ISerializer implementation to perform the translation.
The serialization docs give a lot more detail.

VBJSON for VB6 how to serialize object returned from Parse routine

So there is a nice library for VB6 JSON parsing. HERE
but i actually used one that built on the original and optimized. HERE
Essentially I'm using the parser to deserialize the json i get from a web service. I need to update some values, and resend to the server. Using the Collection/Dictionary objects made it very easy. But now, How do i take those objects and serialize them to a JSON string? is there a library for that?
thanks you for your help.
There are quite a few JSON parser/serializer/DOM classes written in VB6. Perhaps you might want to consider one of those instead. E.g.:
JsonBag, Another JSON Parser/Generator

GWT Label format Object String

I have an String representation of an object obtained from reflectionToString method (http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringBuilder.html#reflectionToString%28java.lang.Object%29) and I need to put it into a GWT Label. But this string could be very long and I wanted to display it in a nice format, ideally in json format.
Anyone has any suggestions on how to format it?
You could use something like http://highlightjs.org/ to format the raw text/code in a vaguely human readable way? Calling JS from GWT is pretty easy.

String XML to JSON conversion in .NET without using XmlDocument

I'm trying to find a more memory efficient solution for converting XML string to JSON string (and vice versa)
without using XmlDocument.
Currently, all 3rd party libraries i tried, expects XmlDocument as input.
Before I'm writing my own parser using XmlReader, i was wondering if anyone know of a out of the box solution?
What are you trying to do exactly: Generate JSON directly from XML or deserialize the XML string to an object and then serialize it to JSON?
If you need a XmlSerializer take a look into this one I created (it uses XmlReader internally), you can find the code and how to use it here:
XML serialization using Generics
I ended up writing my own thin LightXmlDocument which holds a tree of objects representing xml elements.
LoadXml method implemented using XmlReader, i'm reading the xml string and building the tree.
Tested with 10 threads each thread iterating 900 times over different xml sizes:

Is there any pre-built way to parse JSON from VB6?

Is there any existing JSON parser which can be used from VB6?
I could obviously write my own parser, but I don't want to reinvent the wheel if I don't have to.
Thanks!
I would look at VB-JSON:
VB-JSON is a Visual Basic 6 class library for parsing and emitting JSON (Javascript Object Notation) and can handle nested arrays and objects in the data. It does not rely on the JScript engine for parsing.