【问题标题】:Corona / JSON Result电晕 / JSON 结果
【发布时间】:2014-01-24 20:15:05
【问题描述】:

我玩过coronadropbox,我从 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"
}'

但似乎并不容易。

如果你能给我一个解决方案,最终得到所有“路径”结果,那就太好了

非常感谢 克里斯

【问题讨论】:

  • 第一个 t 不是有效的 JSON 字符串。它似乎是一个对象列表,但其中没有数组表示法。也许 Corona 只返回第一个,因为它没有将输入作为数组处理。

标签: json coronasdk dropbox-api


【解决方案1】:

我会采取的第一步是这样打印 t:

print("t: "..tostring(t))

可能 t 实际上是一个字符串而不是 JSON 对象,在这种情况下,您可以自己有条件地对 t 进行子串化并将其插入到数组中(为“不是真正的 JSON”字符串制作您自己的解码器)。如果您从上述打印中获得预期的输出,请在此处发表评论,如果您愿意,我将提供一个条件子字符串方法

【讨论】:

    【解决方案2】:

    就像@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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2013-04-12
      • 2013-07-05
      • 1970-01-01
      相关资源
      最近更新 更多