c# - 使用 ServiceStack.Text 解析 JSON 时出现问题

标签 c# .net json servicestack

我正在使用 ServiceStack.Text 来解析 WorldWeatherOnline's Marine Api

反序列化 JSON 时,库会错误地解析 JSON,如下图第二列所示

enter image description here

这是 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/

相关文章:

c# - C# 插件功能的模式/方法

c# - 何时使用 "await"关键字

c# - 我如何为经典 ASP 创建一个不连续阻塞其他线程的 DOTNET COM 互操作程序集?

.net - 找不到与某些项目的目标运行时之一兼容的框架 .NETCoreApp=v1 的运行时目标

javascript - 如何使用 Ajax 将变量从 PHP 发送到 javascript?

javascript - 检查 Titanium 中的空 json

javascript - XMLHttpRequest 发送 Json 对象时返回状态 0

c# - 通用 DbDataReader 到 List<T> 的映射

c# - 在 ASP.NET MVC 中将属性类型设置为另一个 ViewModel 的 ViewModel 是一种不好的做法吗

c# - WMI 性能不佳