Skip to content

Commit 1784d5d

Browse files
author
zhangbin4
committed
json array from vector and class
1 parent d6c6109 commit 1784d5d

File tree

1 file changed

+205
-33
lines changed

1 file changed

+205
-33
lines changed

JSON/JSON/JSON.cpp

Lines changed: 205 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ j5{
141141
"content":[{
142142
"key1" : "%E5%86%85%E5%AE%B91",
143143
"key2" : "%E5%86%AE%B91",
144-
144+
145145
"key3" : "内容3"
146146
},{
147147
"key1" : "内容1",
@@ -207,20 +207,21 @@ j["object"] = {{"currency", "USD"}, {"value", 42.99}};
207207
j["module"] = "china";
208208
std::cout << std::setw(4) << j << std::endl;
209209

210-
std::cout << "=============================" << std::endl;
211-
//怎么变为了这个样子: "1content": " [ \"one\", \"two\", \"three\" ]", ??????TODO
210+
std::cout << "=============================" << std::endl;
211+
//怎么变为了这个样子: "1content": " [ \"one\", \"two\", \"three\" ]", ??????TODO
212212
j["1_content"] = R"( [ "one", "two", "three" ])";
213-
/*
214-
"2_content": "[\n { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\", \"path\": \"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} \n]",
215-
*/
216-
j["2_content"] = R"([
213+
/*
214+
"2_content": "[\n { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\",
215+
\"path\": \"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} \n]",
216+
*/
217+
j["2_content"] = R"([
217218
{ "op": "replace", "path": "/baz", "value": "boo" },
218219
{ "op": "add", "path": "/hello", "value": ["world"] },
219220
{ "op": "remove", "path": "/foo"}
220221
])";
221-
/*
222-
3 是这样的,看起来这个就符合要求
223-
"3_content": [
222+
/*
223+
3 是这样的,看起来这个就符合要求
224+
"3_content": [
224225
{
225226
"op": "replace",
226227
"path": "/baz",
@@ -238,60 +239,231 @@ j["object"] = {{"currency", "USD"}, {"value", 42.99}};
238239
"path": "/foo"
239240
}
240241
],
241-
*/
242-
//这个也符号要求,j["2_content"] 的方法可以直接转为json的字符串
243-
j["3_content"] =
244-
json::parse("[ { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\", \"path\": "
245-
"\"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} ]");
246-
/*
247-
GOOD!
248-
厉害了,这个符合要求
249-
"4_content": [
242+
*/
243+
//这个也符号要求,j["2_content"] 的方法可以直接转为json的字符串
244+
j["3_content"] = json::parse(
245+
"[ { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\", \"path\": "
246+
"\"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} ]");
247+
/*
248+
GOOD!
249+
厉害了,这个符合要求
250+
"4_content": [
250251
{
251-
"currency": "USD",
252-
"nihao": "home",
253-
"value": 42.99
252+
"currency": "USD",
253+
"nihao": "home",
254+
"value": 42.99
254255
},
255256
{
256-
"5": "5",
257-
"6": 42.99,
258-
"7": "8"
257+
"5": "5",
258+
"6": 42.99,
259+
"7": "8"
259260
},
260261
{
261-
"5": "5",
262-
"6": 42.99,
263-
"7": "8"
262+
"5": "5",
263+
"6": 42.99,
264+
"7": "8"
264265
}
265266
],
266-
*/
267-
json m1= {{"currency", "USD"}, {"value", 42.99}, {"nihao", "home"}};
267+
*/
268+
json m1 = {{"currency", "USD"}, {"value", 42.99}, {"nihao", "home"}};
268269
json m2 = {{"1", "2"}, {"3", 42.99}, {"4", "5"}};
269270
json m3 = {{"5", "5"}, {"6", 42.99}, {"7", "8"}};
270-
//这样符合要求
271+
//这样符合要求
271272
j["4_content"] = {m1, m3, m3};
272273
std::cout << std::setw(4) << j << std::endl;
273274
}
274-
void creatPatch() {
275+
void creatPatch()
276+
{
275277
// a JSON value
276278
json j_original = R"({
277279
"baz": ["one", "two", "three"],
278280
"foo": "bar"
279281
})"_json;
280282

281-
//https://www.jianshu.com/p/69e57f2af904
283+
// https://www.jianshu.com/p/69e57f2af904
282284
// a JSON patch (RFC 6902)
283285
json j_patch = R"([
284286
{ "op": "replace", "path": "/baz", "value": "boo" },
285287
{ "op": "add", "path": "/hello", "value": ["world"] },
286288
{ "op": "remove", "path": "/foo"}
287289
])"_json;
288290
}
291+
class Component
292+
{
293+
public:
294+
Component(std::string type) { name = type;
295+
}
296+
/*
297+
{
298+
"TIME": "china_2",
299+
"Type": "1_china"
300+
},
301+
*/
302+
void Serialize(json& outJson) { outJson["Type"] = std::to_string(++id) + "_"+name;
303+
outJson["TIME"] = name + "_" + std::to_string(++id);
304+
}
305+
306+
private:
307+
static int id;
308+
std::string name;
309+
};
310+
int Component::id = 0;
311+
/*
312+
d
313+
314+
[
315+
{
316+
"Components": [
317+
{
318+
"Type": "1_china"
319+
},
320+
{
321+
"Type": "1_usa"
322+
},
323+
{
324+
"Type": "1_english"
325+
}
326+
],
327+
"Name": "zhangbin"
328+
}
329+
]
330+
331+
*/
332+
void createVecor(json& d)
333+
{
334+
json thing;
335+
thing["Name"] = "zhangbin";
336+
json& componentsJson = thing["Components"];
337+
338+
std::vector<Component> sss;
339+
sss.push_back(Component("china"));
340+
sss.push_back(Component ("usa"));
341+
sss.push_back(Component ("english"));
342+
auto comps = sss;
343+
for (auto comp : comps)
344+
{
345+
json compJson;
346+
comp.Serialize(compJson);
347+
/*
348+
从vector来自动变为数组
349+
[
350+
{
351+
"TIME": "china_2",
352+
"Type": "1_china"
353+
},
354+
{
355+
"TIME": "usa_4",
356+
"Type": "3_usa"
357+
},
358+
{
359+
"TIME": "english_6",
360+
"Type": "5_english"
361+
}
362+
],
363+
*/
364+
componentsJson.push_back(compJson);
365+
}
366+
std::cout << "===========thing==================" << std::endl;
367+
/*
368+
{
369+
"Components": [
370+
{
371+
"Type": "1_china"
372+
},
373+
{
374+
"Type": "2_usa"
375+
},
376+
{
377+
"Type": "3_english"
378+
}
379+
],
380+
"Name": "zhangbin"
381+
}
382+
*/
383+
std::cout << std::setw(4) << thing << std::endl;
384+
d.push_back(thing);
385+
d.push_back(thing);
386+
}
289387
int main()
290388
{
291389
std::cout << "Hello World!\n";
292390
createJson();
293391
create100();
294392
createarray();
393+
std::cout << "=============================" << std::endl;
394+
/*
395+
[
396+
{
397+
"Components": [
398+
{
399+
"Type": "1_china"
400+
},
401+
{
402+
"Type": "1_usa"
403+
},
404+
{
405+
"Type": "1_english"
406+
}
407+
],
408+
"Name": "zhangbin"
409+
},
410+
{
411+
"Components": [
412+
{
413+
"Type": "1_china"
414+
},
415+
{
416+
"Type": "1_usa"
417+
},
418+
{
419+
"Type": "1_english"
420+
}
421+
],
422+
"Name": "zhangbin"
423+
}
424+
]
425+
*/
426+
json ddd;
427+
createVecor(ddd);
428+
std::cout << std::setw(4) << ddd << std::endl;
429+
json d;
430+
d["content"] = ddd;
431+
std::cout << "=============================" << std::endl;
432+
/*
433+
{
434+
"content": [
435+
{
436+
"Components": [
437+
{
438+
"Type": "1_china"
439+
},
440+
{
441+
"Type": "1_usa"
442+
},
443+
{
444+
"Type": "1_english"
445+
}
446+
],
447+
"Name": "zhangbin"
448+
},
449+
{
450+
"Components": [
451+
{
452+
"Type": "1_china"
453+
},
454+
{
455+
"Type": "1_usa"
456+
},
457+
{
458+
"Type": "1_english"
459+
}
460+
],
461+
"Name": "zhangbin"
462+
}
463+
]
464+
}
465+
*/
466+
std::cout << std::setw(4) << d << std::endl;
295467
system("pause");
296468
return 0;
297469
}

0 commit comments

Comments
 (0)