-1

OK, please forgive me if I have the terminology wrong. I am attempting to do the following.

  1. Get JSON string from URL
  2. Parse said string into an object.

Normally for me this would be very easy. Usually the JSON objects are static names.

Where the problem arises is that the sub json objects vary person to person. They will also change if you change your inventory.

Now, I need to be able to put this into an object, and query it (not necessarily linq, but pull data at will). Does anyone have any information? I attempted at creating a dynamic object (as shown here Deserialize JSON into C# dynamic object?) but it did not work for me.

Would that link actually suit my purposes, and did I just implement it incorrectly?

Here is a dump of what the JSON looks like: http://chopapp.com/#ro46jfay

Thank you for all your help.

Community
  • 1
  • 1
stoXe
  • 313
  • 6
  • 13
  • 2
    You point to a question with plenty of answers, then say "it does not work for me" but don't show any code of what you tried. – crashmstr Aug 24 '15 at 17:36

2 Answers2

4

Newtonsoft Json.net supports creating dynamic objects from json.

var obj = JsonConvert.DeserializeObject<dynamic>(json); 
Oleh Nechytailo
  • 2,155
  • 17
  • 26
  • Right, but I have not successfully been able to access it's heirarchy and get the data. `code string name = item["rgDescriptions[0]"]; string name = item["rgDescriptions"];` – stoXe Aug 24 '15 at 17:02
  • `item["rgDescriptions[0]"];` - it doesn't make sense. Correct way will be something like `obj["rgDescriptions"][0]["name"]`. Imagine it like dictionaries of "string -> object" and navigate them like it's dictionaries and not some mythical json. – Oleh Nechytailo Aug 24 '15 at 17:12
  • But that doesnt let me iterate through. Please take a look at the sample JSON. The hierarchy is as follows: ROOT -> rgDescriptios -> RandomName how can I access all the random names and their children? – stoXe Aug 24 '15 at 17:30
2

In fact your data in not as dynamic as you think. All these ID's used as the name of a property can be deserialized to Dictionary<string,SomeObject>.

Although your json is more complex than the one in this question, same idea can be used easily.

So your Model can be as follows:

public class RGInventory
{
    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 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 MarketAction
{
    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<MarketAction> market_actions { get; set; }
    public List<Tag> tags { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public bool more { get; set; }
    public bool more_start { get; set; }
    public Dictionary<string, RGInventory> rgInventory { get; set; }
    public Dictionary<string, RGDescription> rgDescriptions { get; set; }
}

Now you can deserialize (using Json.Net) as

var obj = JsonConvert.DeserializeObject<RootObject>(json);

The hierarchy is as follows: ROOT -> rgDescriptios -> RandomName how can I access all the random names and their children?

A sample linq can be written as

var appids = obj.rgDescriptions.Select(x => x.Value.appid).ToList();

in a type-safe way.

Community
  • 1
  • 1
Eser
  • 12,346
  • 1
  • 22
  • 32
  • @DevinStokes Although, I don't understand your silence, here is my other answer http://stackoverflow.com/questions/32466119/c-sharp-get-json-value – Eser Sep 08 '15 at 20:36
  • looks like you used VS->Edit->PasteSpecial->Paste JSON to Classes :) – Unbreakable Mar 03 '19 at 02:08