void parseJsonToStruct(const std::string& jsonString, std::vector<TDXDAT2>& result) {
size_t pos = 0;
size_t start = 0;
bool inObject = false;
TDXDAT2 currentData;
while ((pos = jsonString.find("\"", start)) != std::string::npos) {
if (!inObject && jsonString.substr(pos, 8) == "\"stkcode\"") {
inObject = true;
start = pos + 9; // Skip over "stkcode":"
continue;
}
if (inObject) {
if (jsonString[pos - 1] == ':') { // Value starts after ":"
start = pos + 1;
size_t end = jsonString.find("\"", start);
std::string key = jsonString.substr(pos + 1, end - pos - 1);
if (key == "stkcode") {
start = end + 2;
end = jsonString.find("\"", start);
currentData.stkcode = jsonString.substr(start, end - start);
}
else if (key == "stkname") {
start = end + 2;
end = jsonString.find("\"", start);
currentData.stkname = jsonString.substr(start, end - start);
}
else if (key == "stknew") {
start = end + 2;
end = jsonString.find(",", start);
currentData.stknew = std::stof(jsonString.substr(start, end - start));
}
else if (key == "roe") {
start = end + 2;
end = jsonString.find(",", start);
currentData.roe = std::stof(jsonString.substr(start, end - start));
}
else if (key == "mgsy") {
start = end + 2;
end = jsonString.find(",", start);
currentData.mgsy = std::stoi(jsonString.substr(start, end - start));
}
else if (key == "score") {
start = end + 2;
end = jsonString.find(",", start);
currentData.score = std::stoi(jsonString.substr(start, end - start));
}
else if (key == "height") {
start = end + 2;
end = jsonString.find(",", start);
currentData.height = std::stoi(jsonString.substr(start, end - start));
}
else if (key == "c2exp12") {
start = end + 2;
end = jsonString.find(",", start);
currentData.c2exp12 = std::stoi(jsonString.substr(start, end - start));
}
else if (key == "c2exp50") {
start = end + 2;
end = jsonString.find("}", start);
currentData.c2exp50 = std::stoi(jsonString.substr(start, end - start));
result.push_back(currentData);
inObject = false;
}
}
}
start = pos + 1;
}
}
上面的代码在解析下面的数据:
[ { ""stkcode"": ""002592"", ""stkname"": ""ST八菱"", ""stknew"": ""4.78"", ""roe"": ""5.67"", ""mgsy"": ""0.19"", ""score"": ""25"", ""height"": ""2"", ""c2exp12"": ""5.5"", ""c2exp50"": ""5.0"" }, { ""stkcode"": ""603817"", ""stkname"": ""海峡环保"", ""stknew"": ""5.61"", ""roe"": ""6.10"", ""mgsy"": ""0.32"", ""score"": ""23"", ""height"": ""6"", ""c2exp12"": ""2.9"", ""c2exp50"": ""2.0"" }, { ""stkcode"": ""600149"", ""stkname"": ""廊坊发展"", ""stknew"": ""4.21"", ""roe"": ""32.20"", ""mgsy"": ""0.18"", ""score"": ""111"", ""height"": ""7"", ""c2exp12"": ""2.7"", ""c2exp50"": ""-1.3"" }, { ""stkcode"": ""603797"", ""stkname"": ""联泰环保"", ""stknew"": ""3.91"", ""roe"": ""5.19"", ""mgsy"": ""0.29"", ""score"": ""24"", ""height"": ""7"", ""c2exp12"": ""1.1"", ""c2exp50"": ""-0.5"" }, { ""stkcode"": ""300692"", ""stkname"": ""中环环保"", ""stknew"": ""5.55"", ""roe"": ""3.27"", ""mgsy"": ""0.18"", ""score"": ""16"", ""height"": ""10"", ""c2exp12"": ""-0.9"", ""c2exp50"": ""-6.4"" }, { ""stkcode"": ""002412"", ""stkname"": ""汉森制药"", ""stknew"": ""5.88"", ""roe"": ""6.22"", ""mgsy"": ""0.26"", ""score"": ""65"", ""height"": ""12"", ""c2exp12"": ""0.7"", ""c2exp50"": ""-1.7"" }, { ""stkcode"": ""600735"", ""stkname"": ""新华锦"", ""stknew"": ""4.56"", ""roe"": ""3.06"", ""mgsy"": ""0.10"", ""score"": ""53"", ""height"": ""14"", ""c2exp12"": ""-1.7"", ""c2exp50"": ""-4.9"" }, { ""stkcode"": ""002442"", ""stkname"": ""龙星化工"", ""stknew"": ""5.59"", ""roe"": ""5.95"", ""mgsy"": ""0.23"", ""score"": ""22"", ""height"": ""16"", ""c2exp12"": ""0.6"", ""c2exp50"": ""2.7"" }, { ""stkcode"": ""000953"", ""stkname"": ""河化股份"", ""stknew"": ""5.33"", ""roe"": ""54.79"", ""mgsy"": ""0.22"", ""score"": ""111"", ""height"": ""18"", ""c2exp12"": ""0.0"", ""c2exp50"": ""-2.0"" }, { ""stkcode"": ""600493"", ""stkname"": ""凤竹纺织"", ""stknew"": ""5.24"", ""roe"": ""5.50"", ""mgsy"": ""0.23"", ""score"": ""24"", ""height"": ""18"", ""c2exp12"": ""1.6"", ""c2exp50"": ""0.3"" }, { ""stkcode"": ""002420"", ""stkname"": ""毅昌科技"", ""stknew"": ""5.89"", ""roe"": ""13.72"", ""mgsy"": ""0.18"", ""score"": ""83"", ""height"": ""19"", ""c2exp12"": ""1.7"", ""c2exp50"": ""1.8"" }, { ""stkcode"": ""603176"", ""stkname"": ""汇通集团"", ""stknew"": ""4.67"", ""roe"": ""5.83"", ""mgsy"": ""0.00"", ""score"": ""22"", ""height"": ""19"", ""c2exp12"": ""2.2"", ""c2exp50"": ""0.0"" }, { ""stkcode"": ""000788"", ""stkname"": ""北大医药"", ""stknew"": ""5.85"", ""roe"": ""8.48"", ""mgsy"": ""0.22"", ""score"": ""63"", ""height"": ""24"", ""c2exp12"": ""-0.5"", ""c2exp50"": ""-5.0"" }, { ""stkcode"": ""600854"", ""stkname"": ""春兰股份"", ""stknew"": ""4.53"", ""roe"": ""5.63"", ""mgsy"": ""0.26"", ""score"": ""25"", ""height"": ""26"", ""c2exp12"": ""0.2"", ""c2exp50"": ""-2.7"" }, { ""stkcode"": ""300272"", ""stkname"": ""开能健康"", ""stknew"": ""5.86"", ""roe"": ""6.72"", ""mgsy"": ""0.15"", ""score"": ""62"", ""height"": ""27"", ""c2exp12"": ""-0.7"", ""c2exp50"": ""-5.5"" }, { ""stkcode"": ""603878"", ""stkname"": ""武进不锈"", ""stknew"": ""5.35"", ""roe"": ""6.40"", ""mgsy"": ""0.30"", ""score"": ""64"", ""height"": ""28"", ""c2exp12"": ""-2.4"", ""c2exp50"": ""-7.5"" }, { ""stkcode"": ""000554"", ""stkname"": ""泰山石油"", ""stknew"": ""5.82"", ""roe"": ""5.35"", ""mgsy"": ""0.11"", ""score"": ""22"", ""height"": ""29"", ""c2exp12"": ""0.1"", ""c2exp50"": ""-1.6"" }, { ""stkcode"": ""600731"", ""stkname"": ""湖南海利"", ""stknew"": ""5.78"", ""roe"": ""6.48"", ""mgsy"": ""0.36"", ""score"": ""65"", ""height"": ""35"", ""c2exp12"": ""2.2"", ""c2exp50"": ""1.6"" }, { ""stkcode"": ""600232"", ""stkname"": ""金鹰股份"", ""stknew"": ""5.59"", ""roe"": ""5.14"", ""mgsy"": ""0.15"", ""score"": ""61"", ""height"": ""36"", ""c2exp12"": ""2.8"", ""c2exp50"": ""3.7"" }, { ""stkcode"": ""000153"", ""stkname"": ""丰原药业"", ""stknew"": ""5.93"", ""roe"": ""7.38"", ""mgsy"": ""0.32"", ""score"": ""65"", ""height"": ""39"", ""c2exp12"": ""0.2"", ""c2exp50"": ""-2.6"" } ]
貌似失败, dataList 为空