在C#中解析JSON

时间:2016-11-05 13:56:30

标签: c# json parsing

我有像这样的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"

3 个答案:

答案 0 :(得分:2)

我刚刚使用了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.CSharp作为项目的参考,以便进行编译。

答案 1 :(得分:1)

正如所指出的,如果你没有合法的JSON,那么你需要编写自己的解析器。

这并不难做到,但是您没有提供足够的源文件定义供我为您编写。例如,你真的发布了正确的JSON吗?是否始终以"value"开头,其他多少内容是固定的还是可以变化的?

答案 2 :(得分:0)

如果您想使用自定义解析,可以选择其他解决方案:

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);