How do I how to search for data in json and separate it into array?

0 favourites
  • 7 posts
From the Asset Store
Searching
$16 USD
Searching is a game where you are challenged to find all the images with the requested letter in 30 seconds.
  • Hi guys, I want to separate data that comes from json, in fact the data is coming from my api, I want to treat this data so that it is saved in an array, for example, the user in the database has two motorcycles of different rarities, I want to get the data coming in this way {"query":[{"id":1,"rarity":"1","animacao":"1"},{"id":2," rarity":"2","animacao":"2"}]}

    and save it in the array, however I want to get only the value of the key and save it in each field of the array, for example rarity 1 is a motorcycle, and rarity 2 is another motorcycle. I would like a light. after it is already saved in the array, so the user can select which bike he will race with

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You need to request the data with AJAX, then parse it into a JSON object. After that you can read data from JSON and copy it into an array if you wish so.

    For example, to get the rarity of the second bike:

    JSON.get("query.1.rarity")

    See the official "JSON" example in Construct.

  • I need to do this dynamically, because this data comes directly from the database and I need to put everything that comes from each rarity into the array, for example, I want to put rarity: 1, 2 and so on without having to keep doing several events for this to happen, as users will be able to have N bikes of different or the same rarities on their account

  • I'm trying to do it like this, but without success, the array doesn't contain the key data I'm looking for.

  • You can't use "Array Load" action, it's for loading JSON in a very specific format.

    You need to read values from your JSON and insert them into the array one by one. (Although I don't understand why you want to use the array, it would be easier to work with JSON directly)

    Anyway, you need to do something like this:

  • I managed to do it another way, using insert, because I will need this data not to be persisted in the front, as several things will only happen in the back end, so I made an insert looking for the data that comes from the api. In this case, it looked like this:

  • Ok, just be aware that you are saving a string of two values in one element of the array. If you retrieve the element, you will get a string like "3,8". Which is not the same as numbers 3 and 8.

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