【问题标题】:Newbie cant get JSON新手无法获取 JSON
【发布时间】:2014-02-24 22:02:09
【问题描述】:

在 LiveCode 中,我有一个连接到本地 MongoDB 的堆栈,该堆栈有一个带有 mouseup 处理程序的按钮和来自 MergJSON 的函数 JSONToArray 和两个字段:“A”接收服务器回答“原样”和字段 B“接收解码后的 JSON。

这是按钮的脚本:

on mouseup
  set the hideConsoleWindows to true
  put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & \ 
    "printjson(db.test.findOne())" & quote) into pJSON
  put pJSON into fld "a"
  put JSONToArray(pJSON) into tArray
  put tArray["a"] into fld "B"
end mouseup

mouseup后fld“A”的内容为:

MongoDB shell version: 2.2.7
connecting to: test
{ "_id" : ObjectId("52e3f87c8da8b1efb07004c9"), "a" : 1 }

脚本失败并出现以下 LiveCode 错误:

        executing at 8:58:32 PM
Type    could not decode JSON: invalid token near 'MongoDB'
Object  Completo
Line    repeat for each line tKey in mergJSONDecode(pJSON,"tArray")
Hint    could not decode JSON: invalid token near 'MongoDB'

如果我将脚本更改为:

on mouseup
  put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & "printjson(db.test.find())" & quote) into pJSON
  put pJSON into fld "A"
end mouseup

字段“A”得到这个:

MongoDB shell version: 2.2.7
connecting to: test
{
    _"_mongo" : connection to 127.0.0.1,
    _"_db" : test,
    _"_collection" : test.test
    _"_ns" : "test.test",
    _"_query" : {
    __
_},
    _"_fields" : null,
    _"_limit" : 0,
    _"_skip" : 0,
    _"_batchSize" : 0,
    _"_options" : 0,
    _"_cursor" : null,
    _"_numReturned" : 0,
    _"_special" : false,
    _"help" : function () {
    print("find() modifiers");
    ...
    ...
    ...
}

我正在缩短实际字段“A”的内容,它有很多文字。

你能指导我吗?我做错了什么?为什么我没有得到 JSON。我使用在线服务检查了{ "_id" : ObjectId("52e3f87c8da8b1efb07004c9"), "a" : 1 },发现它不是有效的 JSON。

【问题讨论】:

    标签: mongodb livecode


    【解决方案1】:

    尝试只输入从 { 到 } 的字符串,而不输入前面的行。

    on mouseUp
      set the hideConsoleWindows to true
      put shell("C:\mongodb\bin\mongo.exe --eval" && \
        quote & "printjson(db.test.findOne())" & quote) into pJSON
      put pJSON into fld "a"
      put JSONToArray(line 3 to -1 of pJSON) into tArray // <-- this line changed
      put tArray["a"] into fld "B"
    end mouseUp
    

    【讨论】:

    • 是的 .findOne() 将只返回一条记录作为可以解释为 JSON 的内容,但我不能说你理解这个问题。关键是.find() 不会返回可以序列化为 JSON 的内容。在关注 livecode 组件时,您似乎错过了重点。 (另外请记住,这个问题在几个小时前看起来有很大不同,但这一点仍然有效)
    • 我们或许应该等待哈维尔对我们的答案发表评论。
    • 尼尔,马克,非常感谢。我将尝试将脚本更改为:将 JSONToArray(pJSON 的第 3 行到 -1 行) 放入 tArray 中,正如 Mark 建议的那样,Neil,我将重新阅读有关 .find 的 info.mation 以确保我总是得到一个JSON。请等待我的下一篇文章。
    • 哈维尔,当您有新结果时,您可以更新您的问题以包含这些结果。
    • @Mark ,Neil Lunn 正确地指出我得到的是一个游标而不是 JSON,他对 shell 关于 find() 工作方式的解释是准确的,我尝试了 db.test.find( ) 并且发出时的结果是相同的: var c = db.test.find() ;而 ( c.hasNext() ) printjson( c.next())。现在的问题是如何在 LC 中编码,我试过: put shell("C:\mongodb\bin\mongo.exe --eval var c = db.test.find() ; while ( c.hasNext() ) printjson( c.next() )") 放入 pJSON 将 pJSON 放入 fld "A" 但字段 "A" 得到错误 MongoDB... 连接到:c SyntaxError: missing variable name (shell eval):1
    【解决方案2】:

    回答“我做错了什么?”的问题您首先需要了解在集合上调用 .find() 时您在做什么。

    现在,该链接中的文档会告诉您(就在页面顶部),您没有从此调用返回 results,但实际上您正在返回cursor。现在调用它时您可能看到的是,shell 会输出一系列文件。

    但发生这种情况的唯一原因是外壳(以交互形式)充当REPL评估您的语句以打印输出。所以 interactive shell 正在评估您的cursor 并多次调用.next() 方法。这只是在 shell 中工作时的便利功能。

    为了按照您的意图以编程方式获得结果,您需要使用此cursor做一些事情才能真正看到结果。现在您可以创建一个循环,在其中迭代每次调用.next() 的结果,但对于您的目的,.toArray() 方法就足够了。

    所以在稍微扩展的方式中,为了清晰的定义,你会有一个像这样的脚本形式:

    var cursor = db.test.find();
    
    var results = cursor.toArray();
    
    printjson( results );
    

    或者作为一般的one liner使用方法链:

    printjson( db.test.find().toArray() )
    

    这将发出您需要的 JSON Array 文档。

    您也可以使用 .findOne() 方法,它只返回一个 document,您可以将其传递给 printjson() 函数。

    【讨论】:

    • 我不明白这是如何解决问题的。 OP 不需要数组类型的结果。在 LiveCode 中,一切都是字符串。 Json 结果必须是格式正确的字符串,可以提供给 MergJson 外部。
    • @Mark 答案有明显区别。 OP 没有返回任何有效的结果,因为结果是光标 并且我将结果(来自 printjson() 方法)作为字符串提供给他们。
    • 嗨,我将 mouseuphandler 中的行更改为:将 JSONToArray(pJSON 的第 3 行到 -1 行)放入 tArray //
    • 所以考虑到我得到的结果是 s 光标,问题是如何使用 LC 命令对其进行迭代以构建要传递给函数的数组。如果我错了,请纠正我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多