How do I parse JSON Data in a loop?

Get help using Construct 2

Post » Sun May 08, 2016 12:24 pm

Hi. Does anyone have an example how I can use a loop on AJAX.LastData with JSON like below to get a list of all realm names and the realm data for each of the realms?

example for a single realm:
Code: Select all
{"realms":[{"type":"rp","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":1,"status":0,"next":1462710723768},"tol-barad":{"area":21,"controlling-faction":0,"status":1,"next":1462710700949},"status":true,"name":"Zirkel des Cenarius","slug":"zirkel-des-cenarius","battlegroup":"Embuscade / Hinterhalt","locale":"de_DE","timezone":"Europe/Paris","connected_realms":["zirkel-des-cenarius","todeswache"]}]}


example for multiple realms:
Code: Select all
{"realms":[{"type":"pvp","population":"high","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1462710732001},"tol-barad":{"area":21,"controlling-faction":0,"status":0,"next":1462712427270},"status":true,"name":"Aegwynn","slug":"aegwynn","battlegroup":"Misery","locale":"de_DE","timezone":"Europe/Paris","connected_realms":["aegwynn"]},{"type":"pve","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":1,"status":0,"next":1462710724874},"tol-barad":{"area":21,"controlling-faction":1,"status":1,"next":1462710703055},"status":true,"name":"Aerie Peak","slug":"aerie-peak","battlegroup":"Reckoning / Abrechnung","locale":"en_GB","timezone":"Europe/Paris","connected_realms":["bronzebeard","aerie-peak"]},{"type":"pvp","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1462710910703},"tol-barad":{"area":21,"controlling-faction":0,"status":1,"next":1462710887883},"status":true,"name":"Agamaggan","slug":"agamaggan","battlegroup":"Reckoning / Abrechnung","locale":"en_GB","timezone":"Europe/Paris","connected_realms":["agamaggan","crushridge","bloodscalp","twilights-hammer","hakkar","emeriss"]}


I tried plugin-json-import-export-generate-edit-inspect_p1034911?#p1034911 but I get an error when I try to load the AJAX data.
B
53
S
16
G
6
Posts: 718
Reputation: 9,150

Post » Sun May 08, 2016 2:11 pm

you have error in example for multiple realms..you end with
Code: Select all
]}

should be
Code: Select all
]}]}

Code: Select all
{
  "realms": [
    {
      "type": "pvp",
      "population": "high",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462710732001
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462712427270
      },
      "status": true,
      "name": "Aegwynn",
      "slug": "aegwynn",
      "battlegroup": "Misery",
      "locale": "de_DE",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "aegwynn"
      ]
    },
    {
      "type": "pve",
      "population": "medium",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 1,
        "status": 0,
        "next": 1462710724874
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 1,
        "status": 1,
        "next": 1462710703055
      },
      "status": true,
      "name": "Aerie Peak",
      "slug": "aerie-peak",
      "battlegroup": "Reckoning / Abrechnung",
      "locale": "en_GB",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "bronzebeard",
        "aerie-peak"
      ]
    },
    {
      "type": "pvp",
      "population": "medium",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462710910703
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 0,
        "status": 1,
        "next": 1462710887883
      },
      "status": true,
      "name": "Agamaggan",
      "slug": "agamaggan",
      "battlegroup": "Reckoning / Abrechnung",
      "locale": "en_GB",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "agamaggan",
        "crushridge",
        "bloodscalp",
        "twilights-hammer",
        "hakkar",
        "emeriss"
      ]
    }
  ]
}


realmsJSON.capx

Edit:

also..perhaps would be better to write json for multiple realms
like this:
Code: Select all
{
  "realm1": [
    {
      "type": "pvp",
      "population": "high",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462710732001
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462712427270
      },
      "status": true,
      "name": "Aegwynn",
      "slug": "aegwynn",
      "battlegroup": "Misery",
      "locale": "de_DE",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "aegwynn"
      ]
    }
  ],
  "realm2": [
    {
      "type": "pve",
      "population": "medium",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 1,
        "status": 0,
        "next": 1462710724874
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 1,
        "status": 1,
        "next": 1462710703055
      },
      "status": true,
      "name": "Aerie Peak",
      "slug": "aerie-peak",
      "battlegroup": "Reckoning / Abrechnung",
      "locale": "en_GB",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "bronzebeard",
        "aerie-peak"
      ]
    }
  ],
  "realm3": [
    {
      "type": "pvp",
      "population": "medium",
      "queue": false,
      "wintergrasp": {
        "area": 1,
        "controlling-faction": 0,
        "status": 0,
        "next": 1462710910703
      },
      "tol-barad": {
        "area": 21,
        "controlling-faction": 0,
        "status": 1,
        "next": 1462710887883
      },
      "status": true,
      "name": "Agamaggan",
      "slug": "agamaggan",
      "battlegroup": "Reckoning / Abrechnung",
      "locale": "en_GB",
      "timezone": "Europe/Paris",
      "connected_realms": [
        "agamaggan",
        "crushridge",
        "bloodscalp",
        "twilights-hammer",
        "hakkar",
        "emeriss"
      ]
    }
  ]
}



realms2JSON.capx
Image
B
64
S
23
G
7
Posts: 1,483
Reputation: 10,827


Return to How do I....?

Who is online

Users browsing this forum: imothep85, KTML5 and 9 guests