How do I reach nested json objects from array

0 favourites
  • 6 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi, using websocket, my client recevie this :

    var data =

    {"c2dictionary":true,

    "data":{

    "contenu":{

    "0":{"id":1,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},

    "1":{"id":2,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},

    "2":{"id":3,"what":"rock","qt":1,"owner_id":59,"screen_x":516,"screen_y":373},

    "3":{"id":4,"what":"rock","qt":1,"owner_id":59,"screen_x":639,"screen_y":287}},

    "return":"load_objects_return"

    }}

    It works very good. I can see the whole JSON string in console. I used to load my json message into a dictionnary (called jmessage) so it's very convenient to reach data.

    In that case I do :

    Jmessage : Load JSON from string data

    then :

    jmessage.get("return")

    is correct with "load_object_return" as value. But jmessage.get("contenu") is supposed to contain the object list :

    :{

    "0":{"id":1,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},

    "1":{"id":2,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},

    "2":{"id":3,"what":"rock","qt":1,"owner_id":59,"screen_x":516,"screen_y":373},

    "3":{"id":4,"what":"rock","qt":1,"owner_id":59,"screen_x":639,"screen_y":287}}

    but it returns 0. I believe that's because a dictionnary can not contain a object list as a value.

    So how can I do to retrieve the object list from the original string data ?

    UPDATE : I realize the nested data wasn't at the C2 dictionary format. So I changed the string like this :

    {"c2dictionary":true,"data":{"contenu":{"c2dictionary":true,"data":{"0":{"id":1,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},"1":{"id":2,"what":"rock","qt":1,"owner_id":59,"screen_x":202,"screen_y":314},"2":{"id":3,"what":"rock","qt":1,"owner_id":59,"screen_x":516,"screen_y":373},"3":{"id":4,"what":"rock","qt":1,"owner_id":59,"screen_x":639,"screen_y":287}}},"return":"load_objects_return"}}

    So I can load jmessage.get("contenu") as a dictionnary. But this one remains empty from data. Because it's supposed to load values and not objects ... I don't need to store the objects but I would need to list it in a loop...

    UPDATE 2 :

    I managed to load some json object data into an array.

    But i'm unable to extract it !

    In debug mode I can see there is the data into the array.

    But when I do :

    foreach X,Y,Z from Array

    log Array.CurValue

    and str(Array.CurValue) doesn't help ...

    Can you help, anyone ?

  • check this [plugin] JSON

    {
    	"0": {
    		"id": 1,
    		"what": "rock",
    		"qt": 1,
    		"owner_id": 59,
    		"screen_x": 202,
    		"screen_y": 314
    	},
    	"1": {
    		"id": 2,
    		"what": "rock",
    		"qt": 1,
    		"owner_id": 59,
    		"screen_x": 250,
    		"screen_y": 314
    	},
    	"2": {
    		"id": 3,
    		"what": "rock",
    		"qt": 1,
    		"owner_id": 59,
    		"screen_x": 516,
    		"screen_y": 373
    	},
    	"3": {
    		"id": 4,
    		"what": "rock",
    		"qt": 1,
    		"owner_id": 59,
    		"screen_x": 639,
    		"screen_y": 287
    	}
    }[/code:3f1k6ccl]
    
    [url=https://app.box.com/s/jdmylg8x6jyujcz8v6eubimw28zb2e5o]json2example.capx[/url]
  • ho well thanks, I managed to use the plugin. Now I'm just afraid I wasted my time with dictionnaries ? We'll see.

  • Your format is wrong. This is what a dictionary of dictionaries looks like, in C2:

    {"c2dictionary":true,"data":{"dict2":"{\"c2dictionary\":true,\"data\":{\"one\":\"data 1\",\"two\":\"data 2\"}}","dict3":"{\"c2dictionary\":true,\"data\":{\"d3_one\":\"some more data\"}}"}}

    The general format must match, or it won't work.

  • I see, thanks Blackhornet. The JSON plugin also do it better in the end.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ho well thanks, I managed to use the plugin. Now I'm just afraid I wasted my time with dictionnaries ? We'll see.

    Yea, JSON support is one of C2's weak spots. The plugins are far better.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)