Embedded JSON parsing (Dictionary Load)

0 favourites
  • 4 posts
  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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:14r0to40]JSON++ by Nandynho, [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!

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