我玩corona
和dropbox
,我从Dropbox获得如下结果
我将它们保存在变量t
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"
}'
我想提取所有“path
”,以便我得到
/mydays_b_12132012120312.txt
/mydays_b_12122012120314.txt
/mydays_b_12132012120319.txt
我尝试了corona json api
local decode = json.decode( t )
print( decode.path )
但我只得到第一个提取的路径......
有什么想法吗?
我也可以使用其他结果
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"
}'
但似乎并不容易。
如果你能给我一个解决方案,最终获得所有“路径”结果,那将是很棒的
非常感谢 克里斯
答案 0 :(得分:0)
我要采取的第一步是打印t:
print("t: "..tostring(t))
可能是t实际上是一个字符串而不是JSON对象,在这种情况下,您可以有条件地自己子串t并将其插入到数组中(为“不是真正的JSON”字符串创建自己的解码器)。如果您从上面的打印中获得预期的输出,请在此处注释,如果您愿意,我将提供条件子串方法
答案 1 :(得分:0)
与@Cory一样,第一个块中的字符串不包含有效的JSON,它由两个JSON对象组成: {...},{...} 。 Corona的JSON解析器显然通过在解析有效对象后忽略字符串中的所有内容来处理它。
一种解决方案是通过将 [] 添加到字符串 [{...},{...}] 来手动将其变为可解析的JSON数组,但这确实是一个黑客。 dropbox api的结果永远不会给你这个格式错误的对象。
第二个块包含有效的JSON,可以像这样提取路径:
local json = require("json")
local foo = json.decode(t)
local content = foo.contents
for i=1,#content do
print("PATH", content[i].path)
end