How do I parse string and find specific values

Get help using Construct 2

Post » Wed Mar 05, 2014 3:30 pm

I'd like to parse JSON like this:
Code: Select all
{
"otherData":"not important",
"otherData2":"also not important",
"name":"john",
"status":0,
"otherData3":"also not important",
"otherNumber":666
}

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
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Mar 05, 2014 3:34 pm

If you load an array with this JSON, you should retrieve the value at array.at(2,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.
B
39
S
15
G
5
Posts: 295
Reputation: 6,089

Post » Wed Mar 05, 2014 3:41 pm

Index isn't fixed. Could you please explain in little more detail how to use foreach on JSON?
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Mar 05, 2014 3:42 pm

Have a look at Rex Rainbows Hash Plugin! It's awesome!
B
14
S
3
G
1
Posts: 255
Reputation: 2,027

Post » Wed Mar 05, 2014 3:45 pm

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.
B
39
S
15
G
5
Posts: 295
Reputation: 6,089

Post » Wed Mar 05, 2014 3:57 pm

@Rabenmutter - Do you mean Hash Table plugin (plugin-hash-table_t65765?&hilit=Rex+Hash+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.
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Mar 05, 2014 4:35 pm

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 ! ;)
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Thu Mar 06, 2014 6:23 am

Yes tecbug: you can compare it with an associative array. And you can set keys and ask if keys are set.
B
14
S
3
G
1
Posts: 255
Reputation: 2,027

Post » Thu Mar 06, 2014 8:31 pm

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:
Code: Select all
{"otherData":"not important","otherData2":"also not important","status":0,"playerData":{"name":"john","surname":"sesese","age":66}}

there's problem with reading nested data (eg. "name").

--edit--
And here is working example (with commas but without nesting):
https://dl.dropboxusercontent.com/u/176 ... se_v2.capx
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Fri Mar 07, 2014 3:35 pm

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Next

Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 57 guests