Struggle with JSON Paths

0 favourites
  • 7 posts
From the Asset Store
Rectangular Path
$2 USD
60% off
Move around the rectangular and travel as much as you can!
  • Struggling with figuring out how loading in JSON works; why exactly does this not work?

  • "hammers" in your JSON is an array. If it contains more than one record, and you don't know the exact index, you will need to loop through it to find the right hammer record.

    JSON For Each entry in "hammers" 
    JSON Has Key "." & hammer.AnimationName 
    ---> Wheel add JSON.get("." & hammer.AnimationName & ".speed") to speedTotal 
     
    

    When a path starts with "." it's a relative path. For example, once you set the path to "hammers.0.Basic", after that you can get speed value by a relative path ".speed":

    JSON Set path to "hammers.0.Basic" 
    Wheel add JSON.Get(".speed") to speedTotal 
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If all hammer names are unique, it may be easier to get rid of the array and change your JSON to this:

    { 
    	"hammers": { 
    		"Basic": {"damage": 2, "speed": 3}, 
    		"Enchanted": {"damage": 4, "speed": 3}, 
    		"Massive": {"damage": 10, "speed": 1} 
    	} 
    } 
    

    Then you will be able to access any hammer stats directly, for example:

    JSON.Get("hammers.Massive.speed")

  • Okay, that makes a lot more sense. Thank you, I set it to not be an array and changed up the code but now it's only adding the value after the second time I attach it. Good to know it's at least adding the value, but is there something wrong with my order of operations?

  • AJAX request is an asynchronous action, it is not completed immediately.

    Better move it to "On Start Of Layout" event, you don't need to read the file on every tap. And add "System Wait for previous action to complete" between the "AJAX request" and "JSON Parse".

  • That worked, thank you! I feel like I have a better understanding of how running JSON works, it's very powerful!

  • Cheatsheet:

    Level 1

    { 
     "name": "Cheems", 
     "age": 18, 
     "score": 100 
    } 
    

    return "Cheems"

    JSON.Get("name") 
    

    -----

    Level 2

    { 
     "team":  
     { 
     "name": "Cheems", 
     "age": 18, 
     "score": 100 
     } 
    } 
    

    return "Cheems"

    JSON.Get("team.name") 
    

    -----

    Level 3: Array

    { 
     "array": [123, 456] 
    } 
    

    return 123

    JSON.Get("array.0") 
    

    -----

    Level 4: Array

    { 
     "team":  
     [ 
     { "name": "Cheems", "age": 18, "score": 100 }, 
     { "name": "Marmot", "age": 17, "score": 95 } 
     ] 
    } 
    

    return "Cheems"

    JSON.Get("team.0.name") 
    

    -----

    Level 5:

    Note that it does not start with {}, but [] as a JSON array

    [ 
     { 
     "name": "Cheems", 
     "age": 18, 
     "score": 100 
     }, 
     { 
     "name": "Marmot", 
     "age": 17, 
     "score": 95 
     } 
    ]

    return "Cheems"

    JSON.Get("0.name") 
    

    -----

    Level 6:

    { 
     "team": [ 
     { 
     "name": "Cheems", 
     "age": 18, 
     "score": 100, 
     "fruit": ["Apple", "Banana", "Cherry", "Durian"] 
     }, 
     { 
     "name": "Marmot", 
     "age": 17, 
     "score": 95, 
     "fruit": ["Apple","Berry"] 
     } 
     ] 
    } 
    

    return "Banana"

    JSON.Get("team.0.fruit.1") 
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)