C++ Json数据保存与解析(到文件)
直接上代码
1
#include
"
stdafx.h
"
2 #include < fstream >
3 #include " Json.h "
4
5 #include " ShopLogic.h "
6 #include " src/FileCsv/WebUrlConfig.h "
7 #include " src/support/GlobalDefine.h "
8
9 using namespace std;
10
11 const char * g_pcszWebDataSaveFile = " shopWebData_ " ;
12
13 ////////////////////////////////////////////////////////////////////////////// /
14 /// class CShopData
15 ///
16 CShopData * getShopData() {
17 return ChaSingleton < CShopData > ::singleton();
18 }
19
20 CShopData::CShopData() {
21
22 }
23
24 CShopData:: ~ CShopData() {
25
26 }
27
28 ///
29 /// Add by (jacc.kim) [2014-10-16 17:01]
30 /// 将从web下发的商品列表数据保存到本地配置中.注意:如果列表为空,则不保存,
31 /// 同时旧有的本地数据也不清空。
32 ///
33 void CShopData::saveShopWebItems( const TShopWebDataVec & vecItems) {
34 if (vecItems.empty()) {
35 return ;
36 }
37 ShopWebDataLog( " ======================================================== " );
38 ShopWebDataLog( " 准备保存宝石商店数据到本地文件. " );
39 std:: string strFileName( "" );
40 this -> generateWebDataSaveFileName(strFileName);
41 ShopWebDataLog( " 保存数据文件名称: %s " , strFileName.c_str());
42 ofstream fout;
43 fout.open(strFileName.c_str());
44 if ( ! fout.is_open()) {
45 ShopWebDataLog( " 打开或创建文件失败 " );
46 return ;
47 }
48 Json::Value jRoot;
49 Json::Value jArray;
50 Json::Value jItem;
51 Json::Value jHttpData;
52 Json::Value jTreasureInfo;
53 typedef Json::Value::Int TJInt;
54
55 CShopHttpDataTotal * pWebDataItem = NULL;
56 const TShopWebDataVec::size_type nAmount = vecItems.size();
57 for (TShopWebDataVec::size_type nIdx = 0 ; nIdx < nAmount; ++ nIdx) {
58 pWebDataItem = vecItems[nIdx];
59 if (NULL == pWebDataItem) {
60 continue ;
61 }
62 // 保存 网络商店的数据(即:CShopHttpData) 数据
63 jHttpData[ " pc_id " ] = pWebDataItem -> httpData.pc_id.c_str();
64 jHttpData[ " pc_name " ] = pWebDataItem -> httpData.pc_name.c_str();
65 jHttpData[ " pc_point " ] = (TJInt)pWebDataItem -> httpData.pc_point;
66 jHttpData[ " pc_type " ] = pWebDataItem -> httpData.pc_type.c_str();
67 jHttpData[ " pc_flag " ] = (TJInt)pWebDataItem -> httpData.pc_flag;
68 jHttpData[ " pc_credit_rate " ] = pWebDataItem -> httpData.pc_credit_rate.c_str();
69 jHttpData[ " pcc_price_usd " ] = pWebDataItem -> httpData.pcc_price_usd.c_str();
70 jHttpData[ " pcc_price_cfg " ] = pWebDataItem -> httpData.pcc_price_cfg.c_str();
71 jHttpData[ " pcc_memo " ] = pWebDataItem -> httpData.pcc_memo.c_str();
72 jHttpData[ " pc_free_point " ] = (TJInt)pWebDataItem -> httpData.pc_free_point;
73 jHttpData[ " ppc_end_time " ] = pWebDataItem -> httpData.ppc_end_time.c_str();
74 #ifdef ORDERS_PROMOTIONS
75 jHttpData[ " pcc_presen_count " ] = (TJInt)pWebDataItem -> httpData.pcc_presen_count;
76 #else // #ifdef ORDERS_PROMOTIONS
77 jHttpData[ " pcc_presen_count " ] = (TJInt) 0 ;
78 #endif // #ifdef ORDERS_PROMOTIONS
79
80 // 保存 商城财产相关信息(即:TreasureInfo) 数据
81 jTreasureInfo[ " nIconStatus " ] = (TJInt)pWebDataItem -> localData.nIconStatus;
82 jTreasureInfo[ " nTextStatus " ] = (TJInt)pWebDataItem -> localData.nTextStatus;
83 jTreasureInfo[ " nBuyType " ] = (TJInt)pWebDataItem -> localData.nBuyType;
84 jTreasureInfo[ " nCostType " ] = (TJInt)pWebDataItem -> localData.nCostType;
85 jTreasureInfo[ " nBuyNumber " ] = (TJInt)pWebDataItem -> localData.nBuyNumber;
86 jTreasureInfo[ " nCostNumber " ] = (TJInt)pWebDataItem -> localData.nCostNumber;
87 jTreasureInfo[ " nCapacityPercentage " ] = (TJInt)pWebDataItem -> localData.nCapacityPercentage;
88 jTreasureInfo[ " strName " ] = pWebDataItem -> localData.strName.c_str();
89 jTreasureInfo[ " strTitleTid " ] = pWebDataItem -> localData.strTitleTid.c_str();
90 jTreasureInfo[ " strIconAddress " ] = pWebDataItem -> localData.strIconAddress.c_str();
91 jTreasureInfo[ " nCooling " ] = (TJInt)pWebDataItem -> localData.nCooling;
92
93 jItem[ " CShopHttpData " ] = jHttpData;
94 jItem[ " TreasureInfo " ] = jTreasureInfo;
95 jArray.append(jItem);
96 }
97 ShopWebDataLog( " 待保存的宝石商店数据: %d 条, 实际保存成功: %d 条 "
98 , static_cast < int > (nAmount), static_cast < int > (jArray.size()));
99 jRoot[ " CShopHttpDataTotal " ] = jArray;
100 fout << jRoot.toStyledString() << endl;
101 fout.close();
102 }
103
104 ///
105 /// Add by (jacc.kim) [2014-10-16 17:02]
106 /// 从本地加载web商品列表数据.并返回.注意:刚进入接口内部时,参数列表必被清空.
107 /// 然后再从本地文件中加载.
108 ///
109 void CShopData::getLocalShopWebItems(TShopWebDataVec & vecOutItems) {
110 ShopWebDataLog( " -------------------------------------------------------- " );
111 ShopWebDataLog( " 准备从本地提取数据宝石商店数据. " );
112 vecOutItems.clear();
113 std:: string strFileName( "" );
114 this -> generateWebDataSaveFileName(strFileName);
115 ShopWebDataLog( " 本地数据源文件名称: %s " , strFileName.c_str());
116 ifstream fin;
117 fin.open(strFileName.c_str());
118 if ( ! fin.is_open()) {
119 ShopWebDataLog( " 本地数据源文件不存在或已损坏,打开失败 " );
120 return ;
121 }
122 Json::Reader jReader;
123 Json::Value jRoot;
124 Json::Value jHttpData;
125 Json::Value jTreasureInfo;
126 if ( ! jReader.parse(fin, jRoot)) {
127 ShopWebDataLog( " 本地数据源文件糟破坏,解析失败 " );
128 return ;
129 }
130 CShopHttpDataTotal * pWebDataItem = NULL;
131 const int nAmount = static_cast < int > (jRoot[ " CShopHttpDataTotal " ].size());
132 for ( int nIdx = 0 ; nIdx < nAmount; ++ nIdx) {
133 pWebDataItem = new CShopHttpDataTotal();
134 if (NULL == pWebDataItem) {
135 break ;
136 }
137 jHttpData = jRoot[ " CShopHttpDataTotal " ][nIdx][ " CShopHttpData " ];
138 pWebDataItem -> httpData.pc_id = jHttpData[ " pc_id " ].asString();
139 pWebDataItem -> httpData.pc_name = jHttpData[ " pc_name " ].asString();
140 pWebDataItem -> httpData.pc_point = jHttpData[ " pc_point " ].asInt();
141 pWebDataItem -> httpData.pc_type = jHttpData[ " pc_type " ].asString();
142 pWebDataItem -> httpData.pc_flag = jHttpData[ " pc_flag " ].asInt();
143 pWebDataItem -> httpData.pc_credit_rate = jHttpData[ " pc_credit_rate " ].asString();
144 pWebDataItem -> httpData.pcc_price_usd = jHttpData[ " pcc_price_usd " ].asString();
145 pWebDataItem -> httpData.pcc_price_cfg = jHttpData[ " pcc_price_cfg " ].asString();
146 pWebDataItem -> httpData.pcc_memo = jHttpData[ " pcc_memo " ].asString();
147 pWebDataItem -> httpData.pc_free_point = jHttpData[ " pc_free_point " ].asInt();
148 pWebDataItem -> httpData.ppc_end_time = jHttpData[ " ppc_end_time " ].asString();
149 #ifdef ORDERS_PROMOTIONS
150 pWebDataItem -> httpData.pcc_presen_count = jHttpData[ " pcc_presen_count " ].asInt();
151 #else // #ifdef ORDERS_PROMOTIONS
152 // do nothing here
..
153 #endif // #ifdef ORDERS_PROMOTIONS
154
155 jTreasureInfo = jRoot[ " CShopHttpDataTotal " ][nIdx][ " TreasureInfo " ];
156 pWebDataItem -> localData.nIconStatus = jTreasureInfo[ " nIconStatus " ].asInt();
157 pWebDataItem -> localData.nTextStatus = jTreasureInfo[ " nTextStatus " ].asInt();
158 pWebDataItem -> localData.nBuyType = jTreasureInfo[ " nBuyType " ].asInt();
159 pWebDataItem -> localData.nCostType = jTreasureInfo[ " nCostType " ].asInt();
160 pWebDataItem -> localData.nBuyNumber = jTreasureInfo[ " nBuyNumber " ].asInt();
161 pWebDataItem -> localData.nCostNumber = jTreasureInfo[ " nCostNumber " ].asInt();
162 pWebDataItem -> localData.nCapacityPercentage = jTreasureInfo[ " nCapacityPercentage " ].asInt();
163 pWebDataItem -> localData.strName = jTreasureInfo[ " strName " ].asString();
164 pWebDataItem -> localData.strTitleTid = jTreasureInfo[ " strTitleTid " ].asString();
165 pWebDataItem -> localData.strIconAddress = jTreasureInfo[ " strIconAddress " ].asString();
166 pWebDataItem -> localData.nCooling = jTreasureInfo[ " nCooling " ].asInt();
167
168 vecOutItems.push_back(pWebDataItem);
169 }
170 ShopWebDataLog( " 本地数据源共: %d 条数据, 实际解析成功: %d 条 "
171 , nAmount, static_cast < int > (vecOutItems.size()));
172 fin.close();
173 }
174
175 void CShopData::generateWebDataSaveFileName(std:: string & strFileName) {
176 strFileName = g_pcszWebDataSaveFile;
177 CWebUrlConfig * pUrlCfg = CWebUrlConfigInstance::singleton();
178 std:: string strGameId = pUrlCfg -> getWebUrlAddress( " gameid " );
179 strFileName = strFileName + strGameId + " .json " ;
180 }
2 #include < fstream >
3 #include " Json.h "
4
5 #include " ShopLogic.h "
6 #include " src/FileCsv/WebUrlConfig.h "
7 #include " src/support/GlobalDefine.h "
8
9 using namespace std;
10
11 const char * g_pcszWebDataSaveFile = " shopWebData_ " ;
12
13 ////////////////////////////////////////////////////////////////////////////// /
14 /// class CShopData
15 ///
16 CShopData * getShopData() {
17 return ChaSingleton < CShopData > ::singleton();
18 }
19
20 CShopData::CShopData() {
21
22 }
23
24 CShopData:: ~ CShopData() {
25
26 }
27
28 ///
29 /// Add by (jacc.kim) [2014-10-16 17:01]
30 /// 将从web下发的商品列表数据保存到本地配置中.注意:如果列表为空,则不保存,
31 /// 同时旧有的本地数据也不清空。
32 ///
33 void CShopData::saveShopWebItems( const TShopWebDataVec & vecItems) {
34 if (vecItems.empty()) {
35 return ;
36 }
37 ShopWebDataLog( " ======================================================== " );
38 ShopWebDataLog( " 准备保存宝石商店数据到本地文件. " );
39 std:: string strFileName( "" );
40 this -> generateWebDataSaveFileName(strFileName);
41 ShopWebDataLog( " 保存数据文件名称: %s " , strFileName.c_str());
42 ofstream fout;
43 fout.open(strFileName.c_str());
44 if ( ! fout.is_open()) {
45 ShopWebDataLog( " 打开或创建文件失败 " );
46 return ;
47 }
48 Json::Value jRoot;
49 Json::Value jArray;
50 Json::Value jItem;
51 Json::Value jHttpData;
52 Json::Value jTreasureInfo;
53 typedef Json::Value::Int TJInt;
54
55 CShopHttpDataTotal * pWebDataItem = NULL;
56 const TShopWebDataVec::size_type nAmount = vecItems.size();
57 for (TShopWebDataVec::size_type nIdx = 0 ; nIdx < nAmount; ++ nIdx) {
58 pWebDataItem = vecItems[nIdx];
59 if (NULL == pWebDataItem) {
60 continue ;
61 }
62 // 保存 网络商店的数据(即:CShopHttpData) 数据
63 jHttpData[ " pc_id " ] = pWebDataItem -> httpData.pc_id.c_str();
64 jHttpData[ " pc_name " ] = pWebDataItem -> httpData.pc_name.c_str();
65 jHttpData[ " pc_point " ] = (TJInt)pWebDataItem -> httpData.pc_point;
66 jHttpData[ " pc_type " ] = pWebDataItem -> httpData.pc_type.c_str();
67 jHttpData[ " pc_flag " ] = (TJInt)pWebDataItem -> httpData.pc_flag;
68 jHttpData[ " pc_credit_rate " ] = pWebDataItem -> httpData.pc_credit_rate.c_str();
69 jHttpData[ " pcc_price_usd " ] = pWebDataItem -> httpData.pcc_price_usd.c_str();
70 jHttpData[ " pcc_price_cfg " ] = pWebDataItem -> httpData.pcc_price_cfg.c_str();
71 jHttpData[ " pcc_memo " ] = pWebDataItem -> httpData.pcc_memo.c_str();
72 jHttpData[ " pc_free_point " ] = (TJInt)pWebDataItem -> httpData.pc_free_point;
73 jHttpData[ " ppc_end_time " ] = pWebDataItem -> httpData.ppc_end_time.c_str();
74 #ifdef ORDERS_PROMOTIONS
75 jHttpData[ " pcc_presen_count " ] = (TJInt)pWebDataItem -> httpData.pcc_presen_count;
76 #else // #ifdef ORDERS_PROMOTIONS
77 jHttpData[ " pcc_presen_count " ] = (TJInt) 0 ;
78 #endif // #ifdef ORDERS_PROMOTIONS
79
80 // 保存 商城财产相关信息(即:TreasureInfo) 数据
81 jTreasureInfo[ " nIconStatus " ] = (TJInt)pWebDataItem -> localData.nIconStatus;
82 jTreasureInfo[ " nTextStatus " ] = (TJInt)pWebDataItem -> localData.nTextStatus;
83 jTreasureInfo[ " nBuyType " ] = (TJInt)pWebDataItem -> localData.nBuyType;
84 jTreasureInfo[ " nCostType " ] = (TJInt)pWebDataItem -> localData.nCostType;
85 jTreasureInfo[ " nBuyNumber " ] = (TJInt)pWebDataItem -> localData.nBuyNumber;
86 jTreasureInfo[ " nCostNumber " ] = (TJInt)pWebDataItem -> localData.nCostNumber;
87 jTreasureInfo[ " nCapacityPercentage " ] = (TJInt)pWebDataItem -> localData.nCapacityPercentage;
88 jTreasureInfo[ " strName " ] = pWebDataItem -> localData.strName.c_str();
89 jTreasureInfo[ " strTitleTid " ] = pWebDataItem -> localData.strTitleTid.c_str();
90 jTreasureInfo[ " strIconAddress " ] = pWebDataItem -> localData.strIconAddress.c_str();
91 jTreasureInfo[ " nCooling " ] = (TJInt)pWebDataItem -> localData.nCooling;
92
93 jItem[ " CShopHttpData " ] = jHttpData;
94 jItem[ " TreasureInfo " ] = jTreasureInfo;
95 jArray.append(jItem);
96 }
97 ShopWebDataLog( " 待保存的宝石商店数据: %d 条, 实际保存成功: %d 条 "
98 , static_cast < int > (nAmount), static_cast < int > (jArray.size()));
99 jRoot[ " CShopHttpDataTotal " ] = jArray;
100 fout << jRoot.toStyledString() << endl;
101 fout.close();
102 }
103
104 ///
105 /// Add by (jacc.kim) [2014-10-16 17:02]
106 /// 从本地加载web商品列表数据.并返回.注意:刚进入接口内部时,参数列表必被清空.
107 /// 然后再从本地文件中加载.
108 ///
109 void CShopData::getLocalShopWebItems(TShopWebDataVec & vecOutItems) {
110 ShopWebDataLog( " -------------------------------------------------------- " );
111 ShopWebDataLog( " 准备从本地提取数据宝石商店数据. " );
112 vecOutItems.clear();
113 std:: string strFileName( "" );
114 this -> generateWebDataSaveFileName(strFileName);
115 ShopWebDataLog( " 本地数据源文件名称: %s " , strFileName.c_str());
116 ifstream fin;
117 fin.open(strFileName.c_str());
118 if ( ! fin.is_open()) {
119 ShopWebDataLog( " 本地数据源文件不存在或已损坏,打开失败 " );
120 return ;
121 }
122 Json::Reader jReader;
123 Json::Value jRoot;
124 Json::Value jHttpData;
125 Json::Value jTreasureInfo;
126 if ( ! jReader.parse(fin, jRoot)) {
127 ShopWebDataLog( " 本地数据源文件糟破坏,解析失败 " );
128 return ;
129 }
130 CShopHttpDataTotal * pWebDataItem = NULL;
131 const int nAmount = static_cast < int > (jRoot[ " CShopHttpDataTotal " ].size());
132 for ( int nIdx = 0 ; nIdx < nAmount; ++ nIdx) {
133 pWebDataItem = new CShopHttpDataTotal();
134 if (NULL == pWebDataItem) {
135 break ;
136 }
137 jHttpData = jRoot[ " CShopHttpDataTotal " ][nIdx][ " CShopHttpData " ];
138 pWebDataItem -> httpData.pc_id = jHttpData[ " pc_id " ].asString();
139 pWebDataItem -> httpData.pc_name = jHttpData[ " pc_name " ].asString();
140 pWebDataItem -> httpData.pc_point = jHttpData[ " pc_point " ].asInt();
141 pWebDataItem -> httpData.pc_type = jHttpData[ " pc_type " ].asString();
142 pWebDataItem -> httpData.pc_flag = jHttpData[ " pc_flag " ].asInt();
143 pWebDataItem -> httpData.pc_credit_rate = jHttpData[ " pc_credit_rate " ].asString();
144 pWebDataItem -> httpData.pcc_price_usd = jHttpData[ " pcc_price_usd " ].asString();
145 pWebDataItem -> httpData.pcc_price_cfg = jHttpData[ " pcc_price_cfg " ].asString();
146 pWebDataItem -> httpData.pcc_memo = jHttpData[ " pcc_memo " ].asString();
147 pWebDataItem -> httpData.pc_free_point = jHttpData[ " pc_free_point " ].asInt();
148 pWebDataItem -> httpData.ppc_end_time = jHttpData[ " ppc_end_time " ].asString();
149 #ifdef ORDERS_PROMOTIONS
150 pWebDataItem -> httpData.pcc_presen_count = jHttpData[ " pcc_presen_count " ].asInt();
151 #else // #ifdef ORDERS_PROMOTIONS
152 // do nothing here


153 #endif // #ifdef ORDERS_PROMOTIONS
154
155 jTreasureInfo = jRoot[ " CShopHttpDataTotal " ][nIdx][ " TreasureInfo " ];
156 pWebDataItem -> localData.nIconStatus = jTreasureInfo[ " nIconStatus " ].asInt();
157 pWebDataItem -> localData.nTextStatus = jTreasureInfo[ " nTextStatus " ].asInt();
158 pWebDataItem -> localData.nBuyType = jTreasureInfo[ " nBuyType " ].asInt();
159 pWebDataItem -> localData.nCostType = jTreasureInfo[ " nCostType " ].asInt();
160 pWebDataItem -> localData.nBuyNumber = jTreasureInfo[ " nBuyNumber " ].asInt();
161 pWebDataItem -> localData.nCostNumber = jTreasureInfo[ " nCostNumber " ].asInt();
162 pWebDataItem -> localData.nCapacityPercentage = jTreasureInfo[ " nCapacityPercentage " ].asInt();
163 pWebDataItem -> localData.strName = jTreasureInfo[ " strName " ].asString();
164 pWebDataItem -> localData.strTitleTid = jTreasureInfo[ " strTitleTid " ].asString();
165 pWebDataItem -> localData.strIconAddress = jTreasureInfo[ " strIconAddress " ].asString();
166 pWebDataItem -> localData.nCooling = jTreasureInfo[ " nCooling " ].asInt();
167
168 vecOutItems.push_back(pWebDataItem);
169 }
170 ShopWebDataLog( " 本地数据源共: %d 条数据, 实际解析成功: %d 条 "
171 , nAmount, static_cast < int > (vecOutItems.size()));
172 fin.close();
173 }
174
175 void CShopData::generateWebDataSaveFileName(std:: string & strFileName) {
176 strFileName = g_pcszWebDataSaveFile;
177 CWebUrlConfig * pUrlCfg = CWebUrlConfigInstance::singleton();
178 std:: string strGameId = pUrlCfg -> getWebUrlAddress( " gameid " );
179 strFileName = strFileName + strGameId + " .json " ;
180 }