0

json - http://pastebin.com/Ss1YZsLK

I need to get market_hash_name values to list. I can receive first value so far:

using (WebClient webClient = new System.Net.WebClient()) {
    WebClient web = new WebClient();
    var json = web.DownloadString(">JSON LINK<");
    Desc data = JsonConvert.DeserializeObject<Desc>(json);

    Console.WriteLine(data.rgDescriptions.something.market_hash_name);
}

public class Desc {
    public Something rgDescriptions { get; set; }
}

public class Something {
    [JsonProperty("822948188_338584038")]
    public Name something { get; set; }
}

public class Name {
    public string market_hash_name { get; set; }
}

How can I get all if them?

1
  • Are the always 822948188_338584038, 778805950_597476706 etc' ? always those ones ? Commented Jul 12, 2015 at 17:20

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works but what if there is a few the same values like here pastebin.com/sLqVNdrK I want to know amount of them

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.