2016-04-09 29 views
0

我有以下JSON:反序列化JSON對象中的C#Windows電話8

「{\」 DDlState \ 「:[{\」 respcode \ 「:0,\」 ID \ 「:\」 0 \」 ,\ 「國家\」:\ 「\」},{\ 「respcode \」:0,\ 「ID \」:\ 「0 \」,\ 「國家\」:\ 「所有\」},{\」 \「Id \」:\「1 \」,\「State \」:\「JAMMU AND KASHMIR \」},{\「respcode \」:0,\「Id \」:\「 2 \「,\」State \「:\」PUNJAB \「},{\」respcode \「:0,\」Id \「:\」3 \「,\」State \「:\」HIMACHAL PRADESH \「 },{\ 「respcode \」:0,\ 「ID \」:\ 「4 \」 \ 「狀態\」:\ 「HARYANA \」},{\ 「respcode \」:0,\ 「ID \」 :\ 「5 \」,\ 「狀態\」:\ 「DELHI \」},{\ 「respcode \」:0,\ 「ID \」:\ 「6 \」 \ 「狀態\」:\「北方邦PRADESH \ 「},{\」 respcode \ 「:0,\」 ID \ 「:\」 7 \ 「\ 」國家\「:\ 」BIHAR \「},{\ 」respcode \「:0,\」 Id \「:\」8 \「,\」State \「:\」WEST BENGAL \「},{\」respcode \「:0,\」Id \「:\」9 \「,\」State \ :\ 「奧里薩邦\」},{\ 「respcode \」:0,\ 「ID \」:\ 「10 \」,\ 「狀態\」:\ 「加爾克漢德\」},{\ 「respcode \」:0 ,\ 「ID \」:\ 「11 \」,\ 「國家\」:\ 「北安查爾邦\」},{\「respcode \ 「:0,\」 ID \ 「:\」 12 \」,\ 「狀態\」:\ 「恰蒂斯加爾\」},{\ 「respcode \」:0,\ 「ID \」:\ 「13 \」 ,\ 「國家\」:\ 「CHANDIGARH \」},{\ 「respcode \」:0,\ 「ID \」:\ 「14 \」,\ 「國家\」:\ 「拉賈斯坦邦\」},{\ 「respcode \」:0,\「Id \」:\「15 \」,\「State \」:\「MADHYA PRADESH \」},{\「respcode \」:0,\「Id \」:\「 16 \」 \ 「狀態\」:\ 「GUJARAT \」},{\ 「respcode \」:0,\ 「ID \」:\ 「17 \」,\ 「狀態\」:\ 「MAHARASHTRA \」} {\ 「respcode \」:0,\ 「ID \」:\ 「18 \」,\ 「國家\」:\ 「GOA \」},{\ 「respcode \」:0,\ 「ID \」: \ 「19 \」,\ 「狀態\」:\ 「KARNATAKA \」},{\ 「respcode \」:0,\ 「ID \」:\ 「20 \」,\ 「狀態\」:\「KERALA \ 「},{\」respcode \「:0,\」Id \「:\」21 \「,\」State \「:\」TAMIL NADU \「},{\」respcode \「:0,\」 \「:\」22 \「,\」State \「:\」ANDHRA PRADESH \「},{\」respcode \「:0,\」Id \「:\」23 \「,\」State \ \ 「曼尼普爾邦\」},{\ 「respcode \」:0,\ 「ID \」:\ 「24 \」,\ 「國家\」:\ 「米佐拉姆\」},{\ 「respcode \」:0, \ 「ID \」:\ 「25 \」,\ 「國家\」:\ 「那加蘭邦\」},{\ 「respcode \」:0,\ 「ID \」:\ 「26 \」,\「國家\ 「:\」ASSAM \「},{\」respcode \「:0,\」Id \「:\」27 \「,\」State \「:\」ARUNACHAL PRADESH \「},{\」respcode \「 :0,\ 「ID \」:\ 「28 \」,\ 「狀態\」:\ 「特里普拉\」},{\ 「respcode \」:0,\ 「ID \」:\ 「29 \」,\ 「國家\」:\ 「梅加拉亞邦\」},{\ 「respcode \」:0,\「我d \ 「:\」 30 \」,\ 「狀態\」:\ 「Pondicherry的\」},{\ 「respcode \」:0,\ 「ID \」:\ 「31 \」,\ 「狀態\」: \ 「UTRAKHAND \」},{\ 「respcode \」:0,\ 「ID \」:\ 「32 \」,\ 「狀態\」:\ 「ONSITE \」},{\ 「respcode \」:0, \「Id \」:\「33 \」,\「State \」:\「LAKSHADWEEP \」},{\「respcode \」:0,\「Id \」:\「34 \」,\「 「:\」ANDAMAN AND NICOBAR ISLANDS \「},{\」respcode \「:0,\」Id \「:\」35 \「,\」State \「:\」DADRA AND NAGAR HAVELI \ 「respcode」:0,\「Id \」:\「36 \」,\「State \」:\「DAMAN AND DIU \」},{\「respcode \」:0,\「Id \」: \ 「38 \」,\ 「國家\」:\ 「特蘭伽納\」}],\ 「respcode \」:0}」

而且我的代碼是:

 string JsonStr = e.Result.ToString(); 

     var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr); 
     MessageBox.Show(container.DDIState.Count.ToString()); // Prints 2 
     MessageBox.Show(container.DDIState[0].State.Length.ToString()); // Prints 3 
     //MessageBox.Show(container.attributes.Count.ToString()); // Prints 2 
     //MessageBox.Show(container.attributes[0].State.Length.ToString()); // Prints 3 

    } 

    public static T DeserializeFromJson<T>(string json) 
    { 
     T deserializedProduct = JsonConvert.DeserializeObject<T>(json); 
     return deserializedProduct; 
    } 

    public class Attributes 
    { 
     public string respcode { get; set; } 
     public string Id { get; set; } 
     public string State { get; set; } 
    } 


    public class DataJsonAttributeContainer 
    { 
     public List<Attributes> DDIState { get; set; } 
    } 

但我不能反序列化JSON對象

回答

0

您在類DataJsonAttributeContainer的屬性名稱中有錯字。 JSON中的屬性名爲DDlState,但在您的班級中名爲DDIStateI - >l)。

+0

我聽不懂。你能解釋一點點嗎? –

+0

JSON的正確類是'public class DataJsonAttributeContainer {public List DDlState {get;組; } }' – Rudis

+0

明白了。工作很好,謝謝 –