因为工作需要,需要对一段嵌套的json进行解析。其中我使用的开发语言是C++。
由于rapidjson的解析效率高效,所以今天研究了下RapidJson,下面是一段我用RapidJson解析的一段具有嵌套数组的json文本代码。
JSON文本如下:
{ "Info": [ { "lots": 10, "order_algorithm": "01", "buy_close": 9000, "spread_shift": 0, "position_b_sell": 0, "position_a_buy_today": 0, "position_a_buy_yesterday": 0, "sell_open": -9000, "list_instrument_id": [ "rb1705", "rb1701" ], "position_b_buy_today": 0, "buy_open": -9000, "position_a_sell_yesterday": 0, "strategy_id": "02", "position_b_buy": 0, "a_wait_price_tick": 1, "trade_model": "boll_reversion", "b_wait_price_tick": 0, "sell_close": 9000, "only_close": 0, "order_action_limit": 400, "is_active": 1, "lots_batch": 1, "position_a_sell": 0, "position_b_buy_yesterday": 0, "user_id": "063802", "position_a_buy": 0, "trader_id": "1601", "position_a_sell_today": 0, "stop_loss": 0, "position_b_sell_today": 0, "position_b_sell_yesterday": 0, "on_off": 0 }, { "lots": 10, "order_algorithm": "01", "buy_close": 9000, "spread_shift": 0, "position_b_sell": 0, "position_a_buy_today": 0, "position_a_buy_yesterday": 0, "sell_open": -9000, "list_instrument_id": [ "ni1705", "ni1701" ], "position_b_buy_today": 0, "buy_open": -9000, "position_a_sell_yesterday": 0, "strategy_id": "01", "position_b_buy": 0, "a_wait_price_tick": 1, "trade_model": "boll_reversion", "b_wait_price_tick": 0, "sell_close": 9000, "only_close": 0, "order_action_limit": 400, "is_active": 1, "lots_batch": 1, "position_a_sell": 0, "position_b_buy_yesterday": 0, "user_id": "063802", "position_a_buy": 0, "trader_id": "1601", "position_a_sell_today": 0, "stop_loss": 0, "position_b_sell_today": 0, "position_b_sell_yesterday": 0, "on_off": 0 } ], "MsgSendFlag": 0, "MsgErrorReason": "ID or password error", "MsgRef": 1, "MsgType": 3, "MsgResult": 0 }
解析其格式的C++代码如下,我只解析关键部分,其余读者可以自行添加。
#include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <iostream> using namespace rapidjson; int main() { // 1. Parse a JSON string into DOM. const char* json ="{\"Info\":[{\"lots\":10,\"order_algorithm\":\"01\",\"buy_close\":9000,\"spread_shift\":0,\"position_b_sell\":0,\"position_a_buy_today\":0,\"position_a_buy_yesterday\":0,\"sell_open\":-9000,\"list_instrument_id\":[\"rb1705\",\"rb1701\"],\"position_b_buy_today\":0,\"buy_open\":-9000,\"position_a_sell_yesterday\":0,\"strategy_id\":\"02\",\"position_b_buy\":0,\"a_wait_price_tick\":1,\"trade_model\":\"boll_reversion\",\"b_wait_price_tick\":0,\"sell_close\":9000,\"only_close\":0,\"order_action_limit\":400,\"is_active\":1,\"lots_batch\":1,\"position_a_sell\":0,\"position_b_buy_yesterday\":0,\"user_id\":\"063802\",\"position_a_buy\":0,\"trader_id\":\"1601\",\"position_a_sell_today\":0,\"stop_loss\":0,\"position_b_sell_today\":0,\"position_b_sell_yesterday\":0,\"on_off\":0},{\"lots\":20,\"order_algorithm\":\"02\",\"buy_close\":9000,\"spread_shift\":0,\"position_b_sell\":0,\"position_a_buy_today\":0,\"position_a_buy_yesterday\":0,\"sell_open\":-9000,\"list_instrument_id\":[\"ni1705\",\"ni1701\"],\"position_b_buy_today\":0,\"buy_open\":-9000,\"position_a_sell_yesterday\":0,\"strategy_id\":\"01\",\"position_b_buy\":0,\"a_wait_price_tick\":1,\"trade_model\":\"boll_reversion\",\"b_wait_price_tick\":0,\"sell_close\":9000,\"only_close\":0,\"order_action_limit\":400,\"is_active\":1,\"lots_batch\":1,\"position_a_sell\":0,\"position_b_buy_yesterday\":0,\"user_id\":\"063802\",\"position_a_buy\":0,\"trader_id\":\"1601\",\"position_a_sell_today\":0,\"stop_loss\":0,\"position_b_sell_today\":0,\"position_b_sell_yesterday\":0,\"on_off\":0}],\"MsgSendFlag\":0,\"MsgErrorReason\":\"IDorpassworderror\",\"MsgRef\":1,\"MsgType\":3,\"MsgResult\":0}"; //std::cout << json << std::endl; //std::cout << "\n" << std::endl; std::cout << "|==================|" << std::endl; std::cout << "||rapidjson output||" << std::endl; std::cout << "|==================|" << std::endl; Document doc; doc.Parse(json); // 2. Modify it by DOM. Value& s = doc["MsgSendFlag"]; s.SetInt(s.GetInt() + 1); Value& infoArray = doc["Info"]; if (infoArray.IsArray()) { for (int i = 0; i < infoArray.Size(); i++) { const Value& object = infoArray[i]; int lots = object["lots"].GetInt(); std::string order_algorithm = object["order_algorithm"].GetString(); std::cout << "int lots = " << lots << std::endl; std::cout << "string order_algorithm = " << order_algorithm << std::endl; const Value& info_object = object["list_instrument_id"]; if (info_object.IsArray()) { for (int j = 0; j < info_object.Size(); j++) { std::string instrument = info_object[j].GetString(); std::cout << "instrument[" << j << "] = " << instrument << std::endl; } } } } // 3. Stringify the DOM StringBuffer buffer; Writer<StringBuffer> writer(buffer); doc.Accept(writer); std::cout << buffer.GetString() << std::endl; return 0; }
运行结果如下:
参考:http://stackoverflow.com/questions/23170718/rapidjson-get-a-value-inside-an-array-of-another-array
http://www.cnblogs.com/ziyouchutuwenwu/p/3790322.html
文章的脚注信息由WordPress的wp-posturl插件自动生成
2017年07月13日 下午 7:40 沙发
{“link_mask”:1,”defence_time”:{“day_list”:[{“seg_list”:[{“enable”:5,”start”:5,”end”:5},{“enable”:5,”start”:5,”end”:5}]}]},”record_chns”:”0000000″,”domes”:{“channel_list”:[{“type”:0,”index”:0}]}}能帮忙把这个串解析下吗?
2017年07月23日 下午 10:01 1层
@Sky 只有标准JSON格式才可以进行解析。我将你发给我的这串json在json工具里进行解析,发现并不是标准json格式。
2017年07月23日 下午 9:59
@[整理][RapidJson用法2]C++之RapidJson创建嵌套数组json | 勤奋的小青蛙 你这个不属于标准json格式
来自外部的引用: 1