A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
@Noah Aas, Welcome to Microsoft Q&A, when you use arrOutputVariableKey.Clear();, it clears all references to that JsonArray. So, if you're trying to reuse the same array object (arrOutputVariableKey) for multiple inserts, it won't work as expected.
I recommend that you use a New JsonArray instance for each entry.
std::vector<std::wstring> VariableKeyArray = { L"Key1", L"Key2", L"Key3" };
std::vector<std::wstring> VariableValueArray = { L"Value1", L"Value2", L"Value3" };
JsonObject finalOutput;
// Create and fill JsonArray for "VariableKey"
JsonArray arrOutputVariableKey;
for (const auto& key : VariableKeyArray) {
arrOutputVariableKey.Append(JsonValue::CreateStringValue(key));
}
finalOutput.Insert(L"VariableKey", arrOutputVariableKey);
/* arrOutputVariableKey.Clear();*/
// Create and fill JsonArray for "VariableValue"
JsonArray arrOutputVariableValue;
for (const auto& value : VariableValueArray) {
arrOutputVariableValue.Append(JsonValue::CreateStringValue(value));
}
finalOutput.Insert(L"VariableValue", arrOutputVariableValue);
//// Print final JSON output
std::wcout << L"Final JSON Output:\n" << finalOutput.Stringify().c_str() << std::endl;
Output:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.