C ++ - Qt字符串,拆分成Json字符串?

时间:2017-05-01 14:40:22

标签: c++ json string qt

将字符串拆分为char *结果失败

// MainInput string = a/b/c
QMessageBox msgBox;
QString qs = QString::fromLocal8Bit(MainInput.c_str());
QStringList list1 = qs.split('/');

// Expected Result: 
// "{\"settings\": {\"key\": \"c\",\"server\" : \"a.com\"},\"type\" : \"fixed\"}";

// Now we add one by one to pepare the expected result
char *a = "{\"settings\": {\"key\": \"";

QByteArray ba = list1[2].toLatin1();
char *b = ba.data();

char *c = "\",\"server\" : \""; 

QByteArray da = list1[0].toLatin1();
char *d = da.data();

char *e = ".com\"},\"type\" : \"fixed\"}";

char *result;
result = strcpy(result, a); // <<< CRASH / FAIL HERE
result = strcpy(result, b);
result = strcpy(result, c);
result = strcpy(result, d);
result = strcpy(result, e);

msgBox.setText(result);
msgBox.exec();
//data_create_from_json(result); 

1 个答案:

答案 0 :(得分:1)

<style type="text/css"> * { box-sizing: border-box; } body { margin: 3em; background: #efefef; } input[type=checkbox] { display: block; width: 30px; height: 30px; cursor: pointer; background-repeat: no-repeat; background-position: center center; background-size: contain; -webkit-appearance: none; outline: 0; background: white; border-radius: 3px; border: 2px solid #dedede; transition: border 0.3s ease; } input[type="checkbox"]:hover{ border-radius: 3px; border: 2px solid #4291DB; transition: border 0.3s ease; } input[type=checkbox]:checked { background-image: url('data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgNDkgNDEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGcgaWQ9IlBhZ2UtMSIgc3Ryb2tlPSJub25lIiBzdHJva2Utd2lkdGg9IjEiIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+PHBvbHlsaW5lIGlkPSJQYXRoIiBzdHJva2U9IiM0MjkxREIiIHN0cm9rZS13aWR0aD0iMTEiIHBvaW50cz0iMy43NTY1MzE0OCAxOC45ODA0MDUyIDIyLjc1Mzc0MjQgMzMuMDg5OTk4NiA0NC41ODgzMTcxIDMuNDk1NDY5MjIiPjwvcG9seWxpbmU+PC9nPjwvc3ZnPg=='); background-repeat: no-repeat; background-position: center left 2px; background-size: 75%; border-radius: 3px; border: 2px solid #4291DB; transition: border 0.3s ease; } </style> <div class="text-center"> <label> <input type="checkbox" value="on"> </label> </div> 拥有自己的QT个对象,请使用这些对象代替JSON

strcpy