0

I am new to C#. I am trying to convert the JSON string into a list looks like this:

firstName   lastName  departmentID
John        Doe       AAA111
John2       Doe2      BBB222

Updated Post - based on David's answer (Thank you for that). Sorry for all the confusion I might have caused.

Here is the code:

public IActionResult JsonTest()
{

    var respJSON = @"
        [
              {
                ""firstName"": ""John"",
                ""lastName"": ""Doe"",
                ""departmentID"": { ""id"": ""AAA111"" },
              },
              {
                ""firstName"": ""John2"",
                ""lastName"": ""Doe2"",
                ""departmentID"": { ""id"": ""BBB222"" }
              }
          ]
        ";

    List<UserNameWithDept> user = JsonConvert.DeserializeObject<List<UserNameWithDept>>(respJSON);

    return Ok();
}


public class Identifier
{
    [JsonProperty("id")]
    public string Id { get; set; }
}

public class UserNameWithDept
{
    [JsonProperty("firstName")]
    public string FirstName { get; set; }

    [JsonProperty("lastName")]
    public string LastName { get; set; }

    [JsonProperty("departmentID")]
    public Identifier Department { get; set; }
}

I am getting the Department as an object, then value inside it. enter image description here

How can I get the list look this? Department has value, no an object.

Department  "AAA111"
FirstName   "John"
LastName    "Doe"
6
  • Sorry for the person who liked to give negative. I know I am new to C# and I wish I could be more clearer so let me know how I can improve. Commented Apr 16 at 18:14
  • 3
    "Next problem: you've said that departmentID is a string, but it's clearly not - it's an object containing an id property. Your model doesn't match your JSON" - Jon Skeet from your previous post. Commented Apr 16 at 18:29
  • In addition, your JSON is still invalid. You can't have a comma after the closing } from the departmentID property. Commented Apr 16 at 18:35
  • The departmentID is a string such as "AAA111", "BBB111". Yes, it is an object in JSON and I don't know how to map or get that value assigned to the model. Also, I tested that JSON with the command after closing } and it worked fine. Sorry, I am not sure that I understand. Commented Apr 16 at 18:38
  • Is it possible to modify the data to match the object structure, or modify the object structure to match the data? If not then what you'd be looking for is custom deserialization, and the JSON serializer you're using likely supports that. Commented Apr 16 at 18:38

1 Answer 1

3

Based on a comment on the question above:

I cannot modify JSON data. I am looking for away to modify the model or somehow get that departmentID value into that model.

Then simply modify the structure to match the data. This currently expects departmentID to be a string:

public class UserNameWithDept
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string departmentID { get; set; }
}

But is the data a string? Nope:

{
  "firstName": "John",
  "lastName": "Doe",
  "departmentID": { "id": "AAA111" }
}

The departmentID value is an object with a property (called id) and that is a string. So match that in your object structure:

public class Identifier
{
    public string id { get; set; }
}

public class UserNameWithDept
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Identifier departmentID { get; set; }
}

Then you can deserialize the data into that structure the same way you currently do.


Edit: This comment below is worth calling attention to. Naming conventions can be important as the code grows in complexity, as they help readers quickly understand the code. To that end, property names in C# should be capitalized.

You can make use of the JsonProperty attribute to indicate the corresponding property name in the data when it doesn't match what's in the structure. For example:

public class Identifier
{
    [JsonProperty("id")]
    public string Id { get; set; }
}

public class UserNameWithDept
{
    [JsonProperty("firstName")]
    public string FirstName { get; set; }

    [JsonProperty("lastName")]
    public string LastName { get; set; }

    [JsonProperty("departmentID")]
    public Identifier Department { get; set; }
}

Note also that the peoperty on UserNameWithDept was changed to simply Department. Consider the code which would reference this property. It would be redundant to indicate "id" twice to reference an identifier. This also suggests that the object used could perhaps someday hold more than just an identifier.

Naming gets very subjective very quickly, so these are just suggestions but ultimately it's up to you what names intuitively make sense in your application. And it's worth taking time to do that as poorly-named variables/properties/classes/etc. are a very common source of bugs.

Sign up to request clarification or add additional context in comments.

4 Comments

As a follow up, the C# property name doesn't need to match the JSON property name if you use the [JsonProperty()] attribute. By doing so, you can change the departmentID C# property name to something more meaningful.
@David, I really appreciated your response. It works, but I am getting the object for the Department instead. Is there a way I can get the value "AAA111" instead? I have updated my post to make it more clearer based on your suggestion. Thanks a lot for the help.
@Milacay: That's a change from the "modify the model" approach you'd asked for. You may be looking to write a custom JSON converter, such as here: stackoverflow.com/a/29988081/328193
@David, thanks again for your time and suggestion for Naming Convention. I will play around with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.