JSON Parse failing

I’m returning the following string from a Web API service and JSON.Decode is failing. Can any tell me why?

{“UserId":5,“UserName”:“strongMan”,“Email”:"user@email.com”,“DistanceFromMe”:0.0,“FriendCount”:1,“AvailableBombs”:12,“UsedBombs”:134,“Rank”:“Beginner”}

I’ve reduced the object to one property, hence scaling the return value back to {“UserName”:“strongMan”} and it still fails.  

<lua>


– API RESPONSE HANDLER


local function handleResponse( event )

print (event.response)

  if not event.isError then

      local userInfo, pos, msg = json.decode( event.response )

      if not decoded then

           print( "Decode failed at “…tostring(pos)…”: "…tostring(msg) )

           return

      else

           print( userInfo.UserName )

          --loadHomePage( )

      end

   else

      native.showAlert( “Login Failed”, “Network Error”, {“OK”} )

   end

return

end

</lua>

Printing event.response produces the JSON code pasted above.   The result is… Decode failed at 162: nil

Seems to work fine for me, though I have to directly escape the data to test. Is it perhaps adding a newline or something on the return?

My test in Corona:

[lua]

local json = require(“json”)

local j = “{“UserId":5,“UserName”:“strongMan”,“Email”:"user@email.com”,“DistanceFromMe”:0.0,“FriendCount”:1,“AvailableBombs”:12,“UsedBombs”:134,“Rank”:“Beginner”}”

local res = json.decode(j)

for k, v in pairs(res) do

print(k, v)

end

[/lua]

Result:

[lua]UserName strongMan
Rank Beginner
FriendCount 1
DistanceFromMe 0
UsedBombs 134
AvailableBombs 12
UserId 5
Email user@email.com[/lua]

-dev

The “decoded” variable doesn’t seem to exist in your code.

You’ll probably want something like:

if evt.isError then native.showAlert( "Login Failed", "Network Error", {"OK"} ) else local userInfo, pos, msg = json.decode( event.response ) if not userInfo then print( "Decode failed at "..tostring(pos)..": "..tostring(msg) ) return else print( userInfo.UserName ) --loadHomePage( ) end end

-dev

This thread had some good advice.  Work backwards using the JSON encode decode to see what the encoded table should look like:

https://forums.coronalabs.com/topic/69807-jsondecode-a-table-with-arrays-as-values/#entry363390

Then fix your server code to produce encodings that follow those rules.

Also, as Chris points out if you’re missing fields, be sure to add defaults or to account for their possible absence in your listener.

That did it. Thanks!

Develephant nailed it.  Thanks everyone

Seems to work fine for me, though I have to directly escape the data to test. Is it perhaps adding a newline or something on the return?

My test in Corona:

[lua]

local json = require(“json”)

local j = “{“UserId":5,“UserName”:“strongMan”,“Email”:"user@email.com”,“DistanceFromMe”:0.0,“FriendCount”:1,“AvailableBombs”:12,“UsedBombs”:134,“Rank”:“Beginner”}”

local res = json.decode(j)

for k, v in pairs(res) do

print(k, v)

end

[/lua]

Result:

[lua]UserName strongMan
Rank Beginner
FriendCount 1
DistanceFromMe 0
UsedBombs 134
AvailableBombs 12
UserId 5
Email user@email.com[/lua]

-dev

The “decoded” variable doesn’t seem to exist in your code.

You’ll probably want something like:

if evt.isError then native.showAlert( "Login Failed", "Network Error", {"OK"} ) else local userInfo, pos, msg = json.decode( event.response ) if not userInfo then print( "Decode failed at "..tostring(pos)..": "..tostring(msg) ) return else print( userInfo.UserName ) --loadHomePage( ) end end

-dev

This thread had some good advice.  Work backwards using the JSON encode decode to see what the encoded table should look like:

https://forums.coronalabs.com/topic/69807-jsondecode-a-table-with-arrays-as-values/#entry363390

Then fix your server code to produce encodings that follow those rules.

Also, as Chris points out if you’re missing fields, be sure to add defaults or to account for their possible absence in your listener.

That did it. Thanks!

Develephant nailed it.  Thanks everyone