2016-11-25 68 views
0

我有以下JSON,我試圖反序列化。DeSerializing JSON返回空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中取少數值。例如,我不需要像 「type」:「string」, 「name」:「Error」, 「scope」:「local」 這就是爲什麼我創建這樣的C#類的原因。

+1

你不應該尋找 「output_parameters [0] .value.array_.element [0] .string.value」? –

+1

嘗試使用http://json2csharp.com/從JSON生成C#類? –

回答

3

您的類應該看起來像這樣,因爲output-parameters是一個數組[,它可能包含更多值或列表,因此創建一個包含輸出參數列表的Rootobject

現在output-parameters不僅包含valuename而它也包含。 typescope處於相同的層次。

你沒有叫string聲明節點其在此處稱爲SomeStringSomeString1

你的類值還包含string其在這裏表示爲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; } 
} 

那麼你應該使用反序列化以下代碼

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

Screensho t內顯示該值已提取

ScreenShot

+0

公共財產應該在PascalCase –

+0

@ManfredRadlwimmer:這是沒有必要的,而它是命名約定的標準之一。 –

+0

這也說相同的事情..值爲空:( – Sandaru