JSON manipulation pack

DoctorMikeReddy

  • *
  • Posts: 180
I looked through your record of posts and couldn't see the post showing string manipulation of the jsonip.com data. Did I miss it? Could you repost?

Joe

  • *
  • Posts: 2478
I can't find it, but I've whipped up something better. This example recursively loops through the decoded JSON object. Some notes:

1) The current key (e.g., "firstName") and its corresponding value (e.g., "John") are accessible via two string Attributes.
2) This example requires code blocks and use a non-global custom block.
3) The actual iteration of a JSON object occurs in a random order. Any ordering you wanted to ensure would have to be included in the data itself and would have to be sorted after the fact, as might be important in the sentence example you give (e.g., "word index: 1", "word index: 2").

Hopefully this will be a good start for you.

herby

  • Posts: 124
It might also be possible for JSON data to use the same identifier multiple times, and I'm not sure what the Stencyl code would do with { "ip":"1.1.1.2", "ip":"1.1.1.3"} when looking for "ip"

I'm not sure it is possible do use multiple identifiers... anyway, JSON code just reuses existing AS3 JSON implementation, so the Stencyl blocks do what as3corlib implementation does.

herby

  • Posts: 124
for example, how would I parse:


{
   "glossary": {
        "title": "example glossary",
      "GlossDiv": {
            "title": "S",
         "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
               "SortAs": "SGML",
               "GlossTerm": "Standard Generalized Markup Language",
               "Acronym": "SGML",
               "Abbrev": "ISO 8879:1986",
               "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                  "GlossSeeAlso": ["GML", "XML"]
                    },
               "GlossSee": "markup"
                }
            }
        }
    }
}

This was a quick implementation for simple structures. You can use "as json" trick to get more that two levels deep:

set glossdiv to ((json source at glossary / GlossDiv) as json)
set glossentry to ((json glossdiv at GlossList / GlossEntry) as json)

... etc.

herby

  • Posts: 124
The pack doesn't appear to have a block to easily do that, but it would be a nice suggestion.

Yes. Iterator is nice suggestion.

The problem is lack of object ("struct" object, I'm not talking about classes with methods for now, just plain data holders) manipulation in Stencyl. No structures (beyond lists). This complicates using nested JSON structures, too. If above-mentioned thing was there, JSON code would only need one decode and one encode... the existing JSON manipulation pack does both - encodes and decodes, but as well lets you to somehow navigate the structure as well... the latter is not entirely its responsibilty, but it must do it when there is no way to read (at least) "struct" objects directly in Stencyl (write, too, of course).

herby

  • Posts: 124
There is a new version uploaded, with two enumerators, one simpler, one more complex.

DoctorMikeReddy

  • *
  • Posts: 180
It's broken on StencylForge: incomplete upload :-(

DoctorMikeReddy

  • *
  • Posts: 180
Just out of interest, how did you make the script and block versions of JSON work so that they could be called from any other script? I've tried to do the same, by converting Input Text (which has two scripts, one code and one blocks) so that the scripts don't need to be attached to a scene, but cannot get it to work. Any hints?

herby

  • Posts: 124
Uploaded again.

Use global custom block.

DoctorMikeReddy

  • *
  • Posts: 180
I've tried, but cannot get past syntax errors saying that the class may not be defined. How do you get JSON.<function name> to work?

DoctorMikeReddy

  • *
  • Posts: 180
in the code block I mean:

CODE return JSON.decode(_json)

herby

  • Posts: 124
Dirty tricks. JSON from this pack is not the usual as3corlib JSON, it's the merge of as3corlib version with Stencyl code block public class. Read first three posts in this thread and look into the JSON code block to find the details.

(not that I am proud of the dirty tricks, but Stencyl itself is restrictive, and any restrictions are only good for making people angry and forcing them to devise workarounds :-) )

DoctorMikeReddy

  • *
  • Posts: 180
OK. Clever, but aren't you worried that it will break when they "change things"

herby

  • Posts: 124
OK. Clever, but aren't you worried that it will break when they "change things"

It will. Hopefully there will be an option to include external libraries in normal way, so true JSON could be there by then... blocks will work with any of them, and the code was just a workaround to be tossed away when real solution comes.

DoctorMikeReddy

  • *
  • Posts: 180
Faceslap! private classes after the public class, which are only file accessible. OMG that's brilliant. Going to have to play with this.