2016-09-12 10 views
0

私は複雑なJSONオブジェクトを持っています。問題は、JSONモデル内の項目のサブリストが要素ごとに存在しないことがあることです。つまり完全に欠けている。ループJSONオブジェクトは常に存在するとは限りません

JSONオブジェクトはすべてを返すいくつかの要素のために、私は多くの場合、2を得ることができるか、3完全な私は、少なくともいくつかの部分のための私のオブジェクトモデルを知っているので、アイテムを返して、変わり、動作しますが、例えば名1以下からモデルは正常に返されます。しかし、その後、構文解析のエラー名前2。だから、例外なしでリスト全体をループすることはできない。

"Object reference not set to an instance of an object" 

Try Catchブロック内に出力をラップしようとしましたが、まだエラーが返されています。

サンプル

[ 
    { 
    "name": "Name1", 
    "description": "", 
    "location": "ANY", 
    "inputs":  [ 
    { 
     "name": "input1", 
     "required": true, 
     "description": "some short description" 
    }, 
    { 
     "name": "input2", 
     "required": true, 
     "description": "another short description" 
    } 
    ], 
    "outputs":  [ 
    { 
     "name": "output1", 
     "required": false 
    }, 
    { 
     "name": "outpu2", 
     "required": false 
    } 
    ] 
}, 
    { 
    "name": "Name2", 
    "description": "some long description", 
    "location": "ANY", 
    "inputs": [  { 
     "name": "input1", 
     "required": false, 
     "description" : "some short description of the input" 
    }] 
    }, 
    { 
    "name": "Name3", 
    "description": "", 
    "location": "ANY" 
    } 
] 

私のC#の定義は、このJSONオブジェクトの最初の参照のために働くすべてのユースケースとJSON、しかし「NAME2さん 『のNAME3オブジェクト参照のインスタンスに設定されていない』私は、これは取得しています」オブジェクトの」エラー。

私のC#JSON定義

public class inputParameters 
{ 
    [JsonProperty("name")] 
    public string inputName { get; set; } 

    [JsonProperty("required")] 
    public bool inputRequired {get; set;} 

    [JsonProperty("description")] 
    public string inputDescription {get; set;} 
} 
public class outputParameters 
{ 
    public string outputName { get; set; } 
    public bool outputRequired { get; set; } 
} 
public class JsonObject 
{ 
    [JsonProperty("name")] 
    public string ProcessName { get; set; } 

    [JsonProperty("description")] 
    public string ProcessDescription { get; set; } 

    [JsonProperty("peerLocation")] 
    public string PeerLocation { get; set; } 

    public List<inputParameters> InputParameters { get; set; } 
    public List<outputParameters> OutputParameters{ get; set; } 
} 

そして、私のデシリアライズオブジェクトとは、

ループ
var Object = JsonConvert.DeserializeObject <List<JsonObject>>(txt); 

      foreach (JsonObject JsonObject in Object) 
      { 
       Console.WriteLine("Process Name: " + JsonObject.ProcessName); 
       Console.WriteLine("PeerLoacatoin: " + JsonObject.PeerLocation); 
       Console.WriteLine("Process Description: " +JsonObject.ProcessDescription); 


       foreach (var InputParams in JsonObject.InputParameters) 
       { 
        try 
        { 
         Console.WriteLine("Input Name: " + InputParams.inputName); 
         Console.WriteLine("Input Required: " + InputParams.inputRequired); 
         Console.WriteLine("Input Description: " + InputParams.inputDescription); 
        } 

        catch(Exception) 
        { 
         InputParams.inputName = ""; 
         InputParams.inputRequired = false; 
         InputParams.inputDescription = ""; 
        } 

       } 
       foreach (var OutputParams in JsonObject.OutputParameters) 
       { 
        try 
        { 
         Console.WriteLine("Output Name: " + OutputParams.outputName); 
         Console.WriteLine("Output Required: " + OutputParams.outputRequired); 
        } 
        catch (Exception) 
        { 
         OutputParams.outputName = ""; 
         OutputParams.outputRequired = false; 
        } 
       } 
       Console.WriteLine(); 
+0

なぜこれをJsonObjectのリストにデシリアライズしていますか?これを適切な.NETタイプにデシリアライズして操作する方が簡単でしょうか? – EJoshuaS

+0

そのメリットは何ですか?どのように実装するのですか? – Autonomic

答えて

1

Listをすべてnew Listに初期化するコンストラクタを作成します。そうすれば、nullポインタ例外を受け取ることなく安全に反復処理を実行できます。

class JsonObject 
{ 
    public JsonObject() 
    { 
     InputParameters = new List<inputParameters>(); 
     OutputParameters = new List<outputParameters>(); 
    } 
} 
+0

ありがとう!できます! public class JsonObject { ... public List InputParameters {get;セット; } パブリックリスト OutputParameters {get;セット; } public JsonObject() { InputParameters = new List (); OutputParameters =新しいリスト(); } } – Autonomic

関連する問題