How to parse JSON

Hi,

I'm having troubles parsing some JSON stuff. I'm talking to an website API over Oauth and getting some JSON as return

Here's some of my main.lua code:

1
2
3
4
5
6
local request_response = oAuth.makeRequest("http://api.mysite.com/users/list.json", params, consumer_key, access_token, consumer_secret, access_token_secret, "GET" )    
        
local myCode_encoded = json.encode(request_response)
local myCode_decoded = json.decode(myCode_encoded)
 
print (myCode_decoded) 

Try something like this:

print(myCode_decoded["id"])
print(myCode_decoded["email"])

Jay

Hi Jay,

1
print(myCode_decoded["id"])

This tutorial, just posted, might be useful for you:

http://blog.anscamobile.com/2011/08/tutorial-exploring-json-usage-in-corona/

Hi Jonathan,

I noticed that my JSON output starts and ends with "[ ]".
Does this mean it's not decoded successfully ?

Any idea's ?

I tried some things from your blogpost, but without success :(

Lieven

ok, I think i understand why I'm getting a nil... my JSON that i'm getting from the server is interpreted as a string, not as a JSON object.

How can i convert that JSON string to real JSON (in a table) ?

Lieven

print(myCode_encoded[1].id) perhaps?

Looks like a array containing objects being returned from the server.

I found a solution for my problem.

I was first encoding the json that was returned and then decoding it.
If I only decode it (without encoding) then it works fine.

Thanks all for the help.

Lieven

views:1710 update:2011/9/28 8:56:32
corona forums © 2003-2011