Read Array with winrt JsonObject using namespace Windows::Data::Json;

Noah Aas 985 Reputation points
2025-04-01T14:11:14.9433333+00:00

I'm filling an array, need to read it, and then fill it into a CStringArray. How can I achieve this?

As integer and as string values.


JsonArray arrPosition;
JsonArray arrIndex;


if (JsonObject::TryParse(jsonstr, obj))
{

	arrPosition = obj.GetNamedArray(L"PositionArray");

	for (int i = 0; i < arrPosition.Size(); i++)
	{
		JsonValue helpJsonValue = arrPosition.GetAt(0);
	
		//CString result = tempStr.c_str();
		//			PositionArray.Append((CA2W(arrPosition[i]).m_psz));
	}

{
  "Station": "X",
  "PositionArray": [
    1,
    2
  ],
  "IndexArray": [
    1,
    2
  ],
 "GroupIndexArray": [
    "White",
    "Black"
  ],
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2025-04-02T07:17:10.7033333+00:00

    Hi, @Noah Aas

    Example:

    	CStringArray PositionArray;
    	if (obj.HasKey (L"PositionArray")) {
    		JsonArray listArray = obj.GetNamedArray (L"PositionArray");
    		for (auto value : listArray) {
    			if (value.ValueType () == JsonValueType::Number) {
    			CString str;
    			#ifdef  UNICODE
    				str.Format (L"%.0f", value.GetNumber ());
    			#else
    				str.Format ("%.0f", value.GetNumber ());
    			#endif
    			PositionArray.Add (str);
    			}
    		}
    	}
    	//std::wcout << L"PositionArray:" << std::endl;
    	//for (int i = 0; i < PositionArray.GetSize (); ++i) {
    	//	std::wcout << L"- " << PositionArray[i].GetString () << std::endl;
    	//}
    	CStringArray GroupIndexArray;
    	if (obj.HasKey (L"GroupIndexArray")) {
    		JsonArray listArray = obj.GetNamedArray (L"GroupIndexArray");
    		for (auto value : listArray) {
    			if (value.ValueType () == JsonValueType::String) {
    				CString str (value.GetString ().c_str ());
    				GroupIndexArray.Add (str);
    			}
    		}
    	}
    	//std::wcout << L"GroupIndexArray:" << std::endl;
    	//for (int i = 0; i < GroupIndexArray.GetSize (); ++i) {
    	//	std::wcout << L"- " << GroupIndexArray[i].GetString () << std::endl;
    	//}
    

    Best regards,

    Minxin Yu


    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Darran Rowe 1,986 Reputation points
    2025-04-01T21:16:53.8433333+00:00

    JsonValue has a GetString member. Remember though that if you don't know the underlying type, the ValueType property identifies the type of the value and what GetXxx you have to call.

    1 person found this answer helpful.

Your answer

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