0

I have issue with deserialize JSON for next classes:

public class Players:List<Player>
{
}

public class Player
{
    public Player()
    {
       PlayerTeam = new Team();
    } 

    public int PlayerId { get; set; }
    public Team PlayerTeam { get; set; } 
}

public class Team
{
    public int TeamId { get; set; }
    public string Name { get; set; }
}

Result for Team object is always TeamId:0 and Name:null. DataContractJsonSerializer working for this example good, but Json.Net no. why? what I need change in my code, because I must to use Json.Net in my program?

5
  • what's your sample json? Commented Mar 6, 2016 at 14:31
  • { "PlayerId": 3, "PlayerTeam": { "TeamId": 20, "Name": "ABC" } } Commented Mar 6, 2016 at 14:42
  • I can't reproduce your issue var json = "{ \"PlayerId\": 3, \"PlayerTeam\": { \"TeamId\": 20, \"Name\": \"ABC\" } }"; var obj = JsonConvert.DeserializeObject<Player>(json); Console.WriteLine($"{obj.PlayerId}, {obj.PlayerTeam.TeamId} - {obj.PlayerTeam.Name}"); Please, provide Minimal Complete Verifiable Example. Commented Mar 6, 2016 at 15:11
  • please try next: var json = "[{ \"PlayerId\": 3, \"PlayerTeam\": { \"TeamId\": 20, \"Name\": \"ABC\" } },{ \"PlayerId\": 4, \"PlayerTeam\": { \"TeamId\": 21, \"Name\": \"ABCD\" } }]"; var obj = JsonConvert.DeserializeObject<Players>(json); Console.WriteLine($"{obj[0].PlayerId}, {obj[0].PlayerTeam.TeamId} - {obj[0].PlayerTeam.Name}"); Commented Mar 6, 2016 at 15:14
  • Seems to work fine for me. Commented Mar 6, 2016 at 15:27

2 Answers 2

1

This work well :

 static void Main(string[] args)
        {
            Player player = new Player
            {
                PlayerTeam = new Team { Name ="ABC", TeamId= 20 }, PlayerId = 3
            };


            string json = JsonConvert.SerializeObject(player, Formatting.Indented);

            Console.WriteLine(json);

            Player player_Des = JsonConvert.DeserializeObject<Player>(json);

            Console.WriteLine(player_Des.PlayerTeam.Name);

            Console.ReadLine();
        }

I prefer this Player Class

public class Player
{
    public Player()
    {
    } 

    public int PlayerId { get; set; }
    public Team PlayerTeam { get; set; } 
}

hope it helps

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

5 Comments

thank you. yes. this example is working, but in my program where classes have other structure this is not working
in my program I have a structure like this: public Players:List<Player> { } public class Player { public Player() { PlayerTeam = new Team(); } public int PlayerId { get; set; } public Team PlayerTeam { get; set; } } public class Team { public int TeamId { get; set; } public string Name { get; set; } }
let me get a look inside
I can not. please try this code: var json = "[{ \"PlayerId\": 3, \"PlayerTeam\": { \"TeamId\": 20, \"Name\": \"ABC\" } },{ \"PlayerId\": 4, \"PlayerTeam\": { \"TeamId\": 21, \"Name\": \"ABCD\" } }]"; var obj = JsonConvert.DeserializeObject<Players>(json); Console.WriteLine($"{obj[0].PlayerId}, {obj[0].PlayerTeam.TeamId} - {obj[0].PlayerTeam.Name}");
It work fine for me , i edited your question. I just not used Console.WriteLine($"{obj[0].PlayerId}, {obj[0].PlayerTeam.TeamId} - {obj[0].PlayerTeam.Name}"); instead i used : Console.WriteLine(obj[1].PlayerId); Console.WriteLine(obj[1].PlayerTeam.TeamId); Console.WriteLine(obj[1].PlayerTeam.Name);
1

Still no. I can't reproduce the issue.

public class Players : List<Player>
{
}

public class Player
{
    public Player()
    {
        PlayerTeam = new Team();
    }

    public int PlayerId { get; set; }
    public Team PlayerTeam { get; set; }
}

public class Team
{
    public int TeamId { get; set; }
    public string Name { get; set; }
}

...

var json = "[{ \"PlayerId\": 3, \"PlayerTeam\": { \"TeamId\": 20, \"Name\": \"ABC\" } },{ \"PlayerId\": 4, \"PlayerTeam\": { \"TeamId\": 21, \"Name\": \"ABCD\" } }]";
var players = JsonConvert.DeserializeObject<Players>(json);

foreach (var player in players)
{
    Console.WriteLine($"{player.PlayerId}, {player.PlayerTeam.TeamId} - {player.PlayerTeam.Name}");
}

Prints:

3, 20 - ABC

4, 21 - ABCD

Perhaps you use some old version of Newtonsoft.Json?

4 Comments

I'm using version 4.5.0.0
@MarianMelnychuk Can you reproduce the issue with the code in my post?
@MarianMelnychuk 4.5.0.0? If neither of us checks something wrong, then it is a very old version - the current one is 8.0.2.
just use this shell : Install-Package Newtonsoft.Json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.