SebTDZ

Json Test

Aug 2nd, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. local HttpService = game:GetService("HttpService")
  2.  
  3. -- An example JSON formatted string
  4. -- For details, see: http://www.json.org/
  5. local jsonString = [[
  6. {
  7.     "message": "success",
  8.     "info": {
  9.         "points": 120,
  10.         "isLeader": true,
  11.         "user": {
  12.             "id": 12345,
  13.             "name": "JohnDoe"
  14.         },
  15.         "past_scores": [50, 42, 95],
  16.         "best_friend": null
  17.     }
  18. }
  19. ]]
  20.  
  21. local data = HttpService:JSONDecode(jsonString)
  22. if data.message == "success" then
  23.     -- Print the sample data
  24.        
  25.     -- Since tab["hello"] and tab.hello are equivalent,
  26.     -- you could also use data["info"]["points"] here:
  27.     print("I have " .. data.info.points .. " points")
  28.     if data.info.isLeader then
  29.         print("I am the leader")
  30.     end
  31.     print("I have " .. #data.info.past_scores .. " past scores")
  32.    
  33.     print("All the information:")
  34.     for k, v in pairs(data.info) do
  35.         print(k, type(v), v)
  36.     end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment