Share via

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

Noah Aas 1,210 Reputation points
2025-04-01T14:03:59.4466667+00:00

How can I read out an integer?

Is this possible directly or via cast ?

hstring jsonstr = winrt::to_hstring(s);
int helpInt=0;
double helpDouble = 0;
if (JsonObject::TryParse(jsonstr, obj)) 
{
		Station = obj.GetNamedString(L"Station", L"Default").c_str();		

		Referencecode = obj.GetNamedString(L"Referencecode", L"Default").c_str();

		JsonValue dd = obj.GetNamedValue(L"OrderQuantity");
		helpDouble = obj.GetNamedNumber(L"OrderQuantity");
		helpInt = helpDouble;
		
		OrderQuantity = obj.GetNamedNumber(L"OrderQuantity");
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.

0 comments No comments

Answer accepted by question author

  1. Jack J Jun 25,306 Reputation points
    2025-04-01T14:51:22.3033333+00:00

    @Noah Aas, Welcome to Microsoft Q&A, you could use the following code to get int and double from jsonobject directly.

     JsonObject obj;
     hstring jsonstr = winrt::to_hstring(L"{\"MessageId\":\"0815-777\",\"Station\":\"X\",\"Number\":\"123.41\",\"Order\":\"0815-2025-03-20\"}");
     int helpInt = 0;
     double helpDouble = 0;
     if (JsonObject::TryParse(jsonstr, obj))
     {
         helpDouble = std::stod(obj.GetNamedString(L"Number").c_str());
         helpInt = static_cast<int>(helpDouble);
         std::wcout << L"Number: " << helpDouble << std::endl;
         std::wcout << L"Number: " << helpInt << std::endl;
       
     }
    

    Result:

    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?

    1 person found 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.