How to take values on complex JSON tables?

I have decode it beforehand.

This table is from GameSparks CloudCode to Corona.

After decode, the result of print↓

{

 “friendsList”:{

  “5ce69a4ecf0xxxxxx237d5d4”:{

      “displayName”:“Mark”

     },

    “5cexxxxxxdddc5237d664”:{

        “displayName”:“Peter”

     }

    }

  }

But the system does not seem to recognize the value of friendsList.

That’s not decoded JSON, that’s a JSON string. Can you show the code where you are decoding?

getLocationRequest:send(function(response)
if response:hasErrors() then
–If errors then print errors
else
local FriendData = response:getScriptData()
local FriendJson = json.encode( FriendData, { indent = true } )
native.showAlert(“FriendData”, json.encode( FriendData, { indent = true } ), {“Ok”})
end
end)

You are using EN code not DE code.

Oh, indeed, because of the use of decode will produce nil.

Well, if the JSON string won’t decode that just means your JSON is malformed. You can’t encode a string and expect to be able to access data within it - it’s not a data structure that Corona understands.

What does response:getScriptData() do?

The result of response is the one posted at the top, and the same output result in Gamespark’s Test tool.

I just used decode but the result is the same as nil. :frowning:

I think I have to go to the hospital to undergo the suture after surgery.

Thank you all.

What happens if you try to decode the raw response?

I found the problem. !!!

Just in the hospital, I kept thinking about the method you provided.

   local FriendData = response:getScriptData()

   local FriendJson = json.encode( FriendData , { indent = true }

   local decoded, pos, msg = json.decode( FriendJson )

    if not decoded then

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

    else

           print( decoded.friendsList )  – Get a table value

           for k,v in pairs( decoded.friendsList ) do

                 print( "UID: “…k…” | " )

                 for a,b in pairs( v ) do

                     print( "displayName: “…a…” | "…b )

                 end

           end

  end

If I only use in pairs once, the latter will get error.

Dear Nick, thank you for your guidance. :smiley: