Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to take values on complex JSON tables?
Started by vb66r55 May 23 2019 07:54 AM

- - - - -
14 replies to this topic

Best Answer nick_sherman , 23 May 2019 - 11:04 AM

Have you tried to iterate over friendsList using in pairs? 

 

https://docs.coronalabs.com/api/library/global/pairs.html

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

Greetings,

 

I am dealing with social issues. I know the value of the decode JSON table, but now I have a problem.

 

local UserDataJson = json.encode( UserData, { indent = true } )

 

{
  "scriptData": {
    "friendsList": {
      "5ce69a4ecf0xxxxxx237d5d4": {
        "displayName": "Mark"
      },
      "5cexxxxxxdddc5237d664": {
        "displayName": "Peter"
      }
    }
  }
}
 
In the middle is the User ID, I used xxx to hide.
 
I used UserDataJson.friendsList to get a table reply.
 
I tried to use UserDataJson.friendsList[1] or UserDataJson.friendsList.displayName to get nil.
 
Is there a way to get the middle UID and displayName individually?
 
Thank you.
 


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

  Best Answer

Have you tried to iterate over friendsList using in pairs? 

 

https://docs.coronalabs.com/api/library/global/pairs.html


  • vb66r55 likes this

[TOPIC: post.html]
#3

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

Dear Nick,

 

I tried to use this method.

 

But it keeps appearing.

 

bad argument #1 to 'pairs' (table expected, got string)



[TOPIC: post.html]
#4

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,105 posts
  • Corona SDK

Your JSON doesn't make much sense to me.  Try

{
  {"id":"5ce69a4ecf0xxxxxx237d5d4", "name": "Mark"},
  {"id":"5cexxxxxxdddc5237d664": "name": "Peter"},
}


[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

Sounds like you're trying to iterate over the JSON, not the original Lua tables. You need to decode the JSON into a Lua table before you can dig into it.



[TOPIC: post.html]
#6

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,105 posts
  • Corona SDK

Yes, you encode to save and decode to use.  Still I advise using a more normalised data structure.



[TOPIC: post.html]
#7

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

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.


[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

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



[TOPIC: post.html]
#9

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

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)

[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

You are using ENcode not DEcode.



[TOPIC: post.html]
#11

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

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

[TOPIC: post.html]
#12

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

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?



[TOPIC: post.html]
#13

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

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. :(

 

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


[TOPIC: post.html]
#14

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

What happens if you try to decode the raw response?

[TOPIC: post.html]
#15

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Enthusiast

  • 44 posts
  • Corona SDK

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. :D



[topic_controls]
[/topic_controls]