Hi,
To use WinRT API, make sure templates is installed.
The template projects have already configured the settings required for C++/WinRt.
For example:
#include"pch.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Data.Json.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <iostream>
using namespace winrt;
using namespace Windows::Data::Json;
int main () {
init_apartment ();
JsonObject obj;
obj.Insert (L"message", JsonValue::CreateStringValue (L"Hello from C++/WinRT!"));
obj.Insert (L"year", JsonValue::CreateNumberValue (2025)); //parameter: double input
JsonArray arr;
arr.Append (JsonValue::CreateStringValue (L"one"));
arr.Append (JsonValue::CreateStringValue (L"two"));
arr.Append (JsonValue::CreateStringValue (L"three"));
obj.Insert (L"list", arr);
hstring jsonStr = obj.Stringify (); //jsonStr = L"{\"message\":\"Hello from C++/WinRT!\",\"year\":2025,\"list\":[\"one\",\"two\",\"three\"]}"
//string data
std::wcout << L"Serialized JSON: " << jsonStr.c_str () << std::endl;
std::string tempStr(jsonStr.begin (), jsonStr.end ());
std::cout << "Serialized JSON: " << tempStr.c_str () << std::endl;
return 0;
}
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.