-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to get length of nested array? and extract all son keys? #33
Comments
Try this: For yaml1: WsjcppYaml yaml;
std::vector<std::string> vKeys = yaml["list"].keys(); For yaml2: WsjcppYaml yaml;
std::vector<std::string> vKeys = yaml.getCursor().keys(); More samples std::string sTest =
"'2021-10-08':\n"
" - code: '300209'\n"
" focusstr: ''\n"
" - code: '300047'\n"
" focusstr: ''\n"
" - code: '300970'\n"
" focusstr: ''\n"
" - code: '300288'\n"
" focusstr: ''\n"
"'2021-10-11':\n"
" - code: '300162'\n"
" focusstr: ''\n"
" - code: '300209'\n"
" focusstr: ''\n"
" - code: '300972'\n"
" focusstr: ''\n"
" - code: '300159'\n"
" focusstr: ''\n"
;
WsjcppYaml yaml2;
if (!yaml2.loadFromString("test", sTest, sError)) {
return -1;
}
WsjcppYamlCursor cur = yaml2.getCursor();
std::vector<std::string> vKeys = cur.keys();
for (int i = 0; i < vKeys.size(); i++) {
std::string sKey = vKeys[i];
int nCount = cur[sKey].size(); // musts array
for (int n = 0; n < nCount; n++) {
WsjcppYamlCursor el = cur[sKey][n];
std::cout
<< "code = " << el["code"].valInt()
<< "; focusstr = " << el["focusstr"].valStr()
<< ";"
<< std::endl;
}
} output:
|
I will try and post codes tomorrow. |
You welcome! |
I just tried to write a loop for almost one hour and haven't got a right output, then find your code. |
one more question:
It seems with indent or without indent are all valid, but wsjcpp-yaml need indent at the line start of array elements. |
@xkungfu sorry for a longtime.... so... python can parse this? |
yaml 1:
yaml2:
want to get how many date string in the yaml, and extract all the date strings to a array such as vector.
so I can loop the yaml by date keys.
thanks!
The text was updated successfully, but these errors were encountered: