代码:
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>我正在尝试获取'orderid‘的键值。
我尝试了几乎所有的方法,从这里-> 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对象并对'orderid‘进行模式匹配?
发布于 2014-11-14 16:58:51
您提供的数据不是有效的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"]来访问它的关联值。
https://stackoverflow.com/questions/26783187
复制相似问题