精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
在构造json输出时,遇到复杂的关系,这时候遇到了LogicError非法操作。报错的函数是:
JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg) {
throw LogicError(msg);
}
在网上搜索英文页面,找到了本文。如果是生成json时,遇到LogicError非法操作,请点击“jsoncpp的LogicError非法操作”。而本文是关注于解析时遇到LogicError非法操作。
I have a Json string as below and I need to parse them with jsoncpp library of C++; however, even if I try to parse status_code which is the simplest key, I get logicerror. My code to parse json is like this:我有一个Json字符串如下所示,我需要使用C ++的jsoncpp库解析它们; 但是,即使我尝试解析最简单的key_code,我也会遇到逻辑错误。我解析json的代码是这样的:
Json::Value root = js; const Json::Value status_code = root["status_code"];
where js is a std::string. I print the root and the results seems normal, but the the second line of code always gives error. Also, I tried Json::Value::get method and nothing changed.其中js是std :: string。我打印根,结果似乎正常,但第二行代码总是出错。此外,我尝试了Json :: Value :: get方法,没有任何改变。
{
"status_code": "OK",
"status_msg": "All images in request have completed successfully. ",
"meta": {
"tag": {
"timestamp": 1457008532.462851,
"model": "general-v1.3",
"config": "34fb1111b4d5f67cf1b8665ebc603704"
}
},
"results": [
{
"docid": 161641131401527616156105737996754786562,
"status_code": "OK",
"status_msg": "OK",
"local_id": "",
"result": {
"tag": {
"concept_ids": [
"ai_786Zr311",
"ai_pPxqdnP5",
"ai_Pf2b7clG",
"ai_c9n7SB25",
"ai_WTrlNkqM",
"ai_ggQlMG6W",
"ai_62K34TR4",
"ai_hf5ZBGcK",
"ai_9c0Hmcx0",
"ai_6lhccv44",
"ai_pCnxWJZh",
"ai_bmls4LpL",
"ai_TkWkj1sX",
"ai_rsX6XWc2",
"ai_x3vjxJsW",
"ai_4Qjv5PTH",
"ai_lrTHSPdB",
"ai_bBXFkGB1",
"ai_xxnGcd42",
"ai_PpTcwbdQ"
],
"classes": [
"no person",
"room",
"indoors",
"furniture",
"vehicle",
"industry",
"technology",
"production",
"exhibition",
"business",
"contemporary",
"group",
"container",
"building",
"home",
"modern",
"action",
"auto racing",
"seat",
"computer"
],
"probs": [
0.9784166812896729,
0.9754077196121216,
0.974927544593811,
0.9747141003608704,
0.9636767506599426,
0.954016923904419,
0.9406172633171082,
0.9292353391647339,
0.9243901968002319,
0.911110520362854,
0.8961678147315979,
0.8841571807861328,
0.8797860145568848,
0.8792335987091064,
0.869953989982605,
0.8654355406761169,
0.8574825525283813,
0.8546661734580994,
0.8530611991882324,
0.8511127829551697
]
}
},
"docid_str": "799af313d0500ce2648b9a4e20c49902"
}
]
}
回答
I solved my own problem by using Json::Reader. I'm posting the code to help others who will face this problem. I'm getting the "classes" tag with the code snippet below:
我通过使用Json::Reader解决了我自己的问题。我发布的代码是为了帮助那些将面临这个问题的人。
我正在使用下面的代码段获取“classes”标记:
Json::Value root; Json::Reader reader; reader.parse(js,root);//解析后放到root对象里,注意参数的使用 const Json::Value classes = root["results"][0]["result"]["tag"]["classes"];//注意[0]是数组关系,[""]是属性关系,属性是子, //[]前面是父