Json到C#double数据类型值returing 0

时间:2017-09-09 13:50:04

标签: c# asp.net

"{\"code\":200,\"status\":\"success\",\"message\":\"Live Tracking data fetched Successfully.\",\"data\":{\"location\":{\"latitude\":19.208087,\"longitude\":73.103965,\"speed_mps\":0,\"bearing\":0,\"altitude\":0,\"timestamp\":1504961984900},\"status\":\"offline\"}}"

以上是我的json输出

我希望这是一个字符串和拆分格式 哪里, 纬度= 19.208087 经度= 73.103965 等等

public class Location
{

    public double latitude { get; set; }
    public double longitude { get; set; }
    public int speed_mps { get; set; }
    public int bearing { get; set; }
    public int altitude { get; set; }
    public long timestamp { get; set; }
}

public class Data
{
    public Location location { get; set; }
    public string status { get; set; }
}

public class RootObject
{
    public int code { get; set; }
    public string status { get; set; }
    public string message { get; set; }
    public Data data { get; set; }
}

反序列化代码:

var client = new WebClient(); var text = 
client.DownloadString("app.rollr.io:4032/…); 
Location Loc = JsonConvert.DeserializeObject<Location>(text); 
Response.Write("Location" + Loc.longitude);
Response.Write("Location" + Loc.latitude);

问题在于我为不同的字段获得0

1 个答案:

答案 0 :(得分:0)

反序列化时使用 RootObject ...

var root = JsonConvert.DeserializeObject<RootObject>(text);

访问位置:root.data.location