Error on Unit 9 'Build .NET applications with C#' makes you write the wrong statement

Maria Soledad Fernandez Oporto 20 Reputation points
2025-01-11T01:27:07.2866667+00:00

According to the instruction when trying to deserialize a JSON object it indicates to write the following code
// Parse the contents as JSON
SalesData? data = JsonSerializer.Deserialize<SalesData?>(salesJson);

However this will fail in the local execution since the correct line is
SalesData? data = JsonConvert.DeserializeObject<SalesData?>(salesJson);

It forces you to copy all or review line by line what is wrong with your code and the section 'Got stuck?'

Can we fix that to avoid confusion?

This question is related to the following Learning Module

Azure Azure Training
0 comments No comments
{count} votes

Accepted answer
  1. Pradeep M 9,765 Reputation points Microsoft External Staff Volunteer Moderator
    2025-01-13T05:53:39.49+00:00

    Hi Maria Soledad Fernandez Oporto,

    Thank you for reaching out to Microsoft Q & A forum. 

    The exercise instructs the use of System.Text.Json for deserialization, but the provided code uses JsonConvert.DeserializeObject, which is part of the Newtonsoft.Json library. The correct approach for System.Text.Json should be:

    
    SalesData? data = JsonSerializer.Deserialize<SalesData?>(salesJson);
    
    

    This discrepancy arises from the exercise referencing methods from Newtonsoft.Json. Please modify the deserialization code to use JsonSerializer from System.Text.Json to align with the exercise instructions. 

    We will forward this issue to our internal team for an update. An updated version of the code will be provided in the future to prevent any confusion and ensure a smoother experience. 

    Please feel free to contact us if you have any additional questions.     

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.  


0 additional answers

Sort by: Most 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.