I have no problem reading and parsing your data, but it’s a bit oddly formatted with quite a few nested objects. This is the json from “groupname=test”:
{
"users": [
{
"user": {
"id": "28",
"gcm_regid": "eWuPthLZKMk:APA91bENXnADkD4b9nQFYqKASlIGUNNiMwNN6HcQ4_ZRDlA02RrhTE08Rih3UsxTSFzW2JzZ13_QLJLhELhAEZOLgNfwbDo9eOyTkH8LAkAyuvTbdNtEjDeGCR_OSMoBsXVUU7j7-Nr5",
"email": "test4@",
"firstname": "Test4",
"lastname": "Test4",
"groupname": "test"
}
}
]
}
This will correspond to a Lua table with a field named users
containing another table. Each entry into this inner table will have a single field named user
containing the table with the actual user data…
Here’s the code I use to read and parse your data:
local function http_request(url, method, headers, post_data, options)
local co = coroutine.running()
assert(co, "You must call this function from within a coroutine")
http.request(url, method, function(self, id, response)
coroutine.resume(co, response)
end, headers, post_data, options)
return coroutine.yield()
end
local function get_users()
local response = http_request("http://solidparts.se/test/getAllUsers.php", "POST", { ["Content-Type"] = "application/x-www-form-urlencoded" }, "groupname=test2")
if response.status >= 200 or response.status < 300 then
return json.decode(response.response)
else
return nil, "Error getting users"
end
end
local co = coroutine.create(function()
local users, err = get_users()
if users then
for _,v in pairs(users.users) do
pprint(v.user)
print(v.user and v.user.firstname or "")
end
else
print(err)
end
end)
local ok, err = coroutine.resume(co)
if not ok then
print(err)
end
And it successfully reads and parses the “groupname=test2” values.