dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: JSON structure within AS3 - opinions?

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    2

    JSON structure within AS3 - opinions?

    I'm looking for opinions on the two below JSON data structures when working with AS3. The 2nd JSON sample apparently allows for easier dot notation in AS3 with something like Menu.item1 whereas the 1st option requires Object.menuitem[1].


    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++
    1st JSON Sample
    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++

    "menuitem": [
    {
    "value": "New",
    "onclick": "CreateNewDoc()"
    },
    {
    "value": "Open",
    "onclick": "OpenDoc()"
    },
    {
    "value": "Close",
    "onclick": "CloseDoc()"
    }
    ]




    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++
    2nd JSON Sample
    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++


    "keys" = [
    "item0",
    "item1",
    "item2"
    ],
    "item0": {
    "value": "New",
    "onclick": "CreateNewDoc()"
    },
    "item1": {
    "value": "Open",
    "onclick": "OpenDoc()"
    },
    "item2": {
    "value": "Close",
    "onclick": "CloseDoc()"
    }

    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++


    The 2nd sample also apparently allows for:

    1. keys allow to separate form and content
    Example: you need to quickly hide one item but you want to keep the data there. Easy: just temporarily remove its key:
    "keys" = ["item0", "item1"],
    "item0": {"value": "New", "onclick": "CreateNewDoc()"},
    "item1": {"value": "Open", "onclick": "OpenDoc()"},
    "item2": {"value": "Close", "onclick": "CloseDoc()"}
    ...and item2 will not be shown, code is still there though. Key can easily be retrieved as well (“item2”.
    While here:
    "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"} ]
    Well, I had to really remove the data...




    2. keys allow greater flexibility
    What if for whatever reason, I need to change all the keys to a different naming convention? A key is just a key, it can be anything as long as it is unique within a given structure. While this doesn’t really make much sense in the context of this sample, “item0” could be anything. The developer using that data does not need to know what the key is to access the data.




    3. it gives choice
    Keys CAN be totally disregarded. It is exactly like, in OO, using public static properties to describe constants. I.e.: using Error.ARGUMENT_ERROR versus “argumentError”. You are not forced to use it. By using them though, you’re allowing the keys to change in the future and not use hard-coded values.
    In our rewritten little example, you COULD use:
    object.item0
    instead of the more proper:
    object[object.keys[0]]
    ...but by doing that you’re not allowing “item0” to be renamed to anything without breaking your code.



    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++



    The issue we have run into is deserializing the 2nd JSON sample in a .net object when the JSON string is sent back to the server. The 1st JSON sample is easy to deserialize in .net.

    Please take into account when giving your opinion that all JSON generated is from .net and it needs to read values from the JSON when sent back to server-side.

    Thanks for your time!

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Why not combine the two and reconstitute your key lookup table in AS later?

    PHP Code:
    "menuitem": [
        {
            
    "value""New",
            
    "onclick""CreateNewDoc()",
            
    "key":"item0"
        
    },
        {
            
    "value""Open",
            
    "onclick""OpenDoc()",
            
    "key":"item1"
        
    },
        {
            
    "value""Close",
            
    "onclick""CloseDoc()",
            
    "key":"item2"
        


    PHP Code:
    //  in AS
    const ITEM_BY_INDEX:Array = JSON.deserialize(someString) as Array;
    const 
    ITEM_BY_KEY:Array = [];

    for 
    each(var o:Object in ITEM_BY_INDEX){
        if(
    o.hasOwnProperty('key')){
            
    ITEM_BY_KEY[o.key] = o;
        }

    Depending on what you're using the keys for you could even add that back in and use it for controlling visibility or ordering or whatever - although personally I think it would be better to just use a flag inside the item (something like disabled:true or whatever).

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    2
    Thanks for the reply neznein9!

    I would agree with you on the data structure by including the key how you have. Do you see any issues with the coding in AS? I'm the .net guy serializing and deserializing the JSON and I've got the AS guy telling me his structure is easier/better to work with.

    thanks again.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Pfff - tell your AS guy to quit whining - that loop to recreate the key array is like 3 lines.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center