Embedded JSON parsing (Dictionary Load)

Get help using Construct 2

Post » Mon Mar 27, 2017 10:13 am

Hey there!

So, i have a string representation of some JSON, lets say I retrieved this string from a Websocket-based backend.

Getting the string is fine, but I want to inspect and observe all keys and values, including any embedded documents or arrays.

Given the string response:

myJSON = "{"key": "value", "embedded_key: {"embedded_val_1": 0, "embedded_val_2": [1,2,3,4,5]}}"

...I can load this such as:

Dictionary.Load( "{""c2dictionary"":true, ""data"": " & myJSON & " }"

Which results in a dictionary with 2 keys.
"key": "value"
"embedded_key": [Object object].

Any tips for parsing down the whole line of documents? Should I maybe use some custom plugin that handles JSON better?

Failing that, can anyone recommend any string manipulation utilities in Construct2 for splitting strings into sub-documents?
B
2
Posts: 2
Reputation: 154

Post » Mon Mar 27, 2017 6:10 pm

You will probably have to use a plugin that can readily interpret any JSON thrown at it. (JSON++ by Nandynho, JSON by Yann)

I guess you could also intercept the JSON before loading it as a Dictionary and turn every value in strings, so they could later be loaded into their own dictionaries... there would still be the problem of embedded-embedded objects... but the same logic could still be recursively used.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Mon Mar 27, 2017 6:53 pm

Most of the heavy lifting of parsing/string manipulation can be done in base C2 with the tokenat() expression.

Where that gets too clunky, the regex() expressions should be able to cover any situation, given you're familiar with regex.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,615
Reputation: 61,651

Post » Tue Mar 28, 2017 8:43 am

Message: Terracorrupt can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Magistross wrote:You will probably have to use a plugin that can readily interpret any JSON thrown at it. (https://www.scirra.com/forum/plugin-json_t178836]JSON++ by Nandynho[/url], [url=https://www.scirra.com/forum/plugin-json-import-export-generate-edit-inspect_t100042 JSON by Yann)

I guess you could also intercept the JSON before loading it as a Dictionary and turn every value in strings, so they could later be loaded into their own dictionaries... there would still be the problem of embedded-embedded objects... but the same logic could still be recursively used.


Ah, the JSON plugin by Yann did the trick! I can drill down into the structure and reference specific keys for data.

Thanks!
B
2
Posts: 2
Reputation: 154


Return to How do I....?

Who is online

Users browsing this forum: Blendi1223, Yahoo [Bot] and 54 guests