How do I search through JSON?

Not favoritedFavorited Favorited 0 favourites
  • 3 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.
  • Hello;

    I'm building a game where screen information are stored in a JSON.

    First screen loaded is "screen_1" on which links can lead to other screens. When a link is clicked, next screen name is stored in a variable nextScreen and I'd like to perform a search in the JSON to check which level is this next screen and display according information

    Here is the JSON

    {
     "screens": [
     {
     "id": "screen_1",
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 1 flashing section egestas."
     }
     ]
     },
     {
     "id": "screen_2",
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 2 flashing section egestas."
     }
     ]
     },
     {
     "id": "screen_3",
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 3 flashing section egestas."
     }
     ]
     }
     ]
    }
    

    So far I managed to load the JSON correctily using AJAX. I can display info using the correct path but I've a hard time building the loop "for each" that search the next screen name through the JSON and display related information.

    I tried to add to the foreach loop something like :

    {...}JSON - For each entry in "screens"

    System - JSON.Get ("screens."&LoopIndex&".id") = nextScreen

    but it doesn't work...

    Tagged:

  • loopindex expression doesn't work in "For each entry" loop. You can use "Compare value" condition with a relative path.

    JSON For each entry in "screens"
    JSON Compare value ".id" = nextScreen
    

    Since each screen has a unique ID, it may be easier to change JSON structure to this:

    {
     "screens": {
     "screen_1": {
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 1 flashing section egestas."
     }
     ]
     },
     "screen_2": {
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 2 flashing section egestas."
     }
     ]
     },
     "screen_3": {
     "elements": [
     {
     "type": "text",
     "id": "instructions",
     "content": "Screen 3 flashing section egestas."
     }
     ]
     }
     }
    }
    

    This will allow to access any screen directly, without looping through the entire JSON. For example:

    JSON.Get("screens." & nextscreen & ".elements.0.content")

    .

    Also, if each screen has only a single element - get rid of the arrays of elements, this will make it even simpler:

    {
     "screens": {
     "screen_1": {
     "element_type": "text",
     "element_id": "instructions",
     "element_content": "Screen 1 flashing section egestas."
     },
     "screen_2": {
     "element_type": "text",
     "element_id": "instructions",
     "element_content": "Screen 2 flashing section egestas."
     },
     "screen_3": {
     "element_type": "text",
     "element_id": "instructions",
     "element_content": "Screen 3 flashing section egestas."
     }
     }
    }
    

    Usage:

    JSON.Get("screens." & nextscreen & ".element_content")

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That's perfectly clear ! Thank you

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