Bird
Raised Fist0

Which of the following code snippets correctly deserializes a JSON string into a C# object of type Person using System.Text.Json?

easy📝 Syntax Q3 of Q15
C Sharp (C#) - File IO
Which of the following code snippets correctly deserializes a JSON string into a C# object of type Person using System.Text.Json?
Avar person = JsonSerializer.Serialize<Person>(jsonString);
Bvar person = JsonConvert.DeserializeObject<Person>(jsonString);
Cvar person = JsonSerializer.Deserialize<Person>(jsonString);
Dvar person = JsonSerializer.Parse<Person>(jsonString);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct deserialization method

    JsonSerializer.Deserialize<T>(string) is the method from System.Text.Json to convert JSON string to an object.
  2. Step 2: Check other options

    JsonConvert.DeserializeObject<T> belongs to Newtonsoft.Json, not System.Text.Json. Serialize is for converting objects to JSON, not the reverse. Parse is not a valid method in JsonSerializer.
  3. Final Answer:

    var person = JsonSerializer.Deserialize<Person>(jsonString); -> Option C
  4. Quick Check:

    Use Deserialize<T> from System.Text.Json [OK]
Quick Trick: Use JsonSerializer.Deserialize(json) for deserialization [OK]
Common Mistakes:
MISTAKES
  • Confusing Newtonsoft.Json and System.Text.Json methods
  • Using Serialize instead of Deserialize
  • Using non-existent methods like Parse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes