Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to Decoding and Reading JSON?

Asked by 9 years ago

So I have been looking at the tutorials on the wiki but I cannot seem to understand how to use JSON. I have this based off of the tutorials:

game.Players.PlayerRemoving:connect(function(playerL)
    if playerL:WaitForDataReady() then
        local xp = playerL.xp
        local kills = playerL.kills
        local deaths = playerL.deaths
        local cash = playerL.cash
        local data = {
            xp = playerL.xp,
            kills = playerL.kills,
            deaths = playerL.deaths
            cash = playerL.cash,
            name = playerL.userID
        }
        local RbxUtility = LoadLibrary("RbxUtility")
        player:SaveString('data', RbxUtility.EncodeJSON(data))
        end
    end
end)

I am not exactly sure if it works because I have not been able to test it yet (haven't gotten to it), but assuming this works, how would I decode, because if I am correct JSON decodes into a table, but how would I pick out the info from the table? This is what I have but it is incomplete:

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then
        local RbxUtility = LoadLibrary("RbxUtility")
        player:LoadString('data', RbxUtility.DecodeJSON(data))
        player.xp = xp          
        end
    end
end)


EDIT: The reason I am using JSON is because I don't want to have to worry about the Data Limit later on as much. Is this a valid reason, like should I be worried or should I use a different way?

EDIT (again): I looked over this again, and did some more research and I still don't really understand.

1 answer

Log in to vote
0
Answered by
RM0d 305 Moderation Voter
9 years ago

Use HTTPService as it officially as two functions for JSON.

First we should get the HTTPService

local http = game:GetService("HttpService")

What is JSON?

JSON is short for JavaScript Object Notation,and is a way to store information in an organized manner instead of loads of variables.

In javascript this is how we would display a JSON array

var Me = { "age" : "14", "gender" : "male" };

Please note that an Array is like a dictionary. It has a key and a value In lua the table would look like this

local Me = {["age"]=14,["gender"]="male"}

We can convert them to a string in the form of a JSON array or we can convert the string into a lua array.

http:JSONEncode([Table/Variant Input?])

This Encodes a table into a string

local jsonstringraw=http:JSONEncode(Me)
print(jsonstringraw) 

{"gender":"male","age":14}

As you can see it looks like the JSON array above, of course on a single line this time.

We can wrap the string back into a table simply by calling

http:JSONDecode([String])

So if we call

local jsonarray = http:JSONDecode(jsonstringraw)
print(jsonarray.gender) 

14

0
First time writing a full fledged answer. Please love me. RM0d 305 — 9y
0
Lol and thanks but I decided, after MOAR RESEARCH, to use Data Stores. xD but thank you for the answer, I learned stuff :D, hopefully someone will come here and find it helpful. patch523 20 — 9y
0
Patch can you upvote if it helped thanks :D RM0d 305 — 9y
Ad

Answer this question