c# - Json反序列化异常

标签 c# .net json windows-phone-8 json.net

我尝试运行以下代码,但出现异常:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL but was not handled in user code

Additional information: Error converting value "country" to type 'LocalGasPrices.Station'. Path '', line 1, position 9.

        var jsonObj = JObject.Parse(URL);
        var results = jsonObj["stations"].Children().Values();
        var details = new List<Station>();

        foreach (JToken result in results)
        {
            var st = result.ToString();
            var searchResult = JsonConvert.DeserializeObject<Station>(st);
            details.Add(searchResult);

        }

        foreach (Station s in details)
        {
            this.txtDebug.Text += s.address;

        }

这里是 JsonDeserialization 的类:

public class Status
{
    public string error { get; set; }
    public int code { get; set; }
    public string description { get; set; }
    public string message { get; set; }
}

public class GeoLocation
{
    public string city_id { get; set; }
    public string city_long { get; set; }
    public string region_short { get; set; }
    public string region_long { get; set; }
    public string country_long { get; set; }
    public string country_id { get; set; }
    public string region_id { get; set; }
}

public class Station
{
    public string country { get; set; }
    public string price { get; set; }
    public string address { get; set; }
    public string diesel { get; set; }
    public string id { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string station { get; set; }
    public string region { get; set; }
    public string city { get; set; }
    public string date { get; set; }
    public string distance { get; set; }
}

public class RootObject
{
    public Status status { get; set; }
    public GeoLocation geoLocation { get; set; }
    public List<Station> stations { get; set; }
}

这是 JSON 响应的示例:

 {
    "status": {
        "error": "NO",
        "code": 200,
        "description": "none",
        "message": "Request ok"
    },
    "geoLocation": {
        "city_id": "147",
        "city_long": "Saint-Laurent",
        "region_short": "QC",
        "region_long": "Quebec",
        "country_long": "Canada",
        "country_id": "43",
        "region_id": "35"
    },
    "stations": [
        {
            "country": "Canada",
            "price": "3.65",
            "address": "3885, Boulevard Saint-Rose",
            "diesel": "0",
            "id": "33862",
            "lat": "45.492367",
            "lng": "-73.710915",
            "station": "Shell",
            "region": "Quebec",
            "city": "Saint-Laurent",
            "date": "3 hours agp",
            "distance": "1.9km"
        }
    ]
}

最佳答案

我相信您在结果中不需要 .Values(),.Values() 选择了该站的属性。

var results = jsonObj["stations"].Children();

关于c# - Json反序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075905/

相关文章:

c# List<Items> with DateTime 获取过去 6 周的所有内容

c# - 无法确定元表

.net - 64 位 CLR 可以使用压缩指针吗?

java - 在 Java 中 - 如何将 resultSet 映射到复杂对象?

c# - 实用程序类与子类化 .net 控件

c# - 当服务器关闭或重新启动时,SignalR 客户端不会调用重新连接事件

c# - 带有 retrofit 的多部分内容

c# - 从数据库中获取数据的问题

java - 使用 Jackson 和 Json 时出错

javascript - 将 LinkedIn API 响应正文解析为 JSON