Search code examples
c#jsonjson-deserialization

Json string deserialization C#


I have this json and i want to deserialize it so I can get each object's value for example:

"icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpouLWzKjhzw8zFdC5K092kl5SClMj3PLXFhGpC_Pp8j-3I4IG7i1Hn_UI-Nmj3ItDGe1BoN1mCr1G4xL_vhMS8tcmcn3JhuihwsHvbzQv3309k3tBw8A",

The problem is I can make the class(es) that I need so I can deserialize the json because the json string has nested objects.


Solution

  • I used json2csharp to help me generate classes. After some merging and cleaning up, this is what I got:

    public class InventoryItem
    {
        public string id { get; set; }
        public string classid { get; set; }
        public string instanceid { get; set; }
        public string amount { get; set; }
        public int pos { get; set; }
    }
    
    public class AppData
    {
        public string def_index { get; set; }
        public int? is_itemset_name { get; set; }
        public int? limited { get; set; }
    }
    
    public class Description
    {
        public string type { get; set; }
        public string value { get; set; }
        public string color { get; set; }
        public AppData app_data { get; set; }
    }
    
    public class Action
    {
        public string name { get; set; }
        public string link { get; set; }
    }
    
    public class Tag
    {
        public string internal_name { get; set; }
        public string name { get; set; }
        public string category { get; set; }
        public string category_name { get; set; }
        public string color { get; set; }
    }
    
    public class RgDescription
    {
        public string appid { get; set; }
        public string classid { get; set; }
        public string instanceid { get; set; }
        public string icon_url { get; set; }
        public string icon_url_large { get; set; }
        public string icon_drag_url { get; set; }
        public string name { get; set; }
        public string market_hash_name { get; set; }
        public string market_name { get; set; }
        public string name_color { get; set; }
        public string background_color { get; set; }
        public string type { get; set; }
        public int tradable { get; set; }
        public int marketable { get; set; }
        public int commodity { get; set; }
        public string market_tradable_restriction { get; set; }
        public List<Description> descriptions { get; set; }
        public List<Action> actions { get; set; }
        public List<Action> market_actions { get; set; }
        public List<Tag> tags { get; set; }
    }
    
    public class RootObject
    {
        public bool success { get; set; }
        public IDictionary<string, InventoryItem> rgInventory { get; set; }
        public List<object> rgCurrency { get; set; }
        public IDictionary<string, RgDescription> rgDescriptions { get; set; }
        public bool more { get; set; }
        public bool more_start { get; set; }
    }
    

    These appear to work correctly, you can deserialize and serialize with code like this:

    var obj = JsonConvert.DeserializeObject<RootObject>(oldString);
    Console.WriteLine(obj.rgDescriptions["310776560_302028390"].icon_url); // e.g.
    var newString = JsonConvert.SerializeObject(obj,
         new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
    // null value handling is optional, the above makes it a little more like the source string