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

Error in extracting JSON strings?

Asked by 8 years ago

Basically I have a ROBLOX+ API (an API that is user-created that extracts data from the ROBLOX site) and I'm trying to get the game to return the RAP ( Roblox Average Price) from a user using a User ID. I'm using ROBLOX's User ID which is 1 as an example of how I might do so.

Here is the code I have based on the Blog Post on how to get rep (reputation) from someone on ScriptingHelpers.

function getRap(Player)
    local ID = 1 
    local link  = "http://roblox.plus:2052/inventory?id=".. ID 
    local Service = game:GetService("HttpService")
    local html = Service:GetAsync(link, false)
    local string = [[<div>"rap":%d+</div>]]
    local snippet = html:match(string)
    RAP = (snippet:match("%d+"))

    return ("ROBLOX has " .. RAP .. " ")
end

I was almost sure this would work, but the only thing the output returned was an error I never seen before.

HTTP 403 (HTTP/1.1 403 Forbidden): - path: user/multi-following-exists, 
json: {"otherUserIds":[38096393],"userId":38096393}
ServerSocialScript Loaded

I can't make sense of it. Is there a fix to this?

0
That isn't an error, that's just a weird thing studio says now when it launches a test. aquathorn321 858 — 8y
0
It only says that in the output when I run this script. andreagoingblue 0 — 8y

1 answer

Log in to vote
3
Answered by
XAXA 1569 Moderation Voter
8 years ago

Decode it into a Lua table with HttpService's JSONDecode. From there, you can easily index the RAP.

 local HttpService = game:GetService("HttpService")
 local Players = game:GetService("Players")

function getRapFromUserId(userid) -- I would suggest passing the player's ID rather than their player object
    local link  = "http://roblox.plus:2052/inventory?id=".. userid 
    local json = HttpService:GetAsync(link, false) -- get the json string
    local decoded = HttpService:JSONDecode(json) -- decode the json into a lua table we can index
    local rap = decoded.rap -- index rap from the decoded json
    return rap -- just return the rap
end

print(Players:GetNameFromUserIdAsync(1) .. " has a RAP of " .. getRapFromUserId(1) )

EDIT: If you really want to use string:match(), you made a mistake with your pattern matching. [[<div>"rap":%d+</div>]] should be [["rap":%d+]] instead since JSON doesn't even use <tag>s.

1
Be wary that you may have to wait several seconds until the request completes, so be patient. Also make sure that HttpRequests are enabled (You can check this in the properties of HttpService, it will error otherwise anyway) XAXA 1569 — 8y
0
There is an error on line 6 and line 12. Also if you visited the URL then you will probably see that the RAP is near the top and there is a big mess of code everywhere else. andreagoingblue 0 — 8y
1
It works. I've used this API before. It's just that the site is down as of now (http://roblox.plus:2052/inventory?id=1 will not work). If you go to that link right now in your browser, it will say "Error 522: Connection timed out" It might be fixed several hours from now. XAXA 1569 — 8y
0
I don't see any part of the script separating the RAP from the rest of the output. andreagoingblue 0 — 8y
1
local rap = decoded.rap XAXA 1569 — 8y
Ad

Answer this question