Page 1 of 1

JSON to Array with LiveCode 8

Posted: Tue Jun 20, 2017 2:31 pm
by pjfry
Hi there,

I try to access information via a web API. The API provides the data in JSON format. It likes like this:

Code: Select all

{
    "lastModified": 1497254226000,
    "name": "Sasinchen",
    "realm": "Zirkel des Cenarius",
    "battlegroup": "Embuscade / Hinterhalt",
    "class": 11,
    "race": 4,
    "gender": 1,
    "level": 110,
    "achievementPoints": 13275,
    "thumbnail": "zirkel-des-cenarius/199/102431431-avatar.jpg",
    "calcClass": "U",
    "faction": 0,
    "feed": [{
        "type": "CRITERIA",
        "timestamp": 1497208249000,
        "achievement": {
            "id": 7447,
            "title": "Mehr Spaß mit Freunden",
            "points": 10,
            "description": "Schließt 100 Szenarien in Gildengruppen ab.",
            "rewardItems": [],
            "icon": "achievement_scenariochallenge_100",
            "criteria": [{
                "id": 21188,
                "description": "",
                "orderIndex": 0,
                "max": 100
            }],
            "accountWide": false,
            "factionId": 2
        },
        "featOfStrength": false,
        "criteria": {
            "id": 21188,
            "description": "",
            "orderIndex": 0,
            "max": 100
        }
    }, {
        "type": "BOSSKILL",
        "timestamp": 1497208249000,
        "achievement": {
            "id": 10883,
            "title": "Siege über den Schemen von Xavius (Mythisch: Das Finsterherzdickicht)",
            "points": 0,
            "description": "",
            "rewardItems": [],
            "icon": "trade_engineering",
            "criteria": [{
                "id": 31384,
                "description": "",
                "orderIndex": 0,
                "max": 1
            }],
            "accountWide": false,
            "factionId": 2
        },
        "featOfStrength": false,
        "criteria": {
            "id": 31384,
            "description": "",
            "orderIndex": 0,
            "max": 1
        },
        "quantity": 14,
        "name": ""
    }, {
        "type": "LOOT",
        "timestamp": 1497206270000,
        "itemId": 138019,
        "context": "",
        "bonusLists": []
    }, {
        "type": "LOOT",
        "timestamp": 1497206270000,
        "itemId": 137513,
        "context": "challenge-mode-2",
        "bonusLists": [3414, 1532, 3528]
    }, {
        "type": "BOSSKILL",
        "timestamp": 1497206262000,
        "achievement": {
            "id": 10898,
            "title": "Siege über Cordana Teufelsang (Mythisch: Das Verlies der Wächterinnen)",
            "points": 0,
            "description": "",
            "rewardItems": [],
            "icon": "trade_engineering",
            "criteria": [{
                "id": 31415,
                "description": "",
                "orderIndex": 0,
                "max": 1
            }],
            "accountWide": false,
            "factionId": 2
        },
...
So I put a "Tree View" widget and a button on my card. The On MoseUp code of the Button looks like this:

Code: Select all

on mouseUp
   
   local tBlizzardCharFeed
   put url "https://eu.api.battle.net/wow/character/Zirkel%20des%20Cenarius/Sasinchen?fields=feed&locale=de_DE&apikey=5dxbkec7nrdju5tynks9dhg9kr86prrj" into tBlizzardCharFeed
   if the result begins with "error" then
      answer "Da stimmt was nicht"
      else
         answer "Das auslesen hat geklappt!"
         put JSONToArray(tBlizzardCharFeed) into tBlizzardCharFeed
         -- do something interesting with the profile
         set the ArrayData of widget "Tree View" to tBlizzardCharFeed
      end if
      
end mouseUp
This works fine and the data from the JSON API is put into the Tree View field. But I want only some specific Information put into th Tree View. So I have to "filter" the array in LiveCode but I have no idea how to get this working. I only want the following data:

From the "field" feed I like only the type BOSSKILL with all the corresponding data below. So if I look at the JSON file posted above I only want this data:

Code: Select all

{
        "type": "BOSSKILL",
        "timestamp": 1497206262000,
        "achievement": {
            "id": 10898,
            "title": "Siege über Cordana Teufelsang (Mythisch: Das Verlies der Wächterinnen)",
            "points": 0,
            "description": "",
            "rewardItems": [],
            "icon": "trade_engineering",
            "criteria": [{
                "id": 31415,
                "description": "",
                "orderIndex": 0,
                "max": 1
            }],
            "accountWide": false,
            "factionId": 2
        },
Do anybody have an idea how to get to this data in the array?

Thanks for your help

Benny

Re: JSON to Array with LiveCode 8

Posted: Wed Jun 21, 2017 3:36 pm
by jiml
Here's one way to do it.

Jim Lambert

Code: Select all

local tBlizzardCharFeed

on mouseUp
put url "https://eu.api.battle.net/wow/character/Zirkel%20des%20Cenarius/Sasinchen?fields=feed&locale=de_DE&apikey=5dxbkec7nrdju5tynks9dhg9kr86prrj" into tBlizzardCharFeed
if the result begins with "error" then
   answer "Da stimmt was nicht"
else
   answer "Das auslesen hat geklappt!"
   put JSONToArray(tBlizzardCharFeed) into tBlizzardCharFeed
   -- do something interesting with the profile
   put getTypes("BOSSKILL") into typeArray
   set the ArrayData of widget "Tree View" to typeArray  --tBlizzardCharFeed
end if

end mouseUp

function getTypes whatType
   repeat for each key x in tBlizzardCharFeed["feed"]
      add 1 to count
      if  tBlizzardCharFeed["feed"][x]["type"] = whatType then put tBlizzardCharFeed["feed"][x] into typeArray[count]
   end repeat
   return typeArray
end getTypes