2014-04-03 31 views
0

在反序列化對象時需要幫助。我在C#中新的,所以我希望你不要評判嚴格)在C中反序列化JSON時出現問題#

{"getappdata": 
    {"general": 
     {"message":""}, 
     "pool": 
      {"hashrate":6699750, 
      "workers":8873, 
      "basis_pps":0.132769, 
      "alt_pps":0, 
      "alt_bonus":0}, 
     "ltc_exchange_rates": 
      {"USD":"13", 
      "EUR":"9.69"}, 
     "user": 
      {"username":"overstorm", 
      "balance":4.63247039, 
      "hashrate":0, 
      "sharerate":0, 
      "invalid_share_rate":0}, 
     "worker":[ 
      {"name":"overstorm.1", 
      "hashrate":0,"active":0, 
      "monitoring":1}, 
      {"name":"overstorm.2", 
      "hashrate":0,"active":0, 
      "monitoring":1}], 
     "earnings": 
      {"basis":[], 
      "alt":[], 
      "24h_total":0, 
      "24h_basis":0, 
      "24h_alt":0, 
      "24h_affiliate":0, 
      "48h_total":0, 
      "48h_basis":0, 
      "48h_alt":0, 
      "48h_affiliate":0} 
    } 
} 

我創建看起來像這樣的類:

[Serializable] 
     public class GetAppData 
     { 
      [JsonProperty(PropertyName = "general")] 
      public General general { get; set; } 




      [Serializable] 
      public class General 
      { 
       [JsonProperty(PropertyName = "message")] 
       public string msg { get; set; } 
       [JsonProperty(PropertyName = "pool")] 
       public Pool pool { get; set; } 
       [JsonProperty(PropertyName = "ltc_exchange_rates")] 
       public Erates erates { get; set; } 
       [JsonProperty(PropertyName = "user")] 
       public User user { get; set; } 
       [JsonProperty(PropertyName = "worker")] 
       public IList<Worker> workers { get; set; } 
       [JsonProperty(PropertyName = "earnings")] 
       public Earnings earnings { get; set; } 
       public T DeSerializeData<T>(string t) 
       { 
        return (new JavaScriptSerializer().Deserialize<T>(t)); 
       } 

       [Serializable]     
       public class Msg 
       { 
        public string msg { get; set; } 
        public T DeSerializeData<T>(string t) 
        { 
         return (new JavaScriptSerializer().Deserialize<T>(t)); 
        } 
       } 


       [Serializable] 
       public class Pool 
       { 
        [JsonProperty(PropertyName = "hashrate")] 
        public int hashrate { get; set; } 
        [JsonProperty(PropertyName = "workers")] 
        public int Workers { get; set; } 
        [JsonProperty(PropertyName = "basis_pps")] 
        public double basis_pps { get; set; } 
        [JsonProperty(PropertyName = "alt_pps")] 
        public double alt_pps { get; set; } 
        [JsonProperty(PropertyName = "alt_bonus")] 
        public double alt_bonus { get; set; } 
       } 

       [Serializable] 
       public class Erates 
       { 
        [JsonProperty(PropertyName = "USD")] 
        public double USD { get; set; } 
        [JsonProperty(PropertyName = "EUR")] 
        public double EUR { get; set; } 

        public T DeSerializeData<T>(string t) 
        { 
         return (new JavaScriptSerializer().Deserialize<T>(t)); 
        } 
       } 

       [Serializable] 
       public class User 
       { 
        [JsonProperty(PropertyName = "username")] 
        public string Username { get; set; } 
        [JsonProperty(PropertyName = "balance")] 
        public double Balance { get; set; } 
        [JsonProperty(PropertyName = "hashrate")] 
        public double Hashrate { get; set; } 
        [JsonProperty(PropertyName = "sharerate")] 
        public double Sharerate { get; set; } 
        [JsonProperty(PropertyName = "invalid_share_rate")] 
        public double Invalid_Share_Rates { get; set; } 

        public T DeSerializeData<T>(string t) 
        { 
         return (new JavaScriptSerializer().Deserialize<T>(t)); 
        } 
       } 

       [Serializable] 
       public class Worker 
       { 
        [JsonProperty(PropertyName = "name")] 
        public string Name { get; set; } 
        [JsonProperty(PropertyName = "hashrate")] 
        public int hashrate { get; set; } 
        [JsonProperty(PropertyName = "active")] 
        public string active { get; set; } 
        [JsonProperty(PropertyName = "monitoring")] 
        public string monitoring { get; set; } 

        public T DeSerializeData<T>(string t) 
        { 
         return (new JavaScriptSerializer().Deserialize<T>(t)); 
        } 
       } 

       [Serializable] 
       public class Earnings 
       { 
        [JsonProperty(PropertyName = "basis")] 
        public IList<string> basis { get; set; } 
        [JsonProperty(PropertyName = "alt")] 
        public IList<string> alt { get; set; } 
        [JsonProperty(PropertyName = "24h_total")] 
        public string DayTotal { get; set; } 
        [JsonProperty(PropertyName = "24h_basis")] 
        public string DayBasis { get; set; } 
        [JsonProperty(PropertyName = "24h_alt")] 
        public string DayAlt { get; set; } 
        [JsonProperty(PropertyName = "24h_affiliate")] 
        public string DayAffiliate { get; set; } 
        [JsonProperty(PropertyName = "48h_total")] 
        public string TwoDaysTotal { get; set; } 
        [JsonProperty(PropertyName = "48h_basis")] 
        public string TwoDaysBasis { get; set; } 
        [JsonProperty(PropertyName = "48h_alt")] 
        public string TwoDaysAlt { get; set; } 
        [JsonProperty(PropertyName = "48h_affiliate")] 
        public string TwoDaysAffiliate { get; set; } 

        public T DeSerializeData<T>(string t) 
        { 
         return (new JavaScriptSerializer().Deserialize<T>(t)); 
        } 
       } 
      } 


      public T DeSerializeData<T>(string t) 
      { 
       return (new JavaScriptSerializer().Deserialize<T>(t)); 
      } 
     } 

這就是我如何嘗試反序列化:

GetAppData GAD = new GetAppData(); 
    GAD = jss.Deserialize<GetAppData>(jsonString); 

而這正是我的GAD看起來像

{JSON_Test.Form1.GetAppData} 

請幫忙)Google已經厭倦了我)

+0

你爲什麼要創建一個新的'GetAppData'如果你即將用別的東西覆蓋'GAD'的值?爲什麼不只是'GetAppData appData = jss.Deserialize (jsonString)'?你的意思是「這就是我的GAD的樣子」? –

+0

「我的GAD看起來像」是對象的價值。代碼中有很多錯誤。我還在學習。對不起 – user3493623

+0

什麼*確切*你的意思是「對象的價值」?你剛纔叫了'ToString'嗎?在調試器中檢查它?我懷疑你會發現,這只是因爲你沒有重寫'ToString' ...在調試器中展開對象,你將能夠看到所有的屬性等。 –

回答

1

看來你誤解了你的JSON結構。爲了更好地理解,您可以使用json viever。您還可以使用json2csharp.com/基於JSON樣本

你的數據結構應該(用Newtonsoft JSON)生成你幾乎數據類:

Newtonsoft.Json.JsonConvert.DeserializeObject<JSON>(s); 


    public class JSON 
    { 
     [JsonProperty(PropertyName = "getappdata")] 
     public GetAppData getappdata { get; set; } 
    } 

    [Serializable] 
    public class General 
    { 
     [JsonProperty(PropertyName = "message")] 
     public string msg { get; set; } 
    } 

    [Serializable] 
    public class GetAppData 
    { 
     [JsonProperty(PropertyName = "general")] 
     public General general { get; set; } 
     [JsonProperty(PropertyName = "pool")] 
     public Pool pool { get; set; } 
     [JsonProperty(PropertyName = "ltc_exchange_rates")] 
     public Erates erates { get; set; } 
     [JsonProperty(PropertyName = "user")] 
     public User user { get; set; } 
     [JsonProperty(PropertyName = "worker")] 
     public IList<Worker> workers { get; set; } 
     [JsonProperty(PropertyName = "earnings")] 
     public Earnings earnings { get; set; } 
    } 

    [Serializable]     
    public class Msg 
    { 
     public string msg { get; set; } 

    } 


    [Serializable] 
    public class Pool 
    { 
     [JsonProperty(PropertyName = "hashrate")] 
     public int hashrate { get; set; } 
     [JsonProperty(PropertyName = "workers")] 
     public int Workers { get; set; } 
     [JsonProperty(PropertyName = "basis_pps")] 
     public double basis_pps { get; set; } 
     [JsonProperty(PropertyName = "alt_pps")] 
     public double alt_pps { get; set; } 
     [JsonProperty(PropertyName = "alt_bonus")] 
     public double alt_bonus { get; set; } 
    } 

    [Serializable] 
    public class Erates 
    { 
     [JsonProperty(PropertyName = "USD")] 
     public double USD { get; set; } 
     [JsonProperty(PropertyName = "EUR")] 
     public double EUR { get; set; } 


    } 

    [Serializable] 
    public class User 
    { 
     [JsonProperty(PropertyName = "username")] 
     public string Username { get; set; } 
     [JsonProperty(PropertyName = "balance")] 
     public double Balance { get; set; } 
     [JsonProperty(PropertyName = "hashrate")] 
     public double Hashrate { get; set; } 
     [JsonProperty(PropertyName = "sharerate")] 
     public double Sharerate { get; set; } 
     [JsonProperty(PropertyName = "invalid_share_rate")] 
     public double Invalid_Share_Rates { get; set; } 


    } 

    [Serializable] 
    public class Worker 
    { 
     [JsonProperty(PropertyName = "name")] 
     public string Name { get; set; } 
     [JsonProperty(PropertyName = "hashrate")] 
     public int hashrate { get; set; } 
     [JsonProperty(PropertyName = "active")] 
     public string active { get; set; } 
     [JsonProperty(PropertyName = "monitoring")] 
     public string monitoring { get; set; } 


    } 

    [Serializable] 
    public class Earnings 
    { 
     [JsonProperty(PropertyName = "basis")] 
     public IList<string> basis { get; set; } 
     [JsonProperty(PropertyName = "alt")] 
     public IList<string> alt { get; set; } 
     [JsonProperty(PropertyName = "24h_total")] 
     public string DayTotal { get; set; } 
     [JsonProperty(PropertyName = "24h_basis")] 
     public string DayBasis { get; set; } 
     [JsonProperty(PropertyName = "24h_alt")] 
     public string DayAlt { get; set; } 
     [JsonProperty(PropertyName = "24h_affiliate")] 
     public string DayAffiliate { get; set; } 
     [JsonProperty(PropertyName = "48h_total")] 
     public string TwoDaysTotal { get; set; } 
     [JsonProperty(PropertyName = "48h_basis")] 
     public string TwoDaysBasis { get; set; } 
     [JsonProperty(PropertyName = "48h_alt")] 
     public string TwoDaysAlt { get; set; } 
     [JsonProperty(PropertyName = "48h_affiliate")] 
     public string TwoDaysAffiliate { get; set; } 


    } 
+0

非常感謝yoou)正是我所需要的) – user3493623