c++调用v8引擎解析json
2012-09-06 17:27
525 查看
v8是一个强大的js虚拟机,json是js的内置数据格式。json常用于程序配置和网络信息传输。
例子有些杀鸡用牛刀的感觉,大家就当一个体验吧。
在win7上编译v8的过程可以参考这个,或者v8的官方doc。
http://blog.csdn.net/wuzh1230/article/details/7919932
主要步骤:加载json配置(可以从文件读取,utf-8格式),编译,执行,编译配置项表达式,执行,提取配置项的值。
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(
// 1, 正则表达式测试例子
//"var x='xyziamheres2///sdfsdf123'; x.match(/\\d{3,3}/);"
// 2, json配置测试例子
"var config={'Version':'1.0', 'Title':'HelloWorld', 'ThreadNum':'28'};"
);
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
script->Run();
// 读取Version配置
Handle<v8::String> src1 = v8::String::New("config.Version;");
Handle<Script> script1 = v8::Script::Compile(src1);
Handle<Value> val1 = script1->Run();
String::AsciiValue ascii1(val1);
printf("%s\n", *ascii1);
// 读取Title配置
Handle<v8::String> src2 = v8::String::New("config.Title;");
Handle<Script> script2 = v8::Script::Compile(src2);
Handle<Value> val2 = script2->Run();
String::AsciiValue ascii2(val2);
printf("%s\n", *ascii2);
// Dispose the persistent context.
context.Dispose();
return 0;
}
结果截图:
例子有些杀鸡用牛刀的感觉,大家就当一个体验吧。
在win7上编译v8的过程可以参考这个,或者v8的官方doc。
http://blog.csdn.net/wuzh1230/article/details/7919932
主要步骤:加载json配置(可以从文件读取,utf-8格式),编译,执行,编译配置项表达式,执行,提取配置项的值。
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(
// 1, 正则表达式测试例子
//"var x='xyziamheres2///sdfsdf123'; x.match(/\\d{3,3}/);"
// 2, json配置测试例子
"var config={'Version':'1.0', 'Title':'HelloWorld', 'ThreadNum':'28'};"
);
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
script->Run();
// 读取Version配置
Handle<v8::String> src1 = v8::String::New("config.Version;");
Handle<Script> script1 = v8::Script::Compile(src1);
Handle<Value> val1 = script1->Run();
String::AsciiValue ascii1(val1);
printf("%s\n", *ascii1);
// 读取Title配置
Handle<v8::String> src2 = v8::String::New("config.Title;");
Handle<Script> script2 = v8::Script::Compile(src2);
Handle<Value> val2 = script2->Run();
String::AsciiValue ascii2(val2);
printf("%s\n", *ascii2);
// Dispose the persistent context.
context.Dispose();
return 0;
}
结果截图:
相关文章推荐
- C++的Json解析库:jsoncpp
- 解析XML文件后生成JSON格式,供WEB前端调用
- C++的Json解析库:jsoncpp和boost .
- C++ 解析Json——jsoncpp
- [V8] 在C++中应用Google Chrome脚本引擎——V8
- 通过C++和shell脚本去解析URL && c++调用shell脚本后将脚本的内容返回给程序
- 在c++中使用json----保存到文件、从文件中解析
- c++ 解析json
- LotusScript 调用WSDL 并解析Json字符串
- [推荐] C++解析json包:jsoncpp ——入门攻略
- C++解析JSON
- C++解析JSON的好文章
- C/C++下的JSON解析/生成库cJSON
- JVM高级特性与实践(十一):方法调用 与 字节码解释执行引擎(实例解析)
- iOS中解析json格式数据的各种方法调用
- C++实现Json解析详细教程
- Unity3D 游戏引擎之将场景导出XML或JSON或二进制并且解析还原场景
- .net 调用API并解析Json数据方法
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx 关于JSON对象解析List<XXX>在调用时出错问题