我如何模式匹配rapidjson对象?

时间:2014-11-06 15:29:46

标签: c++ regex json filestream

代码:

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数据,如下所示:

./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>

我正在尝试获得&#39; orderid&#39;。

的关键值

我从这里尝试了几乎所有方法 - &gt; rapidjson user guide访问入站json数据并始终获得相同类型的转换错误。

# 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循环,例如:

for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
    printf("%d ", itr->GetInt());

代码将编译,但我在执行时遇到以下错误(基本上窒息同样的事情):

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对象并为&#39; orderid&#39;进行模式匹配?

1 个答案:

答案 0 :(得分:0)

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

{
"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"]来访问它的相关值。

相关问题