2017-01-25 29 views
0

我收到來自第三方以下JSON字符串:不能在C#中反序列化JSON字符串

{ 
    "Ch": [{ 
     "i": 100, 
     "m": "Time", 
     "u": "(sec)", 
     "d": 0, 
     "z": 0.345313, 
     "yi": "0.000000", 
     "ya": "6.906250", 
     "a": 0, 
     "s": 1664, 
     "data": "RN]]>" 
    }, { 
     "i": 101, 
     "m": "Stress", 
     "u": "(kPa)", 
     "d": 0, 
     "z": 60, 
     "yi": "0.000000", 
     "ya": "1200.000000", 
     "a": 0 
    }, { 
     "i": 102, 
     "m": "Strain", 
     "u": "(micro e)", 
     "d": 0, 
     "z": 8, 
     "yi": "200.000000", 
     "ya": "360.000000", 
     "a": 0 
    }, { 
     "i": 103, 
     "m": "Stress", 
     "u": 360, 
     "d": 0, 
     "z": 0, 
     "yi": "0.000000", 
     "ya": "0.000000", 
     "a": 0, 
     "s": 1664, 
     "data": "QVORR`Pb_UQRR</code>OObTNRRUTaWRVRRSaPQPdRRaPNSORRR]]>" 
    }, { 
     "i": 104, 
     "m": "Strain", 
     "u": 360, 
     "d": 0, 
     "z": 0, 
     "y": 0, 
     "yi": "0.000000", 
     "ya": "0.000000", 
     "a": 1, 
     "s": 1664, 
     "data": "SVdRQSP_VWQRQa]]>" 
    }] 
} 

我用下面的類:

public class testCh 
{ 
    public int i { get; set; } 
    public string m { get; set; } 
    public object u { get; set; } 
    public int d { get; set; } 
    public double z { get; set; } 
    public string yi { get; set; } 
    public string ya { get; set; } 
    public int a { get; set; } 
    [JsonIgnore] 
    public int s { get; set; } 
    [JsonIgnore] 
    public string data { get; set; } 

} 

public class testRootObject 
{ 
    public List<testCh> tCh { get; set; } 
} 

最後我嘗試這在我的主類:

var response1 = JsonConvert.DeserializeObject<testRootObject>(content1); 

foreach (testCh tc in response1.tCh) 
{ 
    string name = tc.m; 
} 

我得到一個空testRootObject []

我試着在我testCH類忽略「a」和「數據」:

[JsonIgnore] 
public int s { get; set; } 

[JsonIgnore] 
public string data { get; set; } 

,並沒有奏效。

我不知道爲什麼我不能反序列化它。它一定是愚蠢的,但在嘗試了幾天之後,我看不到它。

任何幫助或提示,非常感謝。

+0

請格式化你的JSON。這是完全不可讀的 –

+4

嘗試將'testRootObject'類的'tCh'屬性重命名爲'Ch'。 –

+0

爲c#類和json中的對象嘗試一個相同的名稱。因爲它尋找一個匹配的名字。 –

回答

0

我檢查了這一點,並確認。你在這裏得到的唯一錯誤就是列表對象的名字。它應該是Ch和你的json字符串一樣。

public class testRootObject 
{ 
    public List<testCh> Ch { get; set; } 
} 

如果它是一個必須在該屬性的名稱應該是tCh比你能做到以下幾點:

public class testRootObject 
{ 
    [JsonProperty("Ch")] 
    public List<testCh> tCh { get; set; } 
} 
+0

非常感謝你! :d – DanielaTahnee