Json Not Working Correctly

I’m pulling JSON data using the network.request from my server. The JSON is being returned and I’ve validated it at least 100 times to make sure that the returned JSON is valid. In my code I’m doing this.

local json = require("json") local function fetchScoreListener(event) if(event.isError) then print( "Network error!") else local jsonData = event.response local jsonDecoded = json.decode(jsonData) print(jsonDecoded) -- \< -- THIS ALWAYS RETURNS NIL --Make sure JSON is not nil if(jsonDecoded ~= nil) then print("User Rank: " .. jsonDecoded["rank"]) print("Points to Next Rank: " .. jsonDecoded["points\_not\_next\_rank"]) --Loop though users highscores for key2 in pairs(jsonDecoded["users"]) do print(jsonDecoded["users"][key2]["username"]) print(jsonDecoded["users"][key2]["score"]) end end end end

From the code above the event.response is pulling back the json data just fine. However when I print the jsonDecoded variable it is always nil. I can’t seem to figure this out

This is how my JSON is setup

{ "users": [{ "username": "TEST USER1", "score": "135973816" }, { "username": "TEST USER12", "score": "4143165" }], "rank": "16144", "points\_not\_next\_rank": "2493" }

Should I file a bug report or am I missing something in my code?

I tested this:

[lua]

 str = [[{
    “users”: [
        {
            “username”: “TEST USER1”,
            “score”: “135973816”
        },
        {
            “username”: “TEST USER12”,
            “score”: “4143165”
        }
    ],
    “rank”: “16144”,
    “points_not_next_rank”: “2493”
}]]

local json = require(“json”)
local utility = require(“utility”)

local t = json.decode(str)
utility.print_r(t)

[/lua]

print_r is a table dumper from the community code and it printed this:

2013-04-16 20:51:09.406 Corona Simulator[792:707] table: 0x1014fa820 {
2013-04-16 20:51:09.407 Corona Simulator[792:707]   [points_not_next_rank] => “2493”
2013-04-16 20:51:09.407 Corona Simulator[792:707]   [users] => table: 0x1014fa820 {
2013-04-16 20:51:09.408 Corona Simulator[792:707]                [1] => table: 0x1014fa860 {
2013-04-16 20:51:09.408 Corona Simulator[792:707]                         [username] => “TEST USER1”
2013-04-16 20:51:09.408 Corona Simulator[792:707]                         [score] => “135973816”
2013-04-16 20:51:09.409 Corona Simulator[792:707]                       }
2013-04-16 20:51:09.409 Corona Simulator[792:707]                [2] => table: 0x1014fa860 {
2013-04-16 20:51:09.410 Corona Simulator[792:707]                         [username] => “TEST USER12”
2013-04-16 20:51:09.410 Corona Simulator[792:707]                         [score] => “4143165”
2013-04-16 20:51:09.410 Corona Simulator[792:707]                       }
2013-04-16 20:51:09.411 Corona Simulator[792:707]              }
2013-04-16 20:51:09.411 Corona Simulator[792:707]   [rank] => “16144”
2013-04-16 20:51:09.411 Corona Simulator[792:707] }
 

It appears to be working for me.

What do you get when you print out event.response?

Yeah when I use the JSON directly in my code it works fine. When I try pulling the same JSON from a url is when I get the problem. If I print out event.response it prints out the JSON string. When I try to parse that though I always get nil.
 

I’m thinking it might have to do with the character encoding that your server is using to transmit the text?  Maybe it’s using an encoding that works fine for printing, but that for some reason the json library can’t handle.

You might try printing out the headers of the response (event.responseHeaders) and seeing what character encoding is being used.

  • Andrew

Okay after a lot of hunting I figured it out…even though the JSON was valid Corona didn’t like the fact that some of the user entered names contained a \n line break in the record. I corrected the line breaks and everything started working.

I tested this:

[lua]

 str = [[{
    “users”: [
        {
            “username”: “TEST USER1”,
            “score”: “135973816”
        },
        {
            “username”: “TEST USER12”,
            “score”: “4143165”
        }
    ],
    “rank”: “16144”,
    “points_not_next_rank”: “2493”
}]]

local json = require(“json”)
local utility = require(“utility”)

local t = json.decode(str)
utility.print_r(t)

[/lua]

print_r is a table dumper from the community code and it printed this:

2013-04-16 20:51:09.406 Corona Simulator[792:707] table: 0x1014fa820 {
2013-04-16 20:51:09.407 Corona Simulator[792:707]   [points_not_next_rank] => “2493”
2013-04-16 20:51:09.407 Corona Simulator[792:707]   [users] => table: 0x1014fa820 {
2013-04-16 20:51:09.408 Corona Simulator[792:707]                [1] => table: 0x1014fa860 {
2013-04-16 20:51:09.408 Corona Simulator[792:707]                         [username] => “TEST USER1”
2013-04-16 20:51:09.408 Corona Simulator[792:707]                         [score] => “135973816”
2013-04-16 20:51:09.409 Corona Simulator[792:707]                       }
2013-04-16 20:51:09.409 Corona Simulator[792:707]                [2] => table: 0x1014fa860 {
2013-04-16 20:51:09.410 Corona Simulator[792:707]                         [username] => “TEST USER12”
2013-04-16 20:51:09.410 Corona Simulator[792:707]                         [score] => “4143165”
2013-04-16 20:51:09.410 Corona Simulator[792:707]                       }
2013-04-16 20:51:09.411 Corona Simulator[792:707]              }
2013-04-16 20:51:09.411 Corona Simulator[792:707]   [rank] => “16144”
2013-04-16 20:51:09.411 Corona Simulator[792:707] }
 

It appears to be working for me.

What do you get when you print out event.response?

Yeah when I use the JSON directly in my code it works fine. When I try pulling the same JSON from a url is when I get the problem. If I print out event.response it prints out the JSON string. When I try to parse that though I always get nil.
 

I’m thinking it might have to do with the character encoding that your server is using to transmit the text?  Maybe it’s using an encoding that works fine for printing, but that for some reason the json library can’t handle.

You might try printing out the headers of the response (event.responseHeaders) and seeing what character encoding is being used.

  • Andrew

Okay after a lot of hunting I figured it out…even though the JSON was valid Corona didn’t like the fact that some of the user entered names contained a \n line break in the record. I corrected the line breaks and everything started working.