☑ Access json array

Hello, i wanted to access to a json file wich contain an array, i have tried using the code in the tutorial but it doesn’t work. Maybe it’s because i’m still using old script method? Anyway when i refresh the script attributes it give an error (red line in the bottom of editor), tried different things but nothing seems work…start to get depressed…all my efforts crash against errors lately

i have solved the first issue, now i have got an error in json and don’t see why

{ID: 0, type: “BLADE”, damageValue: 1, name: “Knife”, value: “10”, resistence: “30”, twohand: false, obj:""},
{ID: 1, type: “BLADE”, damageValue: 2, name: “Dagger”, value: “20”, resistence: “35”, twohand: false, obj:""},
{ID: 2, type: “BLADE”, damageValue: 3, name: “Short sword”, value: “30”, resistence: “40”, twohand: false, obj:""},
{ID: 3, type: “BLADE”, damageValue: 4, name: “Broad sword”, value: “40”, resistence: “50”, twohand: false, obj:""},
{ID: 4, type: “BLADE”, damageValue: 5, name: “Long sword”, value: “50”, resistence: “60”, twohand: false, obj:""},
{ID: 5, type: “BLADE”, damageValue: 6, name: “Claymore”, value: “60”, resistence: “70”, twohand: true, obj:""},
{ID: 6, type: “BOW”, damageValue: 5, name: “Short bow”, value: “40”, resistence: “50”, twohand: true, obj:""},]

Unexpected token I in JSON at position 3

Check to see if that is valid JSON.

(Hint: it is not… you need quotes around all the property names)

Ops…i see what u mean… i just copied the array i had in js :stuck_out_tongue:

Also, you have a trailing comma on the last element of your array. Should be something like:

    { "ID": 0, "type": "BLADE", "damageValue": 1, "name": "Knife", "value": "10", "resistence": "30", "twohand": false, "obj": ""},
    { "ID": 1, "type": "BLADE", "damageValue": 2, "name": "Dagger", "value": "20", "resistence": "35", "twohand": false, "obj": ""},
    { "ID": 2, "type": "BLADE", "damageValue": 3, "name": "Short sword", "value": "30", "resistence": "40", "twohand": false, "obj": ""},
    { "ID": 3, "type": "BLADE", "damageValue": 4, "name": "Broad sword", "value": "40", "resistence": "50", "twohand": false, "obj": ""},
    { "ID": 4, "type": "BLADE", "damageValue": 5, "name": "Long sword", "value": "50", "resistence": "60", "twohand": false, "obj": ""},
    { "ID": 5, "type": "BLADE", "damageValue": 6, "name": "Claymore", "value": "60", "resistence": "70", "twohand": true, "obj": ""},
    { "ID": 6, "type": "BOW", "damageValue": 5, "name": "Short bow", "value": "40", "resistence": "50", "twohand": true, "obj": ""}

Oh yes, i tried adding it to see if it was needed :stuck_out_tongue: lol now i remove it

A last question about json can i use a sintax like this inside json?
“weapons”: [{arrays}]
“potions”: [{arrays}]
and, if yes, how do i make it into 2 different arrays for weapons and potions?
or is better to make 2 separate json?

Yes, you can do that:

	"weapons": [
			"name": "pistol",
			"range": 10,
			"accuracy": 10,
			"power": 4
			"name": "shotgun",
			"range": 3,
			"accuracy": 7,
			"power": 10
	"potions": [
			"name": "heal",
			"enhancement": "health",
			"bonus": 10
			"name": "superstrength",
			"enhancement": "strength",
			"bonus": 10

It’d be perfectly valid to create two JSON files if you wish though. It’s up to you.

Ok i have created the json with “weapons”: inside. then i tried the code in the tutorial like this
var dbase = this.dbase.resource;
var weapons = this.parseCharacterData(dbase);
but this.dbase.resource return a undefined, while this.dbase return the id of the json object
since i’m not using 2.0 scripts have i to use a different line?
PS i have undefined also if i use this.dbase.weapons

tried also var dbase = JSON.parse(this.dbase.weapons)… no result

If dbase is ID of asset, then you need to get asset object: app.assets.get(id), then use its resource. If resource is string - you need to JSON.parse it, or it could be already a JSON, then no parsing required.

since it’s not a json i parsed it like this
var dbase = JSON.parse(app.assets.get(this.dbase));
and the console log return object OBJECT
and it give an error [launch:1]: Uncaught SyntaxError: Unexpected token o in JSON at position 1
var dbase=app.assets.get(this.dbase);
and after
var dw=data.resource

Please refer to documentation to know what is what: http://developer.playcanvas.com/en/api/pc.Asset.html
And autocomplete in IDE helps as well. Brutforce-trying to put anything just wont work.

Another useful way to know what is what, is simply console.log(thing); and check console what it is. That gives you better idea what variable contains and in dev tools you can inspect what it contains.

1 Like