How do I reach nested json objects from array

Get help using Construct 2

Post » Sat Dec 17, 2016 10:24 am

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 !

Image

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

Image

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

Can you help, anyone ?
B
8
S
4
Posts: 131
Reputation: 1,205

Post » Sat Dec 17, 2016 12:32 pm

check this [plugin] JSON


Code: Select all
{
   "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
   }
}


json2example.capx
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Sat Dec 17, 2016 12:47 pm

ho well thanks, I managed to use the plugin. Now I'm just afraid I wasted my time with dictionnaries ? We'll see.
B
8
S
4
Posts: 131
Reputation: 1,205

Post » Sat Dec 17, 2016 3:13 pm

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.
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Thu Dec 29, 2016 5:39 am

I see, thanks Blackhornet. The JSON plugin also do it better in the end.
B
8
S
4
Posts: 131
Reputation: 1,205

Post » Thu Dec 29, 2016 6:48 pm

c13303 wrote: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.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857


Return to How do I....?

Who is online

Users browsing this forum: androidpro, dop2000 and 16 guests