JavaEar 专注于收集分享传播有价值的技术资料

Parsing json in C# without knowing indexes

问题描述


英文原文

I want to parse this piece of JSON in C# with JSON.NET, but I don't know how to go about it.

Json:

{
   "success":true,
   "rgInventory":{
       "967633758":{
          "id":"967633758",
          "classid":"23973033",
          "instanceid":"11040671",
          "amount":"1",
          "pos":1
       },
      "302756826":{
          "id":"302756826",
          "classid":"15",
          "instanceid":"11041143",
          "amount":"1",
          "pos":2
      },...
   }
}

Full Json: http://steamcommunity.com/id/jessecar/inventory/json/440/2/?trading=1

I need to get the elements of each "rgInventory" child, but I can't make a class for it because the item names are always changing.

I tried using this piece of code, but I always get an exception.

dynamic jsObject = JsonConvert.DeserializeObject(jsonString);

Console.WriteLine("Status: "+jsObject["success"]); //This works fine

foreach(var i in jsObject["rgInventory"]){
    Console.WriteLine("Item ID: "+i["id"]); //This gives an exception
}

Exception:

Unhandled Exception: System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JProperty.


中文翻译

I want to parse this piece of JSON in C# with JSON.NET, but I don't know how to go about it.

Json:

{
   "success":true,
   "rgInventory":{
       "967633758":{
          "id":"967633758",
          "classid":"23973033",
          "instanceid":"11040671",
          "amount":"1",
          "pos":1
       },
      "302756826":{
          "id":"302756826",
          "classid":"15",
          "instanceid":"11041143",
          "amount":"1",
          "pos":2
      },...
   }
}

Full Json: http://steamcommunity.com/id/jessecar/inventory/json/440/2/?trading=1

I need to get the elements of each "rgInventory" child, but I can't make a class for it because the item names are always changing.

I tried using this piece of code, but I always get an exception.

dynamic jsObject = JsonConvert.DeserializeObject(jsonString);

Console.WriteLine("Status: "+jsObject["success"]); //This works fine

foreach(var i in jsObject["rgInventory"]){
    Console.WriteLine("Item ID: "+i["id"]); //This gives an exception
}

Exception:

Unhandled Exception: System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JProperty.

I want to parse this piece of JSON in C# with JSON.NET, but I don't know how to go about it.

Json:

{
   "success":true,
   "rgInventory":{
       "967633758":{
          "id":"967633758",
          "classid":"23973033",
          "instanceid":"11040671",
          "amount":"1",
          "pos":1
       },
      "302756826":{
          "id":"302756826",
          "classid":"15",
          "instanceid":"11041143",
          "amount":"1",
          "pos":2
      },...
   }
}

Full Json: http://steamcommunity.com/id/jessecar/inventory/json/440/2/?trading=1

I need to get the elements of each "rgInventory" child, but I can't make a class for it because the item names are always changing.

I tried using this piece of code, but I always get an exception.

dynamic jsObject = JsonConvert.DeserializeObject(jsonString);

Console.WriteLine("Status: "+jsObject["success"]); //This works fine

foreach(var i in jsObject["rgInventory"]){
    Console.WriteLine("Item ID: "+i["id"]); //This gives an exception
}

Exception:

Unhandled Exception: System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JProperty.

I want to parse this piece of JSON in C# with JSON.NET, but I don't know how to go about it.

Json:

{
   "success":true,
   "rgInventory":{
       "967633758":{
          "id":"967633758",
          "classid":"23973033",
          "instanceid":"11040671",
          "amount":"1",
          "pos":1
       },
      "302756826":{
          "id":"302756826",
          "classid":"15",
          "instanceid":"11041143",
          "amount":"1",
          "pos":2
      },...
   }
}

Full Json: http://steamcommunity.com/id/jessecar/inventory/json/440/2/?trading=1

I need to get the elements of each "rgInventory" child, but I can't make a class for it because the item names are always changing.

I tried using this piece of code, but I always get an exception.

dynamic jsObject = JsonConvert.DeserializeObject(jsonString);

Console.WriteLine("Status: "+jsObject["success"]); //This works fine

foreach(var i in jsObject["rgInventory"]){
    Console.WriteLine("Item ID: "+i["id"]); //This gives an exception
}

Exception:

Unhandled Exception: System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JProperty.

1个回答

    最佳答案

  1. 英文原文

    This should work.

    var jObj = (JObject)JsonConvert.DeserializeObject(json);
    foreach(var child in jObj["rgInventory"].Children())
    {
        Console.WriteLine("Item ID: {0}", child.First()["id"]);
    }
    

    Additionally, using dynamic keyword can make your code more readible:

    dynamic jObj = JsonConvert.DeserializeObject(json);
    Console.WriteLine("Status: " + jObj.success); 
    foreach(var child in jObj.rgInventory.Children())
    {
        Console.WriteLine("Item ID: {0}", child.First.id);
    }
    

    中文翻译

    This should work.

    var jObj = (JObject)JsonConvert.DeserializeObject(json);
    foreach(var child in jObj["rgInventory"].Children())
    {
        Console.WriteLine("Item ID: {0}", child.First()["id"]);
    }
    

    Additionally, using dynamic keyword can make your code more readible:

    dynamic jObj = JsonConvert.DeserializeObject(json);
    Console.WriteLine("Status: " + jObj.success); 
    foreach(var child in jObj.rgInventory.Children())
    {
        Console.WriteLine("Item ID: {0}", child.First.id);
    }