Stavros S.
Stavros S.

Reputation: 117

Retrieve data from dynamic Json object

I'm trying to read the data from a dynamic object using Json.Net, i'm getting the json string using the webclient.downloadString.

    {
  "player1": {
  "id": 21426685,
  "name": "player1",
  "profileIconId": 508,
  "revisionDate": 1436353103000,
  "Level": 30
  },
  "Player2": {
  "id": 27864632,
  "name": "player2",
  "profileIconId": 508,
  "revisionDate": 1436444512000,
  "Level": 30
   }
    }

Each time the property name (player) will be different depending on the api call. I can deserialize the json string using Json.net and get the name of each property but i can't figure out how to get the data of each property since it won't have a constant name

    Dim jsonobj As Object = JsonConvert.DeserializeObject(ChampionsReply)
    For Each p As JProperty In jsonobj 
        ListBox1.Items.Add(p.Name)
    Next

Anyone could point me a way to make it work?

Upvotes: 0

Views: 176

Answers (1)

Jason W
Jason W

Reputation: 13179

Instead of dynamic, try a data structure using a Dictionary with string key (for dynamic key) and then a Player object.

var response = JsonConvert.DeserializeObject<Dictionary<string, Player>>(ChampionsReply);
foreach (var key in response.Keys)
    ListBox1.Items.Add(response[key].Name); // Get player by key

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

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("profileIconId")]
    public int ProfileIconId { get; set; }

    [JsonProperty("revisionDate")]
    public long RevisionDate { get; set; }

    [JsonProperty("level")]
    public int Level { get; set; }
}

And in VB.NET:

Dim response = JsonConvert.DeserializeObject(Of Dictionary(Of String, Player))(ChampionsReply)
For Each key As String In response.Keys
    response(key).Id
Next

Public Class Player
    <JsonProperty("id")> _
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set
            m_Id = Value
        End Set
    End Property
    Private m_Id As String

    <JsonProperty("name")> _
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String

    <JsonProperty("profileIconId")> _
    Public Property ProfileIconId() As Integer
        Get
            Return m_ProfileIconId
        End Get
        Set
            m_ProfileIconId = Value
        End Set
    End Property
    Private m_ProfileIconId As Integer

    <JsonProperty("revisionDate")> _
    Public Property RevisionDate() As Long
        Get
            Return m_RevisionDate
        End Get
        Set
            m_RevisionDate = Value
        End Set
    End Property
    Private m_RevisionDate As Long

    <JsonProperty("level")> _
    Public Property Level() As Integer
        Get
            Return m_Level
        End Get
        Set
            m_Level = Value
        End Set
    End Property
    Private m_Level As Integer
End Class

Great tool: http://converter.telerik.com/

Upvotes: 1

Related Questions