在c#中解析JSON

本文关键字:JSON | 更新日期: 2023-09-27 18:16:40

我有这样的JSON文件

{
   "Value":{
      "9824800036":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9127801433":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9127801368":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9127801458":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9127801485":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9814008909":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9814008920":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9814008911":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9814008910":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9116665832":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9814008955":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      },
      "9116666000":{
         "TZ":0,
         "AllowChange":3,
         "InCall":true,
         "OutCall":true,
         "BeginDT":"00:00:00",
         "EndDT":"23:59:59",
         "Days":127
      }
   },
   "Success":true,
   "Error":null,
   "Failure":false
}

如何得到

"9814008920","9814008911","9814008910","9116665832","9814008955" ?

在c#中解析JSON

我刚用了Newtonsoft。Json,并能够读取Json与以下代码:

string json = File.ReadAllText(pathToFile);
dynamic jsonObj = JsonConvert.DeserializeObject(json);
dynamic value = jsonObj["Value"];
foreach (var val in value)
{
     Console.WriteLine(val.Name);
}

您将需要包括Microsoft。

如前所述,如果您没有合法的JSON,那么您需要编写自己的解析器。

这并不难做到,但是您没有提供源文件的充分定义,我无法为您编写它。例如,您是否真的发布了正确的JSON?它总是从"value"开始吗,其他物质有多少是固定的或可以变化的?

如果您想使用自定义解析,另一种解决方案是:

var source = File.ReadAllText(pathToFile);
string pattern = @"""'d{10}""";    // Note: {10} wil match only 10 digit number           
var matches = Regex.Matches(source, pattern);
foreach (var val in matches)
   Console.WriteLine(val);