How do I parse string and find specific values

0 favourites
  • 11 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I'd like to parse JSON like this:

    "otherData":"not important",
    "otherData2":"also not important",
    "otherData3":"also not important",
    And I'd like to:
    1. find and retrieve value for key "name" (it should be "john") and save it as variable
    2. find and retrieve value for key "status" (it should be 0) and save it as variable
  • If you load an array with this JSON, you should retrieve the value at,1) for the name.

    If the name index isn't fixed, you can use a foreach and check when you reach "name" then get the name.

  • Index isn't fixed. Could you please explain in little more detail how to use foreach on JSON?

  • Have a look at Rex Rainbows Hash Plugin! It's awesome!

  • Load in an array, then use the "for each element" event for this array. Then use the sub event "compare current value" ... If you don't get it i'll try to make a capx later the evening.

  • - Do you mean Hash Table plugin ()?

    Joskin - I understand. But to import JSON into array I should have "{"c2array":true,"size":[10,2,1]" and i should have an array with predefined size.

  • From a pure parsing point of view you could use something like this. If C2 could use matching groups with regex, it would fall to only one line of code, but anyhow, this should do !

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yes tecbug: you can compare it with an associative array. And you can set keys and ask if keys are set.

  • Thak you all for replies.

    Especially Magistross - your example is incredibly helpful. I've changed expression newline to "," to separate elements by commas despite new lines.

    I have one problem with it, when I change JSON into:

    {"otherData":"not important","otherData2":"also not important","status":0,"playerData":{"name":"john","surname":"sesese","age":66}}[/code:108btinh]
    there's problem with reading nested data (eg. "name").
    And here is working example (with commas but without nesting): 
    [url=] ... se_v2.capx[/url]
  • I went ahead and changed the method to use RegEx instead of tokenization. It now works with nested data too. You'll notice I changed how the function is called, I now supply the input and one or more keys for the data to fetch. You can fetch "name" with a call like parse(input, "name") and it'll give you the data at the first occurence of the key "name", whatever its level of nesting... Or, you can call parse(input, "playerData", "name") and it'll give you the "name" of the first occurence of "playerData" in your input string.

    Here is the file.

  • WOW. Big thanks!

    The code looks like a magic I'll try to analyze it step by step.

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