How to retrieve data from an array using AJAX

0 favourites
  • 3 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • I apologize for asking such a basic question.

    Using AJAX, I obtained the following array data:

    {"score_table":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    I would like to retrieve each piece of data from this array and store it in variables.

    (For example) Store the value 1 from the array into a global variable named id.

    set id to 1

    From what I have researched, I believe I need to use tokenat(AJAX.LastData,,) but how exactly should I proceed to retrieve the data?

  • That data is in JSON format. You need to parse it into JSON object, then you will be able to extract the values.

    JSON parse AJAX.LastData
    Set variable to JSON.get("score_table.id")
    

    Notice that "id" is a number, but other values are strings, for example "position_y":"0". You will need to convert them to numbers using int() expression.

    Set var to int(JSON.get("score_table.position_y"))

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I was able to successfully reflect the data obtained in the game!

    Thank you for your detailed explanation regarding the conversion to numeric types as well.

    My concerns have been resolved, and I’ll continue enjoying game development!

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