Как правильно десериализовать ответ в Json - C#
Формулировка задачи:
Как правильно десериализовать ответ в Json (С вложенными классами) используя библиотеку Newtonsoft.Json?
Генерирую классы на сайте http://json2csharp.com/
Генерирую классы на сайте http://json2csharp.com/
Поучая ответы в Json пытаюсь десериализовать с помощью классов
JsonResultWrapper<T>
и JsonResultArrayWrapper<T> соответственно
var result = JsonConvert.DeserializeObject<JsonResultWrapper<Data>>(response);
Но ничего не получается( Как правильно десериализовать с "вложенными классами"? (т.е. когда бывают массивы или класс в классе)
{
"success": true,
"code": "OK",
"msg": "Operation succeeded.",
"data": {
"coinType": "KCS",
"trading": true,
"lastDealPrice": 5040,
"buy": 5000,
"sell": 5040,
"coinTypePair": "BTC",
"sort": 0,
"feeRate": 0.001,
"volValue": 308140577,
"high": 6890,
"datetime": 1506050394000,
"vol": 5028739175025,
"low": 5040,
"changeRate": -0.2642
}
}public class Data
{
public string coinType { get; set; }
public bool trading { get; set; }
public int lastDealPrice { get; set; }
public int buy { get; set; }
public int sell { get; set; }
public string coinTypePair { get; set; }
public int sort { get; set; }
public double feeRate { get; set; }
public int volValue { get; set; }
public int high { get; set; }
public long datetime { get; set; }
public long vol { get; set; }
public int low { get; set; }
public double changeRate { get; set; }
}
}{
"success": true,
"code": "OK",
"msg": "Operation succeeded.",
"data": {
"_comment": "arr[0] Price arr[1] Amount arr[2] Volume",
"SELL": [
[
20,
5,
100
],
[
19,
5,
95
]
],
"BUY": [
[
18,
5,
90
],
[
17,
5,
85
]
]
}
}public class Data
{
public string _comment { get; set; }
public List<List<int>> SELL { get; set; }
public List<List<int>> BUY { get; set; }
}
} public class JsonResultWrapper<T>
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("code")]
public string code { get; set; }
[JsonProperty("msg")]
public string Message { get; set; }
[JsonProperty("data")]
public T Result { get; set; }
} public class JsonResultArrayWrapper<T>
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("code")]
public string code { get; set; }
[JsonProperty("msg")]
public string Message { get; set; }
[JsonProperty("data")]
public IEnumerable<T> data { get; set; }
}Решение задачи: «Как правильно десериализовать ответ в Json»
textual
Листинг программы
var url = "https://maps.googleapis.com/maps/api/distancematrix/json?" + "origins=" + city1 + "&destinations=" + city2 + "&key=" + GOOGLE_API_KEY; var json = _webClient.DownloadString(url); var distanceInfo = JsonConvert.DeserializeObject<GoogleDistanceInfo>(json);