2016-07-12 14 views
0

私はタイプのJSONを持っている:解析、複雑な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 
    } 
    } 
} 
] 
} 

私は"の下に来ているすべての「のJobName」のための「でIsEnabled」プロパティを確認したいですJobProcessors "カテゴリ。私が今まで使用してきたものの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構造を知らないかもしれません。また、ジェンソンは非常に巨大なので、クラスを作ることは面倒なことがあります!

+0

あなたはdynamic' 'に解析しているのはなぜ?あなたの構造を表現するために強く型付けされたオブジェクトを作成することをお勧めします。 – haim770

+0

forループを使ってforeachを切り替えます。 forループは、常により速く、各ループにつきます。 –

+0

@ haim770私はクラスの構造を事前に知っていないかもしれません。 – Arushi

答えて

1

、あなたは「JobProcessors」の下にあるすべての「のJobName」オブジェクトを見つけるために、JSONPath querySelectTokens()を使用することができますありがとう:

// I want to check the "IsEnabled" property for all "JobName" for which come under "JobProcessors" 
foreach (var job in root.SelectTokens("..JobProcessors[?(@.JobName)]")) 
{ 
    var isEnabled = (bool?)job["IsEnabled"]; 
    Debug.WriteLine(string.Format("Job {0}: IsEnabled={1}", job["JobName"], isEnabled)); 
} 

注:

  • ..再帰下降演算子である:それは再帰的にJToken hieraを下降しますrchyは各項目を返し、続いてクエリ文字列の残りの部分と照合されます。

  • JobProcessorsは、その名前のプロパティの値を返します。

  • [?(@.JobName)]は、JobNameプロパティを持つオブジェクトである配列項目(この場合はJobProcessors)を返します。

  • (bool?) "IsEnabled"の値をブール値にキャストするか、欠損している場合はnullをキャストします。

そして、これの出力は次のようになります。

Job ArchivalJob: IsEnabled=True 
Job AuditLogJob: IsEnabled=False 
+0

特にこの '[?(@。JobName)]'に感謝します。 – Arushi

1

コードスニペットでは2つのforeachを使用しているため、大きなオブジェクトには時間がかかることがあります。したがって、単一のforeach内で同じことをすることができます。または、特定のノードをフェッチまたは検索してlinqを使用できる場合は、まずjsonオブジェクトをc#オブジェクトに変換する必要があります。 JsonオブジェクトをC#に変換するには、このサイト "http://json2csharp.com/"を使用して、JsonオブジェクトをDeserializeして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; 
     } 

のようなものだろうと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; } 
    } 

希望すると、これが役立ちます。 は、すでにあなたのJSON文字列を解析するためにJObject.Parse(jsonstring);を行っていることを考えると

+0

このアプローチにはありがたいですが、クラスを使用したくありませんでした! – Arushi

関連する問題