Newbie cant get JSON

北战南征 提交于 2019-12-11 16:07:26

问题


In LiveCode I have a stack connecting to a localhost MongoDB, the stack has a button with a mouseup handler and the function JSONToArray from MergJSON and two fields: "A" to receive the server answer "as is" and field B" to receive the decoded JSON.

This is the script of the button:

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

The contents of fld "A" after mouseup is:

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

The script fails with the following LiveCode error:

        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'

If I change the script to:

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

Field "A" gets this:

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");
    ...
    ...
    ...
}

I am shortening the actual field "A" content, it has a lot of text.

Can you guide me please? What I am doing wrong? Why I am not getting a JSON. I checked { "_id" : ObjectId("52e3f87c8da8b1efb07004c9"), "a" : 1 } using an online service finding that it is not a valid JSON.


回答1:


Try feeding only the string from { until } without the preceding lines.

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



回答2:


TO answer the question "What am I doing wrong?" you first need to understand what you are doing when you invoke .find() on a collection.

Now as the documentation in that link will tell you (right at the top of the page) you are not returning results from this call but in fact you are returning a cursor. Now what you may see when invoking this is the shell is a series of documents come out.

But the only reason this happens is that the shell (in interactive form) acts as a REPL and evaluates your statements in order to print the output. So the interactive shell is evaluating your cursor and calling a .next() method on that several times. This is only a convenience function while working in the shell.

In order to get the results programmatically as you intend to do, you need to do something with this cursor to actually see the results. Now you could create a loop where you iterate over the results calling .next() each time, but for your purposes the .toArray() method should suffice.

So in a slightly expanded way, for clear definition you would have a script form like this:

var cursor = db.test.find();

var results = cursor.toArray();

printjson( results );

Or as the general one liner using method chaining:

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

And that will emit the JSON Array of documents you need.

Alternately you can use the .findOne() method which will just return a single document which you can pass in to the printjson() function.



来源:https://stackoverflow.com/questions/22000015/newbie-cant-get-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!