本文继续上篇博文的研究:http://www.jyguagua.com/?p=2469
上篇博文里我通过RapidJson解析了嵌套的json文本,那么本文章是RapidJson创建嵌套数组Json文本。
代码:
#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.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); doc.AddMember("MsgSendFlag", 1, allocator); doc.AddMember("MsgErrorReason", "IDorpassworderror", allocator); doc.AddMember("MsgRef", 1, allocator); rapidjson::Value info_array(rapidjson::kArrayType); for (int i = 0; i < 2; i++) { rapidjson::Value info_object(rapidjson::kObjectType); info_object.SetObject(); info_object.AddMember("lots", 10 + i, allocator); info_object.AddMember("order_algorithm", "01", allocator); rapidjson::Value instrument_array(rapidjson::kArrayType); for (int j = 0; j < 2; j++) { rapidjson::Value instrument_object(rapidjson::kObjectType); instrument_object.SetObject(); instrument_object.SetString("cu1701"); instrument_array.PushBack(instrument_object, allocator); } info_object.AddMember("list_instrument_id", instrument_array, allocator); info_array.PushBack(info_object, allocator); } doc.AddMember("Info", info_array, allocator); // 3. Stringify the DOM StringBuffer buffer; Writer<StringBuffer> writer(buffer); doc.Accept(writer); std::cout << buffer.GetString() << std::endl; return 0; }
运行效果所创建的JSON文本为:
{ "MsgSendFlag": 1, "MsgErrorReason": "IDorpassworderror", "MsgRef": 1, "Info": [ { "lots": 10, "order_algorithm": "01", "list_instrument_id": [ "cu1701", "cu1701" ] }, { "lots": 11, "order_algorithm": "01", "list_instrument_id": [ "cu1701", "cu1701" ] } ] }
其JSON格式化视图如下:
文章的脚注信息由WordPress的wp-posturl插件自动生成