0
0
C Sharp (C#)programming~20 mins

Working with JSON files in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Master in C#
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this JSON serialization code?

Consider the following C# code that serializes an object to JSON. What will be printed?

C Sharp (C#)
using System;
using System.Text.Json;

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program {
    public static void Main() {
        var person = new Person { Name = "Alice", Age = 30 };
        string json = JsonSerializer.Serialize(person);
        Console.WriteLine(json);
    }
}
A{"Name":Alice,"Age":30}
B{"name":"Alice","age":30}
C{"Name":"Alice","Age":30}
D{Name:"Alice",Age:30}
Attempts:
2 left
💡 Hint

Remember that property names are serialized with exact casing by default.

Predict Output
intermediate
2:00remaining
What error does this JSON deserialization code produce?

What error will this code produce when trying to deserialize the JSON string?

C Sharp (C#)
using System;
using System.Text.Json;

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program {
    public static void Main() {
        string json = "{\"Name\":\"Bob\", \"Age\":\"thirty\"}";
        var person = JsonSerializer.Deserialize<Person>(json);
        Console.WriteLine(person.Name);
    }
}
ASystem.NullReferenceException
BSystem.Text.Json.JsonException
CSystem.FormatException
DNo error, prints 'Bob'
Attempts:
2 left
💡 Hint

Check the type of the 'Age' property and the JSON value type.

🔧 Debug
advanced
2:30remaining
Why does this JSON serialization ignore a property?

Given this class and serialization code, why is the 'Password' property missing in the JSON output?

C Sharp (C#)
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

public class User {
    public string Username { get; set; }
    [JsonIgnore]
    public string Password { get; set; }
}

public class Program {
    public static void Main() {
        var user = new User { Username = "user1", Password = "secret" };
        string json = JsonSerializer.Serialize(user);
        Console.WriteLine(json);
    }
}
APassword is private and cannot be serialized.
BThe serializer only serializes string properties named Username.
CPassword is null so it is not serialized.
DThe [JsonIgnore] attribute tells the serializer to skip the Password property.
Attempts:
2 left
💡 Hint

Look at the attributes above the Password property.

📝 Syntax
advanced
2:30remaining
Which option correctly deserializes JSON with a custom property name?

Given this JSON string and class, which code correctly deserializes the JSON?

{"full_name":"John Doe","Age":25}

Class:

public class Person {
    [JsonPropertyName("full_name")]
    public string Name { get; set; }
    public int Age { get; set; }
}
Avar person = JsonSerializer.Deserialize<Person>(json);
Bvar person = JsonSerializer.Deserialize<Person>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
Cvar person = JsonSerializer.Deserialize<Person>(json, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
Dvar person = JsonSerializer.Deserialize<Person>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
Attempts:
2 left
💡 Hint

The attribute [JsonPropertyName] maps JSON property names to class properties.

🚀 Application
expert
3:00remaining
How many items are in the dictionary after this JSON deserialization?

Given this JSON string and code, how many key-value pairs does the resulting dictionary contain?

{"a":1,"b":2,"c":3}

Code:

using System;
using System.Text.Json;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        string json = "{\"a\":1,\"b\":2,\"c\":3}";
        var dict = JsonSerializer.Deserialize>(json);
        Console.WriteLine(dict.Count);
    }
}
A3
B0
C1
DThrows an exception
Attempts:
2 left
💡 Hint

Count the number of properties in the JSON object.