json help

Hi,

i play with dropbox and I get results like

    

    t = ‘{“revision”: 7, “rev”: “707b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Fri, 24 Jan 2014 03:07:54 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/mydays_b_12132012120312.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}, {“revision”: 9, “rev”: “907b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Fri, 24 Jan 2014 03:08:03 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/mydays_b_12122012120314.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}, {“revision”: 12, “rev”: “c07b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Fri, 24 Jan 2014 18:51:43 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/mydays_b_12132012120319.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}’

i would like to extract all ‘path’ so as result i have

/mydays_b_12132012120312.txt

/mydays_b_12122012120314.txt

/mydays_b_12132012120319.txt

i tried like

    local decode = json.decode( t )

    print( decode.path )  but only get the first Path extracted… 

any ideas

also i could work with another result

t = ‘{“hash”: “c6e5643fe351d4a59b4b3cb61bdfb870”, “thumb_exists”: false, “bytes”: 0, “path”: “/”, “is_dir”: true, “size”: “0 bytes”, “root”: “app_folder”, “contents”: [{“revision”: 1, “rev”: “107b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Mon, 14 May 2012 18:56:57 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/backup.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}, {“revision”: 9, “rev”: “907b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Fri, 24 Jan 2014 03:08:03 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/mydays_b_12122012120314.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}, {“revision”: 7, “rev”: “707b638c6”, “thumb_exists”: false, “bytes”: 36, “modified”: “Fri, 24 Jan 2014 03:07:54 +0000”, “client_mtime”: “Mon, 14 May 2012 18:56:57 +0000”, “path”: “/mydays_b_12132012120312.txt”, “is_dir”: false, “icon”: “page_white_text”, “root”: “dropbox”, “mime_type”: “text/plain”, “size”: “36 bytes”}], “icon”: “folder”}’

but does not seem easier.

would be great u could give me a solution for both… to finally get all ‘path’ results

thanks a lot

chris

json.decode() doesn’t work if the json text provided to it has some formatting issues. I often use an online tool to validate JSON. The following works either with pasted json text or even URL where json is pulled from : http://jsonlint.com/

For example I copy/pasted your json text above and the validator says there are issues with it. Suggest working through this and getting the json validator giving you an a-ok before checking out the rest of your code. Hope this helps.

yes, seems the first one is not valid…

  1. generated by dropbox /metadata, its exactly what i get from dropbox

  2. generated by dropbox /search, and that seems a valid json

any idea how to pull the path’s out there

Your string is an array but it’s not encoded as an array, so json.decode() is only parsing the first {} of data.  Try something like this:

local entries = json.decode("[" … t …"]")

for i = 1, #entries do

    print(entries[i].path

end

Rob

I’ve got Dropbox workiing with my app.

The meta I get looks like the json you have as #2 (search), not #1.  hmmmm

I get the path from the meta like this in my listener.

JsonResponse = json.decode(event.response)

for i = 1, #JsonResponse.contents do
                print(“JsonResponse.contents[”…i…"].path == ", JsonResponse.contents[i].path)
                

end

Sorry, the lua tags format my code into an unreadable long string…argh

Hope this helps,

Nail

thanks guys for ur help. got it in the meantime!

@xnailbender

great to hear u have it working

could u share ur code for uploading a file to dropbox.

somehow my code does not work, always get

filesize: 16

2014-01-25 22:08:30.623 Corona Simulator[63834:507] rawPostRequest posting

2014-01-25 22:08:30.633 Corona Simulator[63834:507] ERROR: Error during request, code: 2, details: The operation couldn’t be completed. No such file or directory

2014-01-25 22:08:30.633 Corona Simulator[63834:507] putFileListener

2014-01-25 22:08:30.634 Corona Simulator[63834:507] Network error!    -1    nil

would be great

thanks

chris

Chris,

I’m not familiar with the error codes you are getting, but it looks to me like the file or file name you are declaring is not working.

I got everything working by first hardcoding all the params to simplify troubleshooting and suggest you do the same.

I’d start by placing a simple text file named something like Test1.txt into your system.DocumentsDirectory and hard code the filename into the params. and try to upload it.  Since the Dropbox core API uses URL’s, your file name cannot have spaces or special characters in it I believe.

Rather than me posting my code, which is specific to my app and my be tough to follow, it would be better for you to post your code and I’ll look through it to see if I can find what’s breaking.

I’m assuming you are using the Dropbox module created by Torkel from the code exchange.

If you are, post the “function M.putFile( path, listener, params )” also so I can verify it’s what I have working.

Nail

thx… got it now :) 

was a beginner mistake…

just assigned the wrong filename :slight_smile:

Glad you got is sorted out.

Dropbox integration has so many uses, GL.

Nail

json.decode() doesn’t work if the json text provided to it has some formatting issues. I often use an online tool to validate JSON. The following works either with pasted json text or even URL where json is pulled from : http://jsonlint.com/

For example I copy/pasted your json text above and the validator says there are issues with it. Suggest working through this and getting the json validator giving you an a-ok before checking out the rest of your code. Hope this helps.

yes, seems the first one is not valid…

  1. generated by dropbox /metadata, its exactly what i get from dropbox

  2. generated by dropbox /search, and that seems a valid json

any idea how to pull the path’s out there

Your string is an array but it’s not encoded as an array, so json.decode() is only parsing the first {} of data.  Try something like this:

local entries = json.decode("[" … t …"]")

for i = 1, #entries do

    print(entries[i].path

end

Rob

I’ve got Dropbox workiing with my app.

The meta I get looks like the json you have as #2 (search), not #1.  hmmmm

I get the path from the meta like this in my listener.

JsonResponse = json.decode(event.response)

for i = 1, #JsonResponse.contents do
                print(“JsonResponse.contents[”…i…"].path == ", JsonResponse.contents[i].path)
                

end

Sorry, the lua tags format my code into an unreadable long string…argh

Hope this helps,

Nail

thanks guys for ur help. got it in the meantime!

@xnailbender

great to hear u have it working

could u share ur code for uploading a file to dropbox.

somehow my code does not work, always get

filesize: 16

2014-01-25 22:08:30.623 Corona Simulator[63834:507] rawPostRequest posting

2014-01-25 22:08:30.633 Corona Simulator[63834:507] ERROR: Error during request, code: 2, details: The operation couldn’t be completed. No such file or directory

2014-01-25 22:08:30.633 Corona Simulator[63834:507] putFileListener

2014-01-25 22:08:30.634 Corona Simulator[63834:507] Network error!    -1    nil

would be great

thanks

chris

Chris,

I’m not familiar with the error codes you are getting, but it looks to me like the file or file name you are declaring is not working.

I got everything working by first hardcoding all the params to simplify troubleshooting and suggest you do the same.

I’d start by placing a simple text file named something like Test1.txt into your system.DocumentsDirectory and hard code the filename into the params. and try to upload it.  Since the Dropbox core API uses URL’s, your file name cannot have spaces or special characters in it I believe.

Rather than me posting my code, which is specific to my app and my be tough to follow, it would be better for you to post your code and I’ll look through it to see if I can find what’s breaking.

I’m assuming you are using the Dropbox module created by Torkel from the code exchange.

If you are, post the “function M.putFile( path, listener, params )” also so I can verify it’s what I have working.

Nail

thx… got it now :) 

was a beginner mistake…

just assigned the wrong filename :slight_smile:

Glad you got is sorted out.

Dropbox integration has so many uses, GL.

Nail