Home > other >  Parse (Deserialize) JSON with dynamic keys (C#)
Parse (Deserialize) JSON with dynamic keys (C#)

Time:01-15

I want to parse the JSON on the bottom. Till now I always have the static key for the variables, but in this example, the keys are always changing. Here the "58e4d88ba9029" could have any name. Since "58e4d88ba9029" is a list, how can I parse it?

{
  "title": "weapon",
  "key": "gun",
  "icon": "ricon-AK47",
  "objects": [
    {
      "id": "5a0ab761719a9fee422f3db1",
      "category": "gun",
      "name": "AK47",
      "dimension": "3d",
      "sets": {
        "58e4d88ba9029": {
          "sid": "58e4d88ba9639",
          "set_id": "592e8adfhdf19a9fab407a0892"
        },
        "58e4d88ba1010": {
          "sid": "58e4d8ahs029",
          "set_id": "592e8ada719a9fab407asf"
        },
        "58e4d88ba5689": {
          "sid": "58e4d88mm9029",
          "set_id": "592e8ada719a9fab407a0333"
        }
      }
    }
  ]
}
public class JsonParser
{
    public string key;
    public List<Objects> objects;
}

[Serializable]
public class Objects
{
    public string name;
    public List<Sets> sets;
    public string id;
}

[Serializable]
public class Sets
{
   ? 
}

I have tried, but still not getting the result:

var deserialized = JsonConvert.DeserializeObject<JObject>(json);
var dictionary = new Dictionary<string, string>();
foreach (JProperty item in deserialized["sets"])
dictionary.Add(item.Name, item.Value.ToString());

Also, I have tried this: root variable in debugger

CodePudding user response:

I hope my solution below help you

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string json = @"{
  'title': 'weapon',
  'key': 'gun',
  'icon': 'ricon-AK47',
  'objects': [
    {
      'id': '5a0ab761719a9fee422f3db1',
      'category': 'gun',
      'name': 'AK47',
      'dimension': '3d',
      'sets': {
        '58e4d88ba9029': {
          'sid': '58e4d88ba9639',
          'set_id': '592e8adfhdf19a9fab407a0892'
        },
        '58e4d88ba1010': {
          'sid': '58e4d8ahs029',
          'set_id': '592e8ada719a9fab407asf'
        },
        '58e4d88ba5689': {
          'sid': '58e4d88mm9029',
          'set_id': '592e8ada719a9fab407a0333'
        }
      }
    }
  ]
}";

JObject keyValuePairs = JObject.Parse(json);
var teste = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
if (teste != null && teste.ContainsKey("objects"))
{
    var objects = keyValuePairs.SelectToken("objects[0]").Children<JProperty>();
    var setsProperty = objects.Where(p=>p.Name == "sets").ToList();
    var setsProperties = setsProperty.Children<JProperty>().ToList();
    List<Sets> setsList = new List<Sets>();
    foreach (var setObject in setsProperties)
    {
        JObject jObject = JObject.Parse(setObject.ToString());
        foreach (var setChild in jObject.Values())
        {
            Sets sets = new Sets
            {
                sid = setChild.Value<string>("sid"),
                set_id = setChild.Value<string>("set_id")
            };
            setsList.Add(sets);
        }
    }
}

[Serializable]
public class Objects
{
    public string name;
    public List<Sets> sets;
    public string id;
}
[Serializable]
public class Sets
{
    public string sid { get; set; }
    public string set_id { get; set; }  
}
  •  Tags:  
  • Related