Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析复杂JSON:多循环与类_C#_Json_Parsing_Json.net - Fatal编程技术网

C# 解析复杂JSON:多循环与类

C# 解析复杂JSON:多循环与类,c#,json,parsing,json.net,C#,Json,Parsing,Json.net,我有一个Json类型: { "JobProcessors": [ { "JobName": "ArchivalJob", "IsEnabled": true, "Batching": { "BatchSize": 0, "DegreeOfParallelism": -1 }, "Settings": { "ArchivalJobCollectionPageSize": 50 } }, { "JobName": "AuditLogJob",

我有一个Json类型:

{
"JobProcessors": [
{
  "JobName": "ArchivalJob",
  "IsEnabled": true,
  "Batching": {
    "BatchSize": 0,
    "DegreeOfParallelism": -1
  },
  "Settings": {
    "ArchivalJobCollectionPageSize": 50
  }
},
{
  "JobName": "AuditLogJob",
  "IsEnabled": false,
  "Batching": {
    "BatchSize": 10,
    "DegreeOfParallelism": -1
  },
  "Settings": {}
} 
],  
"ScheduledJobs": [
{
  "JobName": "RemoteStartClientCommandJob",
  "PrimaryAction": {
    "ConnectionString": "#JobProcessorsIntegrationSBConnectionStringValue#",
     "Settings": {
      "LeadTimeInSeconds": "600",
      "MaxSrsJobCount": 25
    }
  },
  "ErrorAction": {
    "ConnectionString": "#PairedJobProcessorIntegrationSBConnectionStringValue#",
    "EntityPath": "remotestartqueue",
    "Settings": {
      "LeadTimeInSeconds": "600",
      "MaxSrsJobCount": 25
    }
  }
}
]  
}
我想检查“作业处理程序”类别下所有“作业名称”的“IsEnabled”属性。 在C#中,我至今使用的是:

dynamic parsedJson = JsonConvert.DeserializeObject(reader.GetString(1));
foreach (var item in parsedJson)
{
    foreach (var smallitem in item)
    {
        foreach (var tag in smallitem)
        {
            if(tag.IsEnabled.toString()=="true"){
                Console.WriteLine("true");
            }                                 
        }
    }

}
这给了我正确的结果,除了它还对“ScheduledJobs”进行迭代。但主要问题是:


这是正确的还是最有效的方法?如果可能,建议一些更好的方法


我所知道的一个是使用类,但我可能事先不知道json结构。而且json非常庞大,因此创建类可能会很麻烦

在您的代码片段中,我们使用两个foreach,对于大型对象可能需要时间。因此,我们可以在单个foreach中执行相同的操作,或者如果您有特定的节点要获取或搜索,我们可以使用linq,首先我们需要将json对象转换为c#object。要将Json对象转换为C#,您可以使用此站点“”,然后我们可以将Json对象反序列化为C#

会是这样的

string jsonString = "your Json Object as string";
        var jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
        foreach (JobProcessor obj in jsonObject.JobProcessors)
        {
            string JobName = obj.JobName;
            bool value=obj.IsEnabled;
        }
string jsonString=“您的Json对象作为字符串”;
var jsonObject=JsonConvert.DeserializeObject(jsonString);
foreach(jsonObject.JobProcessors中的JobProcessor对象)
{
字符串JobName=obj.JobName;
布尔值=对象已启用;
}
我还将给定的Json转换为c#对象,如果Json对象相同,您可以直接使用这些类

    public class Batching
    {
        public int BatchSize { get; set; }
        public int DegreeOfParallelism { get; set; }
    }

    public class Settings
    {
        public int ArchivalJobCollectionPageSize { get; set; }
    }

    public class JobProcessor
    {
        public string JobName { get; set; }
        public bool IsEnabled { get; set; }
        public Batching Batching { get; set; }
        public Settings Settings { get; set; }
    }

    public class Settings2
    {
        public string LeadTimeInSeconds { get; set; }
        public int MaxSrsJobCount { get; set; }
    }

    public class PrimaryAction
    {
        public string ConnectionString { get; set; }
        public Settings2 Settings { get; set; }
    }

    public class Settings3
    {
        public string LeadTimeInSeconds { get; set; }
        public int MaxSrsJobCount { get; set; }
    }

    public class ErrorAction
    {
        public string ConnectionString { get; set; }
        public string EntityPath { get; set; }
        public Settings3 Settings { get; set; }
    }

    public class ScheduledJob
    {
        public string JobName { get; set; }
        public PrimaryAction PrimaryAction { get; set; }
        public ErrorAction ErrorAction { get; set; }
    }

    public class RootObject
    {
        public List<JobProcessor> JobProcessors { get; set; }
        public List<ScheduledJob> ScheduledJobs { get; set; }
    }
公共类批处理
{
public int BatchSize{get;set;}
公共int度并行性{get;set;}
}
公共类设置
{
public int ArchivalJobCollectionPageSize{get;set;}
}
公共类作业处理器
{
公共字符串JobName{get;set;}
公共布尔值已启用{get;set;}
公共批处理批处理{get;set;}
公共设置{get;set;}
}
公共类设置2
{
公共字符串LeadTimeInSeconds{get;set;}
public int maxsrssjobcount{get;set;}
}
公共集体首要诉讼
{
公共字符串连接字符串{get;set;}
公共设置2设置{get;set;}
}
公共类设置3
{
公共字符串LeadTimeInSeconds{get;set;}
public int maxsrssjobcount{get;set;}
}
公共集体诉讼
{
公共字符串连接字符串{get;set;}
公共字符串EntityPath{get;set;}
公共设置3设置{get;set;}
}
公共类ScheduledJob
{
公共字符串JobName{get;set;}
公共PrimaryAction PrimaryAction{get;set;}
公共错误操作错误操作{get;set;}
}
公共类根对象
{
公共列表作业处理器{get;set;}
公共列表ScheduledJobs{get;set;}
}
希望这会有所帮助。
谢谢

在您的代码片段中,我们使用两个foreach,对于大型对象可能需要时间。因此,我们可以在单个foreach中执行相同的操作,或者如果您有特定的节点要获取或搜索,我们可以使用linq,首先我们需要将json对象转换为c#object。要将Json对象转换为C#,您可以使用此站点“”,然后我们可以将Json对象反序列化为C#

会是这样的

string jsonString = "your Json Object as string";
        var jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
        foreach (JobProcessor obj in jsonObject.JobProcessors)
        {
            string JobName = obj.JobName;
            bool value=obj.IsEnabled;
        }
string jsonString=“您的Json对象作为字符串”;
var jsonObject=JsonConvert.DeserializeObject(jsonString);
foreach(jsonObject.JobProcessors中的JobProcessor对象)
{
字符串JobName=obj.JobName;
布尔值=对象已启用;
}
我还将给定的Json转换为c#对象,如果Json对象相同,您可以直接使用这些类

    public class Batching
    {
        public int BatchSize { get; set; }
        public int DegreeOfParallelism { get; set; }
    }

    public class Settings
    {
        public int ArchivalJobCollectionPageSize { get; set; }
    }

    public class JobProcessor
    {
        public string JobName { get; set; }
        public bool IsEnabled { get; set; }
        public Batching Batching { get; set; }
        public Settings Settings { get; set; }
    }

    public class Settings2
    {
        public string LeadTimeInSeconds { get; set; }
        public int MaxSrsJobCount { get; set; }
    }

    public class PrimaryAction
    {
        public string ConnectionString { get; set; }
        public Settings2 Settings { get; set; }
    }

    public class Settings3
    {
        public string LeadTimeInSeconds { get; set; }
        public int MaxSrsJobCount { get; set; }
    }

    public class ErrorAction
    {
        public string ConnectionString { get; set; }
        public string EntityPath { get; set; }
        public Settings3 Settings { get; set; }
    }

    public class ScheduledJob
    {
        public string JobName { get; set; }
        public PrimaryAction PrimaryAction { get; set; }
        public ErrorAction ErrorAction { get; set; }
    }

    public class RootObject
    {
        public List<JobProcessor> JobProcessors { get; set; }
        public List<ScheduledJob> ScheduledJobs { get; set; }
    }
公共类批处理
{
public int BatchSize{get;set;}
公共int度并行性{get;set;}
}
公共类设置
{
public int ArchivalJobCollectionPageSize{get;set;}
}
公共类作业处理器
{
公共字符串JobName{get;set;}
公共布尔值已启用{get;set;}
公共批处理批处理{get;set;}
公共设置{get;set;}
}
公共类设置2
{
公共字符串LeadTimeInSeconds{get;set;}
public int maxsrssjobcount{get;set;}
}
公共集体首要诉讼
{
公共字符串连接字符串{get;set;}
公共设置2设置{get;set;}
}
公共类设置3
{
公共字符串LeadTimeInSeconds{get;set;}
public int maxsrssjobcount{get;set;}
}
公共集体诉讼
{
公共字符串连接字符串{get;set;}
公共字符串EntityPath{get;set;}
公共设置3设置{get;set;}
}
公共类ScheduledJob
{
公共字符串JobName{get;set;}
公共PrimaryAction PrimaryAction{get;set;}
公共错误操作错误操作{get;set;}
}
公共类根对象
{
公共列表作业处理器{get;set;}
公共列表ScheduledJobs{get;set;}
}
希望这会有所帮助。
感谢您

,因为您已经在做
JObject.Parse(jsonstring)
要解析JSON字符串,您可以使用与查找“JobProcessors”下的所有“JobName”对象:

注:

  • 是递归下降运算符:它递归下降返回每个项的
    JToken
    层次结构,随后与查询字符串的其余部分进行匹配

  • JobProcessors
    返回该名称的属性值

  • [?(@.JobName)]
    返回数组项(在本例中为
    JobProcessors
    ),这些数组项是具有
    JobName
    属性的对象

  • (bool?
    将“IsEnabled”的值强制转换为