2.2.7.3.3 JSON::TreeToVector
Description
Convert a Tree branch into vector. This is part of JSON class due to the fact that the tree here is made from a JSON string
Syntax
BOOL TreeToVector( TreeNode & tree, vectorbase & vv, LPCSTR lpcszTagName = NULL )
Parameters
- tree
- [input] this tree must be created by FromString
- vv
- [output] the resulting vector, string or numeric
- lpcszTagName
- [input] to get values from an array member with the name specified.
Return
FALSE return if Tree is not a valid format with _0, _1 type of tag names
Examples
EX1
//assume new project with an empty worksheet
void odata()
{
string strURL = "https://services.odata.org/TripPinRESTierService/People";
string result;
int err = okutil_http_get(strURL, &result);
if( err != 0 ) {
out_int("err = ", err);
return;
}
Tree tr;
if(!JSON.FromString(tr, result))
return;
//out_tree(tr);
vector<string> saFields = { "UserName", "LastName", "FirstName", "Gender", ", emails"};
//", emails" so that ", " will be used as separator for multiple values
Worksheet wks = Project.ActiveLayer();
wks.SetSize(-1, saFields.GetSize());
vector<string> vs;
for(int ii = 0; ii < saFields.GetSize(); ii++) {
JSON.TreeToVector(tr.value, vs, saFields[ii]);
Column ca = wks.Columns(ii);
ca.PutStringArray(vs);
}
}
Remark
For using lpcszTagName to get array member, that member itself might be another array, and in such case, Origin supports the following special cases:
1. To get value into a string array, then the member array values will be concatenated. You can even specify a separator by putting into the lpcszTagName. For example, see the odata sample code. The emails field may have one or more emails, and by using tag name as ", emails", then the ", " portion will be used as separator between the multiple emails.
2. To get numberic values, only the first will be obtained. This is useful for many JSON files that the member array holds only one value.
See Also
JSON::FromString
Header to Included
origin.h
|