首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何模式匹配rapidjson对象?

如何模式匹配rapidjson对象?
EN

Stack Overflow用户
提问于 2014-11-06 23:29:46
回答 1查看 585关注 0票数 0

代码:

代码语言:javascript
复制
int main()
{
        char buff[BUFSIZ];
        FILE *fp = popen("/usr/bin/php getSome.php 155", "r");
        FileReadStream s(fp, buff, BUFSIZ);
        Document a;
        a.ParseStream(s);
        \\for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
            \\printf("%d ", itr->GetInt());
        printf(a[1]);

}

我的json数据看起来是这样的:

代码语言:javascript
复制
./phpMethod1.o
success:1
orderid:192877561
moreinfo:Your Buy order has been placed for<br><b>0.00100000 DRK @ 0.00517290 BTC</b> each.<br>Order ID: <b>192877561</b>

我正在尝试获取'orderid‘的键值。

我尝试了几乎所有的方法,从这里-> rapidjson user guide访问入站json数据,总是得到相同类型的转换错误。

代码语言:javascript
复制
# g++ -g phpBuyMethod1.cpp -o phpBuyMethod1.o -std=gnu++11
phpBuyMethod1.cpp: In function 'int main()':
phpBuyMethod1.cpp:27:13: error: cannot convert 'rapidjson::GenericValue<rapidjson::UTF8<> >' to 'const char*' for argument '1' to 'int printf(const char*, ...)'

或者如果我尝试一个for循环,比如:

代码语言:javascript
复制
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
    printf("%d ", itr->GetInt());

代码可以编译,但我在执行时得到以下错误(基本上是在同一件事情上卡住了):

代码语言:javascript
复制
phpBuyMethod1.o: rapidjson/include/rapidjson/document.h:1167: rapidjson::GenericValue<Encoding, Allocator>* rapidjson::GenericValue<Encoding, Allocator>::Begin() [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator>::ValueIterator = rapidjson::GenericValue<rapidjson::UTF8<> >*; rapidjson::GenericValue<Encoding, Allocator> = rapidjson::GenericValue<rapidjson::UTF8<> >]: Assertion `IsArray()' failed.

教训是,我就是不能得到模式匹配的数据。我如何正确地访问UTF-8 rapidjson对象并对'orderid‘进行模式匹配?

EN

回答 1

Stack Overflow用户

发布于 2014-11-14 16:58:51

您提供的数据不是有效的JSON。有效的JSON类似于:

代码语言:javascript
复制
{
"success":1,
"orderid":192877561,
"moreinfo":"Your Buy order has been placed for<br><b>0.00100000 DRK @ 0.00517290 BTC</b> each.<br>Order ID: <b>192877561</b>"
}

解析应该已经失败了。您可以通过a.HasParseError()查看解析是否成功。

此外,在解析上述有效的JSON之后,它的根是一个对象类型,您可以使用a["moreinfo"]来访问它的关联值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26783187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档