我正在使用 ServiceStack.Text 来解析 WorldWeatherOnline's Marine Api 。
反序列化 JSON 时,库会错误地解析 JSON,如下图第二列所示
这是 JSON 的一部分(为简洁起见,进行了剪裁)
{
"data":{
"nearest_area":[
{
"distance_miles":"36.8",
"latitude":"53.965",
"longitude":"0.456"
}
]
}
}
这是我试图将其反序列化的类
public class Weather
{
public NearestArea NearestArea { get; set; }
}
public class NearestArea
{
public double? RetLatitude { get; set; }
public double? RetLongitude { get; set; }
public double? MilesFromReq { get; set; }
}
这是进行反序列化的代码
Weather result = JsonObject.Parse(content).Object("data").ConvertTo(x=> new Weather{
NearestArea = x.Object("nearest_area").ConvertTo(n => new NearestArea{
MilesFromReq = Convert.ToDouble(n.Get("distance_miles")),
RetLatitude = Convert.ToDouble(n.Get ("latitude")),
RetLongitude = Convert.ToDouble(n.Get ("longitude"))
})
有人能发现问题吗?
最佳答案
下面的代码应该可以工作...
var weather = ServiceStack.Text.JsonSerializer.DeserializeFromString<RootWeather>(content);
public class RootWeather
{
public Weather data { get; set; }
}
public class Weather
{
public List<NearestArea> nearest_area { get; set; }
}
public class NearestArea
{
public string latitude { get; set; }
public string longitude { get; set; }
public string distance_miles { get; set; }
}
关于c# - 使用 ServiceStack.Text 解析 JSON 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103270/