How do I get values from json, browser

0 favourites
  • 8 posts
From the Asset Store
This is a single chapter from the Construct Starter Kit Collection and the Student Workbook from the Workshop.
  • 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!

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

  • 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 :/

  • Try Construct 3

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

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

  • 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

  • go for JSON tools such as codebeautify.org/jsonviewer and jsonformatter.org to debug and validate JSON data.

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

  • Fengist that would be cool, and it is allowed, but it doenst work, the Invoke download just creates the file

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