Skip to main content
edited body
Source Link
Volkan Paksoy
  • 7k
  • 5
  • 31
  • 41

Since there is no array inside the rgDescriptions but some randomly named looking properties I think you would need a custom JsonConverter. The following console application seesmseems to be working and displaying the market_hash_names correctly:

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("Sample.json");
        Desc result = JsonConvert.DeserializeObject<Desc>(json);
        result.rgDescriptions.ForEach(s => Console.WriteLine(s.market_hash_name));
        Console.ReadLine();
    }
}

public class Desc
{
    [JsonConverter(typeof(DescConverter))]
    public List<Something> rgDescriptions { get; set; }
}

public class Something
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string market_hash_name { get; set; }
}

class DescConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Something[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var descriptions = serializer.Deserialize<JObject>(reader);
        var result = new List<Something>();

        foreach (JProperty property in descriptions.Properties())
        {
            var something = property.Value.ToObject<Something>();
            result.Add(something);
        }

        return result;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Output:

Genuine Tactics Pin
Silver Operation Breakout Coin
Operation Phoenix Challenge Coin
Operation Bravo Challenge Coin

Since there is no array inside the rgDescriptions but some randomly named looking properties I think you would need a custom JsonConverter. The following console application seesm to be working and displaying the market_hash_names correctly:

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("Sample.json");
        Desc result = JsonConvert.DeserializeObject<Desc>(json);
        result.rgDescriptions.ForEach(s => Console.WriteLine(s.market_hash_name));
        Console.ReadLine();
    }
}

public class Desc
{
    [JsonConverter(typeof(DescConverter))]
    public List<Something> rgDescriptions { get; set; }
}

public class Something
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string market_hash_name { get; set; }
}

class DescConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Something[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var descriptions = serializer.Deserialize<JObject>(reader);
        var result = new List<Something>();

        foreach (JProperty property in descriptions.Properties())
        {
            var something = property.Value.ToObject<Something>();
            result.Add(something);
        }

        return result;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Output:

Genuine Tactics Pin
Silver Operation Breakout Coin
Operation Phoenix Challenge Coin
Operation Bravo Challenge Coin

Since there is no array inside the rgDescriptions but some randomly named looking properties I think you would need a custom JsonConverter. The following console application seems to be working and displaying the market_hash_names correctly:

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("Sample.json");
        Desc result = JsonConvert.DeserializeObject<Desc>(json);
        result.rgDescriptions.ForEach(s => Console.WriteLine(s.market_hash_name));
        Console.ReadLine();
    }
}

public class Desc
{
    [JsonConverter(typeof(DescConverter))]
    public List<Something> rgDescriptions { get; set; }
}

public class Something
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string market_hash_name { get; set; }
}

class DescConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Something[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var descriptions = serializer.Deserialize<JObject>(reader);
        var result = new List<Something>();

        foreach (JProperty property in descriptions.Properties())
        {
            var something = property.Value.ToObject<Something>();
            result.Add(something);
        }

        return result;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Output:

Genuine Tactics Pin
Silver Operation Breakout Coin
Operation Phoenix Challenge Coin
Operation Bravo Challenge Coin
Source Link
Volkan Paksoy
  • 7k
  • 5
  • 31
  • 41

Since there is no array inside the rgDescriptions but some randomly named looking properties I think you would need a custom JsonConverter. The following console application seesm to be working and displaying the market_hash_names correctly:

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("Sample.json");
        Desc result = JsonConvert.DeserializeObject<Desc>(json);
        result.rgDescriptions.ForEach(s => Console.WriteLine(s.market_hash_name));
        Console.ReadLine();
    }
}

public class Desc
{
    [JsonConverter(typeof(DescConverter))]
    public List<Something> rgDescriptions { get; set; }
}

public class Something
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string market_hash_name { get; set; }
}

class DescConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Something[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var descriptions = serializer.Deserialize<JObject>(reader);
        var result = new List<Something>();

        foreach (JProperty property in descriptions.Properties())
        {
            var something = property.Value.ToObject<Something>();
            result.Add(something);
        }

        return result;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Output:

Genuine Tactics Pin
Silver Operation Breakout Coin
Operation Phoenix Challenge Coin
Operation Bravo Challenge Coin