简体   繁体   中英

Parse JSON in C#

I have JSON file like this

{
   "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
}

How i can get

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

I just used Newtonsoft.Json and was able to read your json with the following code:

string json = File.ReadAllText(pathToFile);
dynamic jsonObj = JsonConvert.DeserializeObject(json);
dynamic value = jsonObj["Value"];

foreach (var val in value)
{
     Console.WriteLine(val.Name);
}

You will need to include Microsoft.CSharp as a reference to your project for this to compile.

As pointed out, if you don't have legal JSON, then you'll need to write your own parser.

This isn't that difficult to do, but you haven't provided a sufficient definition of your source files for me to write it for you. For example, did you really post the correct JSON? Does it always start with "value" , and how much of the other stuff is fixed or can vary?

An alternative solution if you want to use custom parsing :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM