2016-11-25 22 views
0

デシリアライズしようとしている次のJSONがあります。JSONのデシリアライズでnullが返されるC#

{ 
"output-parameters":[ 
    { 
    "value":{ 
     "array":{ 
      "elements":[ 
       { 
       "string":{ 
        "value":"cp-100" 
       } 
       }, 
       { 
       "string":{ 
        "value":"cp-101" 
       } 
       }, 
       { 
       "string":{ 
        "value":"cp-100" 
       } 
       }, 
       { 
       "string":{ 
        "value":"cp-101" 
       } 
       }, 
       { 
       "string":{ 
        "value":"cp-100" 
       } 
       } 
      ] 
     } 
    }, 
    "type":"Array/string", 
    "name":"Tags", 
    "scope":"local" 
    }, 
    { 
    "value":{ 
     "string":{ 
      "value":"subscribed" 
     } 
    }, 
    "type":"string", 
    "name":"Error", 
    "scope":"local" 
    } 
] 
} 

私はそれをデシリアライズしていながら、私はすべてのエラーを得ることはありませんJSON

public class OutputParameter 
{ 
    [JsonProperty(PropertyName = "value")] 
    public value value { get; set; } 

    [JsonProperty(PropertyName = "name")] 
    public string name { get; set; } 
} 

public class value 
{ 
    [JsonProperty(PropertyName = "array")] 
    public array_ array_ { get; set; } 
} 

public class array_ 
{ 
    [JsonProperty(PropertyName = "elements")] 
    public element[] element { get; set; } 
} 

public class element 
{ 
    [JsonProperty(PropertyName = "value")] 
    public string value { get; set; } 
} 

をバインドするために、次のクラスを作成しました。また、私は簡単にナビゲートすることができます。

でも、要素[n](output_parameters [0] .value.array_.element [0] .value)の値を取得しようとしています。 nullを返します。

ここで問題は何ですか?

注:JSONから取得する値はほとんど必要ありません。たとえば、私は "タイプ"のような値を必要としません: "文字列" "名前": "エラー" "スコープ": "ローカル" それは私がこのようなC#クラスを作成した理由です。

+1

を抽出してきましたか? –

+1

JSONからC#クラスを生成するためにhttp://json2csharp.com/を使用してみてください。 –

答えて

3

output-parametersは配列[であり、より多くの値またはリストが含まれている可能性がありますので、出力パラメータのリストを含むRootobjectを作成してください。

output-parametersは、valuenameを含むだけでなく、それに含まれています。 typescopeが同じ階層にあります。あなたがあなたのクラス値もSomeString1

public class Rootobject 
{ 
    [JsonProperty(PropertyName = "output-parameters")] 
    public List<OutputParameters> outputparameters { get; set; } 
} 
public class OutputParameters 
{ 
    [JsonProperty(PropertyName = "value")] 
    public SomeValue value { get; set; } 

    [JsonProperty(PropertyName = "type")] 
    public string type { get; set; } 

    [JsonProperty(PropertyName = "name")] 
    public string name { get; set; } 

    [JsonProperty(PropertyName = "scope")] 
    public string scope { get; set; } 
} 
public class SomeValue 
{ 
    [JsonProperty(PropertyName = "array")] 
    public SomeArray array { get; set; } 

    [JsonProperty(PropertyName = "string")] 
    public SomeString1 _string { get; set; } 
} 
public class SomeArray 
{ 
    [JsonProperty(PropertyName = "elements")] 
    public List<SomeElement> elements { get; set; } 
} 
public class SomeElement 
{ 
    [JsonProperty(PropertyName = "string")] 
    public SomeString _string { get; set; } 
} 
public class SomeString 
{ 
    [JsonProperty(PropertyName = "value")] 
    public string value { get; set; } 
} 
public class SomeString1 
{ 
    [JsonProperty(PropertyName = "value")] 
    public string value { get; set; } 
} 

としてここに示されstringが含まSomeStringSomeString1

としてここに呼ばれているstringと呼ばれるノードのための宣言が次にあなたが使用して、それをデシリアライズべきではない持って

次のコード

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr); 
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);    

スクリーンショットあなたは "output_parameters [0] .value.array_.element [0] .string.value" を探してはならない値を示し、Tは

ScreenShot

+0

パブリックプロパティはPascalCaseにある必要があります –

+0

@ManfredRadlwimmer:これは命名規則の標準の1つですが、これは必須ではありません。 –

+0

これは同じことを言います..値はnullです:( – Sandaru

関連する問題