使用Json.net在C#中反序列化复杂的Json

使用Json.net在C#中反序列化复杂的Json,c#,.net,json,json.net,deserialization,C#,.net,Json,Json.net,Deserialization,我有一些复杂的JSon,我正试图将其解析成有意义的东西。我正在尝试使用C#反序列化,但无法获得所需的值。我需要的是每个ColData节点的值,除了“摘要”部分中的值。我能够使用反序列化到一个对象中,但是我被困在那里了 string pandltext = @"{ "Header": { "Time": "2017-08-24T08:32:58-07:00", "ReportName": "ProfitAndLoss", "ReportBasis": "Accr

我有一些复杂的JSon,我正试图将其解析成有意义的东西。我正在尝试使用C#反序列化,但无法获得所需的值。我需要的是每个ColData节点的值,除了“摘要”部分中的值。我能够使用反序列化到一个对象中,但是我被困在那里了

    string pandltext = @"{
  "Header": {
    "Time": "2017-08-24T08:32:58-07:00",
    "ReportName": "ProfitAndLoss",
    "ReportBasis": "Accrual",
    "StartPeriod": "2017-06-01",
    "EndPeriod": "2017-06-30",
    "SummarizeColumnsBy": "Total",
    "Currency": "USD",
    "Option": [
      {
        "Name": "AccountingStandard",
        "Value": "GAAP"
      },
      {
        "Name": "NoReportData",
        "Value": "false"
      }
    ]
  },
  "Columns": {
    "Column": [
      {
        "ColTitle": "",
        "ColType": "Account",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "account"
          }
        ]
      },
      {
        "ColTitle": "Total",
        "ColType": "Money",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "total"
          }
        ]
      }
    ]
  },
  "Rows": {
    "Row": [
      {
        "Header": {
          "ColData": [
            {
              "value": "Income"
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "ColData": [
                {
                  "value": "Design income",
                  "id": "82"
                },
                {
                  "value": "975.00"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "Discounts given",
                  "id": "86"
                },
                {
                  "value": "-30.50"
                }
              ],
              "type": "Data"
            },
            {
              "Header": {
                "ColData": [
                  {
                    "value": "Landscaping Services",
                    "id": "45"
                  },
                  {
                    "value": "360.00"
                  }
                ]
              },
              "Rows": {
                "Row": [
                  {
                    "Header": {
                      "ColData": [
                        {
                          "value": "Job Materials",
                          "id": "46"
                        },
                        {
                          "value": ""
                        }
                      ]
                    },
                    "Rows": {
                      "Row": [
                        {
                          "ColData": [
                            {
                              "value": "Fountains and Garden Lighting",
                              "id": "48"
                            },
                            {
                              "value": "550.00"
                            }
                          ],
                          "type": "Data"
                        },
                        {
                          "ColData": [
                            {
                              "value": "Plants and Soil",
                              "id": "49"
                            },
                            {
                              "value": "1820.72"
                            }
                          ],
                          "type": "Data"
                        },
                        {
                          "ColData": [
                            {
                              "value": "Sprinklers and Drip Systems",
                              "id": "50"
                            },
                            {
                              "value": "30.00"
                            }
                          ],
                          "type": "Data"
                        }
                      ]
                    },
                    "Summary": {
                      "ColData": [
                        {
                          "value": "Total Job Materials"
                        },
                        {
                          "value": "2400.72"
                        }
                      ]
                    },
                    "type": "Section"
                  }
                ]
              },
              "Summary": {
                "ColData": [
                  {
                    "value": "Total Landscaping Services"
                  },
                  {
                    "value": "2760.72"
                  }
                ]
              },
              "type": "Section"
            },
            {
              "ColData": [
                {
                  "value": "Pest Control Services",
                  "id": "54"
                },
                {
                  "value": "-100.00"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "Sales of Product Income",
                  "id": "79"
                },
                {
                  "value": "44.00"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "Services",
                  "id": "1"
                },
                {
                  "value": "400.00"
                }
              ],
              "type": "Data"
            }
          ]
        },
        "Summary": {
          "ColData": [
            {
              "value": "Total Income"
            },
            {
              "value": "4049.22"
            }
          ]
        },
        "type": "Section",
        "group": "Income"
      },
      {
        "Summary": {
          "ColData": [
            {
              "value": "Gross Profit"
            },
            {
              "value": "4049.22"
            }
          ]
        },
        "type": "Section",
        "group": "GrossProfit"
      },
      {
        "Header": {
          "ColData": [
            {
              "value": "Expenses"
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "Header": {
                "ColData": [
                  {
                    "value": "Automobile",
                    "id": "55"
                  },
                  {
                    "value": "19.99"
                  }
                ]
              },
              "Rows": {
                "Row": [
                  {
                    "ColData": [
                      {
                        "value": "Fuel",
                        "id": "56"
                      },
                      {
                        "value": "179.15"
                      }
                    ],
                    "type": "Data"
                  }
                ]
              },
              "Summary": {
                "ColData": [
                  {
                    "value": "Total Automobile"
                  },
                  {
                    "value": "199.14"
                  }
                ]
              },
              "type": "Section"
            },
            {
              "Header": {
                "ColData": [
                  {
                    "value": "Job Expenses",
                    "id": "58"
                  },
                  {
                    "value": "108.09"
                  }
                ]
              },
              "Rows": {
                "Row": [
                  {
                    "Header": {
                      "ColData": [
                        {
                          "value": "Job Materials",
                          "id": "63"
                        },
                        {
                          "value": ""
                        }
                      ]
                    },
                    "Rows": {
                      "Row": [
                        {
                          "ColData": [
                            {
                              "value": "Decks and Patios",
                              "id": "64"
                            },
                            {
                              "value": "88.09"
                            }
                          ],
                          "type": "Data"
                        }
                      ]
                    },
                    "Summary": {
                      "ColData": [
                        {
                          "value": "Total Job Materials"
                        },
                        {
                          "value": "88.09"
                        }
                      ]
                    },
                    "type": "Section"
                  }
                ]
              },
              "Summary": {
                "ColData": [
                  {
                    "value": "Total Job Expenses"
                  },
                  {
                    "value": "196.18"
                  }
                ]
              },
              "type": "Section"
            },
            {
              "Header": {
                "ColData": [
                  {
                    "value": "Legal & Professional Fees",
                    "id": "12"
                  },
                  {
                    "value": ""
                  }
                ]
              },
              "Rows": {
                "Row": [
                  {
                    "ColData": [
                      {
                        "value": "Accounting",
                        "id": "69"
                      },
                      {
                        "value": "75.00"
                      }
                    ],
                    "type": "Data"
                  },
                  {
                    "ColData": [
                      {
                        "value": "Lawyer",
                        "id": "71"
                      },
                      {
                        "value": "100.00"
                      }
                    ],
                    "type": "Data"
                  }
                ]
              },
              "Summary": {
                "ColData": [
                  {
                    "value": "Total Legal & Professional Fees"
                  },
                  {
                    "value": "175.00"
                  }
                ]
              },
              "type": "Section"
            },
            {
              "ColData": [
                {
                  "value": "Maintenance and Repair",
                  "id": "72"
                },
                {
                  "value": "185.00"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "Meals and Entertainment",
                  "id": "13"
                },
                {
                  "value": "5.66"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "Rent or Lease",
                  "id": "17"
                },
                {
                  "value": "900.00"
                }
              ],
              "type": "Data"
            },
            {
              "Header": {
                "ColData": [
                  {
                    "value": "Utilities",
                    "id": "24"
                  },
                  {
                    "value": ""
                  }
                ]
              },
              "Rows": {
                "Row": [
                  {
                    "ColData": [
                      {
                        "value": "Gas and Electric",
                        "id": "76"
                      },
                      {
                        "value": "114.09"
                      }
                    ],
                    "type": "Data"
                  },
                  {
                    "ColData": [
                      {
                        "value": "Telephone",
                        "id": "77"
                      },
                      {
                        "value": "74.36"
                      }
                    ],
                    "type": "Data"
                  }
                ]
              },
              "Summary": {
                "ColData": [
                  {
                    "value": "Total Utilities"
                  },
                  {
                    "value": "188.45"
                  }
                ]
              },
              "type": "Section"
            }
          ]
        },
        "Summary": {
          "ColData": [
            {
              "value": "Total Expenses"
            },
            {
              "value": "1849.43"
            }
          ]
        },
        "type": "Section",
        "group": "Expenses"
      },
      {
        "Summary": {
          "ColData": [
            {
              "value": "Net Operating Income"
            },
            {
              "value": "2199.79"
            }
          ]
        },
        "type": "Section",
        "group": "NetOperatingIncome"
      },
      {
        "Header": {
          "ColData": [
            {
              "value": "Other Expenses"
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "ColData": [
                {
                  "value": "Miscellaneous",
                  "id": "14"
                },
                {
                  "value": "916.00"
                }
              ],
              "type": "Data"
            }
          ]
        },
        "Summary": {
          "ColData": [
            {
              "value": "Total Other Expenses"
            },
            {
              "value": "916.00"
            }
          ]
        },
        "type": "Section",
        "group": "OtherExpenses"
      },
      {
        "Summary": {
          "ColData": [
            {
              "value": "Net Other Income"
            },
            {
              "value": "-916.00"
            }
          ]
        },
        "type": "Section",
        "group": "NetOtherIncome"
      },
      {
        "Summary": {
          "ColData": [
            {
              "value": "Net Income"
            },
            {
              "value": "1283.79"
            }
          ]
        },
        "type": "Section",
        "group": "NetIncome"
      }
    ]
  }
}


// Deserialize to object
var rootObj = JsonConvert.DeserializeObject<ProfitLoss.Rootobject>( pandltext );
我明白了

下面是我用来获取值列表的代码:

var jObj = JObject.Parse( pandltext );
var results = jObj.SelectTokens( "$..Rows.Row[?(@.type == 'Data')]..value" ).ToList();
编辑

foreach(var item in colData)
{
    Console.WriteLine(string.Join("=", item.Select(x => x["value"])));
}

公共类选项
    {
公共字符串名称{get;set;}
公共字符串值{get;set;}
   }
公共类标题
    {
公共日期时间{get;set;}
公共字符串ReportName{get;set;}
公共字符串ReportBasis{get;set;}
公共字符串StartPeriod{get;set;}
公共字符串结束周期{get;set;}
公共字符串SUMMARIECOLUMNSBY{get;set;}
公共字符串货币{get;set;}
公共IList选项{get;set;}
    }
公共类元数据
    {
公共字符串名称{get;set;}
公共字符串值{get;set;}
    }
公共类专栏
    {
公共字符串ColTitle{get;set;}
公共字符串ColType{get;set;}
公共IList元数据{get;set;}
    }
公共类列
    {
公共IList列{get;set;}
    }
公共类ColData
    {
公共字符串值{get;set;}
公共字符串id{get;set;}
    }
公共类ColData
    {
公共字符串值{get;set;}
公共字符串id{get;set;}
    }
公共类行
   {
公共IList ColData{get;set;}
公共字符串类型{get;set;}
    }
公共类行
    {
公共IList行{get;set;}
    }
公共类ColData
    {
公共字符串值{get;set;}
    }
公开课摘要
    {
公共IList ColData{get;set;}
    }
公共类ColData
    {
公共字符串值{get;set;}
公共字符串id{get;set;}
    }
公共类行
    {
公共头{get;set;}
公共行{get;set;}
公共摘要摘要{get;set;}
公共字符串类型{get;set;}
公共IList ColData{get;set;}
    }
公共类行
    {
公共IList行{get;set;}
    }
公共类行
    {
公共IList ColData{get;set;}
公共字符串类型{get;set;}
公共头{get;set;}
公共行{get;set;}
公共摘要{get;set;}
    }
公共类行
    {
公共IList行{get;set;}
    }
公共类行
    {
公共头{get;set;}
公共行{get;set;}
公共摘要{get;set;}
公共字符串类型{get;set;}
公共字符串组{get;set;}
    }
公共类行
    {
公共IList行{get;set;}
    }
公开课范例
    {
公共标头{get;set;}
公共列{get;set;}
公共行{get;set;}
    }
并将其用于:

Example results =  Newtonsoft.JSON.JsonConvert.DeserializeObject<Example>(json);
Example results=Newtonsoft.JSON.JsonConvert.DeserializeObject(JSON);

请用代码发布一些您尝试过的示例,并描述它是如何不按预期工作的。另外,发布一个有效/完整的json,以便人们可以使用它。为什么不尝试使用regex?@Youssef13您可以反序列化到对象,然后使用Linq。为什么要使用正则表达式进行此操作。@请使用适当的工具。regex在这里不是合适的工具。谢谢@Eser。我将此作为获取值列表的起点,但我仍在尝试使其可用。使用您答案中的代码,我得到一个值,它看起来像
{[{“value”:“Design income”,“id”:“82”},{“value”:“975.00”}}
,我无法访问列表中的值。我需要“设计收入”和“975.00”。@mack将答案中棘手的部分带入你的问题,并用你的新能力扩展你的问题。这是问问题的方式吗?无论如何,我已经更新了答案,非常感谢你抽出时间来回答这个问题。我不打算用我的编辑来更改问题,只想在原始问题中添加新信息。对不起,如果这改变了原来的问题。再次感谢你的帮助。你的回答让我又朝着正确的方向前进了。
var jObj = JObject.Parse( pandltext );
var results = jObj.SelectTokens( "$..Rows.Row[?(@.type == 'Data')]..value" ).ToList();
var jObj = JObject.Parse(json);

var colData = jObj.SelectTokens("$..ColData")
                .Except(jObj.SelectTokens("$..Summary.ColData"))
                .ToList();
foreach(var item in colData)
{
    Console.WriteLine(string.Join("=", item.Select(x => x["value"])));
}
var finalList = colData.Select(item => item.Select(x => (string)x["value"]).ToList())
               .ToList();
    public class Option
    {
         public string Name { get; set; }
         public string Value { get; set; }
    }

    public class Header
    {
        public DateTime Time { get; set; }
        public string ReportName { get; set; }
        public string ReportBasis { get; set; }
        public string StartPeriod { get; set; }
        public string EndPeriod { get; set; }
        public string SummarizeColumnsBy { get; set; }
        public string Currency { get; set; }
        public IList<Option> Option { get; set; }
    }

    public class MetaData
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

    public class Column
    {
        public string ColTitle { get; set; }
        public string ColType { get; set; }
        public IList<MetaData> MetaData { get; set; }
    }

    public class Columns
    {
        public IList<Column> Column { get; set; }
    }

    public class ColData
    {
         public string value { get; set; }
         public string id { get; set; }
    }

    public class ColData
    {
         public string value { get; set; }
         public string id { get; set; }
    }

    public class Row
    {
        public IList<ColData> ColData { get; set; }
        public string type { get; set; }
    }

    public class Rows
    {
        public IList<Row> Row { get; set; }
    }

    public class ColData
    {
        public string value { get; set; }
    }

    public class Summary
    {
        public IList<ColData> ColData { get; set; }
    }

    public class ColData
    {
        public string value { get; set; }
        public string id { get; set; }
    }

    public class Row
    {
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public Summary Summary { get; set; }
        public string type { get; set; }
        public IList<ColData> ColData { get; set; }
    }

    public class Rows
    {
        public IList<Row> Row { get; set; }
    }

    public class Row
    {
        public IList<ColData> ColData { get; set; }
        public string type { get; set; }
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public  Summary { get; set; }
    }

    public class Rows
    {
        public IList<Row> Row { get; set; }
    }

    public class Row
    {
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public  Summary { get; set; }
        public string type { get; set; }
        public string group { get; set; }
    }

    public class Rows
    {
        public IList<Row> Row { get; set; }
    }

    public class Example
    {
        public Header Header { get; set; }
        public Columns Columns { get; set; }
        public Rows Rows { get; set; }
    }
Example results =  Newtonsoft.JSON.JsonConvert.DeserializeObject<Example>(json);