Share via

WinRT : JsonArray by reference

Noah Aas 1,210 Reputation points
2025-03-20T18:28:38.9533333+00:00

Hello,

Is there a way to use the array once for all arrays

Should be enough one array for all arrays. Works not. Looks like per value instead by reference.

Idea: JsonArray arrOutput; And then clear for each new array.

    struct JsonArray;
    struct JsonError;
    struct JsonObject;
    struct JsonValue;	

JsonObject finalOutput;
JsonArray arrOutputSerial;
JsonArray arrOutputSerialIndex;
JsonArray arrOutputVariableKey;
JsonArray arrOutputVariableValue;
arrOutputVariableKey.Clear();

for (int i = 0; i < VariableKeyArray.GetSize(); i++)
{
	arrOutputVariableKey.Append(JsonValue::CreateStringValue(CA2W(VariableKeyArray[i]).m_psz));
}

finalOutput.Insert(L"VariableKey", arrOutputVariableKey);

arrOutputVariableValue.Clear();

for (int i = 0; i < VariableValueArray.GetSize(); i++)
{
	arrOutputVariableValue.Append(JsonValue::CreateStringValue(CA2W(VariableValueArray[i]).m_psz));
}
finalOutput.Insert(L"VariableValue", arrOutputVariableValue);


Developer technologies | C++
Developer technologies | C++

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.


Answer accepted by question author

Jack J Jun 25,306 Reputation points
2025-03-21T02:49:12.7066667+00:00

@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:

User's image

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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.