如何在.net中反序列化这个json?

kqqjbcuj  于 2023-11-20  发布在  .NET
关注(0)|答案(3)|浏览(136)

我需要PreTaxCost和ResourceGroup从这个JSON中出来,以进行进一步的操作。

{
  "id": "subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/Query/00000000-0000-0000-0000-000000000000",
  "name": "55312978-ba1b-415c-9304-cfd9c43c0481",
  "type": "microsoft.costmanagement/Query",
  "properties": {
    "nextLink": null,
    "columns": [
      {
        "name": "PreTaxCost",
        "type": "Number"
      },
      {
        "name": "ResourceGroup",
        "type": "String"
      },
      {
        "name": "Currency",
        "type": "String"
      }
    ],
    "rows": [
      [0.009865586851323632, "Ict_StratAndPlan_GoldSprova_Prod_0", "USD"],
      [218.68795741935486, "Ict_StratAndPlan_GoldSprova_Prod_1", "USD"],
      [2.10333307059661, "ScreenSharingTest-peer1", "USD"],
      [0.14384913581657052, "Ssbciotelement01", "USD"]
    ]
  }
}

字符串

o4hqfura

o4hqfura1#

你可以使用这组类给你的json给予类型。

public class NeededData
{
   public double PreTaxCost{get; set;}
   public string ResourceGroup{get; set;}
}

public class Column
{
    public string name { get; set; }
    public string type { get; set; }
}

public class Properties
{
    public object nextLink { get; set; }
    public List<Column> columns { get; set; }
    public List<List<object>> rows { get; set; }
}

public class MainClass
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public Properties properties { get; set; }
}

字符串
在代码中保留这些类之后。

var JsonString = ""; //Keep Your Json String Here.
var filteredData = new List<NeededData>();
var data = JsonSerializer.Deserialize<MainClass>(jsonString);
foreach(var item in data.properties.rows){
    NeededData i = new NeededData();
    i.PreTaxCost = item[0];
    i.ResourceGroup = item[1];
    filteredData.Add(i);
}


上面的代码会从json字符串中过滤出你需要的数据。我假设列的数据的位置是准确的,因为它是在你提供的json字符串中。

5f0d552i

5f0d552i2#

你可以试试这个代码

var jObj = JObject.Parse(json);

int PreTaxCostInd = ((JArray)jObj["properties"]["columns"]).ToList().FindIndex(x => (string)x["name"] == "PreTaxCost");
int ResourceGroupInd = ((JArray)jObj["properties"]["columns"]).ToList().FindIndex(x => (string)x["name"] == "ResourceGroup");

// or if you are sure you can replace code above with
int PreTaxCostInd = 0;
int ResourceGroupInd = 1;

List<CostGroup> result = ((JArray)jObj["properties"]["rows"])
                            .Select(x => new CostGroup
                            {
                                PreTaxCost = (double)x[PreTaxCostInd],
                                ResourceGroup = (string)x[ResourceGroupInd]
                            }).ToList();

public class CostGroup
{
    public double PreTaxCost { get; set; }
    public string ResourceGroup { get; set; }
}

字符串

dzhpxtsq

dzhpxtsq3#

通常,解析json文档的最佳方法是创建一个对象结构,您可以将其转换为对象结构。我通常使用https://json2csharp.com/这样的工具。请确保选中“使用Pascal Case”和“使用JsonPropertyName(.NET Core)"。生成的类应该看起来像这样:

public class Column
{
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("type")]
    public string Type { get; set; }
}

public class Properties
{
    [JsonPropertyName("nextLink")]
    public object NextLink { get; set; }

    [JsonPropertyName("columns")]
    public List<Column> Columns { get; set; }

    [JsonPropertyName("rows")]
    public List<List<object>> Rows { get; set; }
}

public class Root
{
    [JsonPropertyName("id")]
    public string Id { get; set; }

    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("type")]
    public string Type { get; set; }

    [JsonPropertyName("properties")]
    public Properties Properties { get; set; }
}

字符串
在这里,你可以使用以下命令对json进行编译:

var deserializedData = JsonSerializer.Deserialize<Root>(jsonString);


然后用一些C#代码就可以很容易地获得属性。

相关问题