How do I get values from json, browser

Get help using Construct 2

Post » Thu Dec 03, 2015 5:30 pm

Hey! I want to create a twitch chat thing, using the twitch API.
I never used json in C2, so i am kinda stuck xD
I've used the Browser Object to Invoke Download(URL), that creates a file in my downloads folder. Now i have no ideia how to use the file, and to parse the json :/
Is there a better way of doing this, without having to create the file.
thx in advance!
B
9
S
3
Posts: 32
Reputation: 707

Post » Thu Dec 03, 2015 5:58 pm

I haven't done anything with twitch chat before, but I have used json and parsing a fair amount.

So basically, json is just an encoded string with all the information you need arranged in a specific way (I've mostly only used it for arrays so this is what I'm basing that off of).

Most (all?) objects in construct 2 can be turned into text via Object.asJson (Try creating a text field and setting the text to the .asJson for one of your objects to see what it looks like). This can later be loaded via a "set from Json" to effectively recreate the object. (I do this a fair amount to duplicate arrays over multiple players to make sure they all have the same leaderboard/player information etc.)

But as this works like a charm getting objects from Construct to Construct, will it work for getting a string from twitch to construct? I don't know, you might have to parse the Json to get the useful information that you need.

So onto parsing a string. For this you need to use the tokenat() function. Json, as I mentioned before, is just a string arranged in a specific way that can be parsed to get the information.

For example, in the platformer example, the Json for the background is:
{"c2":true, "w":{"x":0,"y":0,"w":1280,"h":1024,"l"5904914194331451,"zi":0,"hX":0,"hY":0}}
As you can tell, it breaks the information up with commas, so if you wanted the width, tokenat(ThisJsonString, 3, ",") would give you:
"w":1280
And you could further parse that with tokenat(NewString, 1, ":") to get simply a string that is 1280. Now you've got the width.
You could even put those in the same line: tokenat(tokenat(ThisJsonString, 3, ","), 1, ":") to get 1280 if you wish.
B
11
S
3
G
1
Posts: 98
Reputation: 1,062

Post » Fri Dec 04, 2015 1:10 am

Nice!! the Object.asJson in the instancevarialbes[], each variable has an interesting key("7492324802..", "john"), when the ivs was name : john, not rlly a problem just interesting.

The Problem now is getting the data from the web, i am trying to use the Browser Object, i can do the Invoke download with the url, and it creates a json file, with the correct information, but i really don't want a file, i would like to have either a string, or something that i can use :/
B
9
S
3
Posts: 32
Reputation: 707

Post » Fri Dec 04, 2015 3:29 am

I'm noob at C2 this but 2 things come to mind that I'd try.

If you have the local file name from the browser download and you can figure out how to turn that into a url, you can pass that to an Ajax Request and then, load it into a dictionary as a json. I'm using the Ajax request to load file chooser selections into a textbox.

If Twitch allows Ajax connections (it'll throw an Ajax on error if it doesn't) you could bypass the browser and load the ajax results of get or post straight into a dictionary as json.
B
15
S
6
Posts: 124
Reputation: 1,737

Post » Fri Dec 04, 2015 11:58 am

i've tried using Ajax directly to get the results, didin´t allow, error: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Is there a way to lead a file automatically, if i know the name of the file, could i tell to open it, without having to manually open it with file choser?

If the Browser Object manages to create the correct json file, it means at some point it had the byte[] data, so it should be possible to access this, or not, i dunno. xD
B
9
S
3
Posts: 32
Reputation: 707

Post » Fri Dec 04, 2015 1:52 pm

go for JSON tools such as codebeautify.org/jsonviewer and jsonformatter.org to debug and validate JSON data.
B
2
Posts: 2
Reputation: 158

Post » Fri Dec 04, 2015 11:59 pm

I'm curious, have you tried this?

In your browser invoke download, instead of a file name, try dictionary.asJSON.

I know it'll allow it, whether it works or not...
B
15
S
6
Posts: 124
Reputation: 1,737

Post » Sat Dec 05, 2015 6:48 pm

@Fengist that would be cool, and it is allowed, but it doenst work, the Invoke download just creates the file
B
9
S
3
Posts: 32
Reputation: 707


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 17 guests