本文介绍了如何在node.js中返回JSON内容(c ++ addon)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿!
我正在制作Node.js扩展名,我想返回一个json格式的对象,而不是一个json格式的字符串。
Hey !
I'm making a Node.js extension, and I would like to return a json format object, instead of a json-formatted string.
#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)
怎么办?
How can it be done ?
var addon = require("./ADDON");
var jsonvar = JSON.parse(addon.listDevices());
console.log(jsonvar);
实际上,在这部分中,我想删除JSON.parse
顺便问一下,是我,还是很难找到文件?我试过google,但是很多内容已经过时了,在v8.h中,有趣的功能没有记录。
谢谢;)
Actually, in this part, I would like to remove the JSON.parse
By the way, is it me, or it is really difficult to find documentation ? I tried on google, but a lot of content was out of date, and in v8.h, interesting functions weren't documented.
Thank you ;)
推荐答案
这篇关于如何在node.js中返回JSON内容(c ++ addon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!