我正在制作一个 Node.js 扩展,我想返回一个 json 格式的对象,而不是一个 json 格式的字符串。
#include <node.h>
#include <node_object_wrap.h>
using namespace v8;
void ListDevices(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
std::string json = "[\"test\", \"test2\"]";
args.GetReturnValue().Set(String::NewFromUtf8(isolate, json.c_str()));
}
void InitAll(Handle<Object> exports) {
NODE_SET_METHOD(exports, "listDevices", ListDevices);
}
NODE_MODULE(addon, InitAll)
Run Code Online (Sandbox Code Playgroud)
怎么做到呢 ?
var addon = require("./ADDON");
var jsonvar = JSON.parse(addon.listDevices());
console.log(jsonvar);
Run Code Online (Sandbox Code Playgroud)
实际上,在这部分中,我想删除 JSON.parse
顺便说一句,是我,还是真的很难找到文档?我在google上试过,但是很多内容已经过时了,在v8.h中,有趣的功能没有记录。
谢谢 ;)