0

I have a JSON list of players for a game. I need the program to be able to read the JSON file and give me the list of players. Then I can use this list of players to get each player's name and who they are following.

{
"Version": "1",
"Users": [
 {
  "UserName": "CoolDude",
  "Following": [ "SniperElitez", "IamAwesome" ]
},
{
  "UserName": "GamerChick",
  "Following": [ "IamAwesome", "NoWayBro", "WowWhoMe", "SniperElitez", "SurfingIsFun", "NowUTry" ]
},
{
  "UserName": "SurfingIsFun",
  "Following": [ "WowWhoMe" ]
},
{
  "UserName": "IamAwesome",
  "Following": [ "GamerChick", "NoWayBro" ]
},
{
  "UserName": "NowUTry",
  "Following": [ "GamerChick", "SniperElitez" ]
},
{
  "UserName": "SniperElitez",
  "Following": [ "SurfingIsFun", "IamAwesome" ]
},
{
  "UserName": "WowWhoMe",
  "Following": [ "NoWayBro", "GamerChick", "SniperElitez" ]
},
{
  "UserName": "NoWayBro",
  "Following": [ "GamerChick", "IamAwesome", "SniperElitez" ]
}
]
}

Here is the code I'm trying to use currently Player Class

class Player
{
public string UserName { get; set;}
public List<string> Following { get; set;}
}

PlayerList Class

class PlayerList
{
    public List<Player> pList { get; set; }

}

Main

{

string json = File.ReadAllText("friends.json");
// this gives me a playerList object
PlayerList playerList = JsonConvert.DeserializeObject<PlayerList>(json);

    }

Supposedly the PlayerList object is not null but the List is empty. If anyone can tell me how to turn the JSON into the PlayerList object and then from there, get player's variables like UserName, I would greatly appreciate it!

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
KT_Grizzly
  • 11
  • 1
  • 1
  • 2

3 Answers3

4

Visual Studio has a nice feature for parsing XML / JSON... namely, if you copy an XML or a JSON to your clipboard then you can use Edit -> Paste Special -> Paste JSON As Classes command and have VS automatically generate valid model classes.

In case of your JSON this is the output my VS generated:

public class Rootobject
{
    public string Version { get; set; }
    public User[] Users { get; set; }
}

public class User
{
    public string UserName { get; set; }
    public string[] Following { get; set; }
}

Try that.

Also, you're using JsonConvert, which appears to be something by Newtonsoft. I have no idea if it's a good library or not, but there ARE .NET classes which are capable of deserializing JSON. The linked question offers one method. Another is to use a DataContractJsonSerializer, albeit I think the model classes would need to have the [DataContract] and [DataMember] attributes added to them to work with that.

Community
  • 1
  • 1
MBender
  • 5,395
  • 1
  • 42
  • 69
  • Newtonsoft is so good that Microsoft started including it in ASP.Net in preference to their own libraries. – Martin Brown Apr 27 '16 at 17:09
  • @MartinBrown In that case don't panic, and carry on. ;) I personally try to avoid using 3rd party libraries if possible, if only because having to attach them to projects to make sure everything compiles can sometimes be a hassle. That said, nugget helps immensely... – MBender Apr 27 '16 at 17:11
1

The reason is that in the json doc the attribute is called "Users" and in your code you have called it "pList".

There are two solutions to this:

  1. Rename PlayerList.pList to PlayerList.Users

  2. Add a JsonProperty attribute on the property to set its name like this:

    class PlayerList
    {
    [JsonProperty(PropertyName = "Users")]
    public List pList { get; set; }
    }

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
0

I was able to get this working by following what was stated in the question at the following link:

How to deserialize an JObject to .NET object

Here is my code:

        var playerList = JsonConvert.DeserializeObject(json);
        JObject jObject = ((JObject)playerList);
        PlayerList users = jObject.ToObject<PlayerList>();

I also renamed the pList to Users on the PlayerList Class.

Community
  • 1
  • 1
Mike
  • 550
  • 2
  • 16