需要帮助反序列化以下对对象的响应。我是c#的新手,所以我希望你不要严格判断)
{"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 :(得分: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; }
}