0

I'm making a friend list management tool for Steam using the web API. The following JSON is returned by the API:

{
"friendslist": {
    "friends": [
        {
            "steamid": "12345678",
            "relationship": "friend",
            "friend_since": 1290444866
        },
        {
            "steamid": "87654321",
            "relationship": "friend",
            "friend_since": 1421674782
        },
        {
            "steamid": "5287931",
            "relationship": "friend",
            "friend_since": 1418428351
        }
    ]
}

i try to parse it with this code:

public class Friendslist
{
    public IDictionary<int, IDictionary<int, Friend>> friends { get; set; }
}

public class Friend
{
    public uint steamid { get; set; }
    public string relationship { get; set; }
    public uint friend_since { get; set; }
}

        string jsonString;
        Friendslist jsonData;
        WebRequest wr;
        Stream objStream;
        wr = WebRequest.Create(sURL);
        objStream = wr.GetResponse().GetResponseStream();
        StreamReader objReader = new StreamReader(objStream);
        jsonString = objReader.ReadToEnd();
        jsonData = JsonConvert.DeserializeObject<Friendslist>(jsonString);

but jsonData is always null when i check it with the debugger.

user1365830
  • 171
  • 1
  • 11
  • The first thing I notice is that you're trying to deserialize to public IDictionary> but there is no 'int' in the jsonString object returned from the API. You need to make your object match the response more closely. Is a 'friendList' not just an IList? – darth_phoenixx Sep 09 '15 at 18:56

1 Answers1

3

Your model should be like

public class Friend
{
    public string steamid { get; set; }
    public string relationship { get; set; }
    public int friend_since { get; set; }
}

public class Friendslist
{
    public List<Friend> friends { get; set; }
}

public class RootObject
{
    public Friendslist friendslist { get; set; }
}

now you can deserialize as

var root = JsonConvert.DeserializeObject<RootObject>(jsonString);

See this useful site http://json2csharp.com/

Eser
  • 12,346
  • 1
  • 22
  • 32