Invalidcastexception JsonConvert.DeserializeObject

时间:2016-08-10 16:18:46

标签: c# json xamarin json.net

我收到一个无效的强制转换异常,指出的强制转换是无效的。在这一行:

RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1);

documentation这应该没问题?我可以看到控制台输出正常吗?

  

回复:[{“Height_ft”:2999.0,“Height_m”:914.0,“ID”:“c1”,   “纬度”:57.588007,“经度”:-5.5233564,“名称”:“Beinn Dearg”,   “湿度”:0.81,“snowCover”:4.99,“温度”:63.0}]

        Spinner spinner = (Spinner)sender;
        string urlmountain = "http://removed.azurewebsites.net/api/Mountains?name=";
        JsonValue json1 = FetchMountain(urlmountain+string.Format("{0}", spinner.GetItemAtPosition(e.Position)));
        //below.................................
        RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1); //this line
        string toast = mountain.Name;
        Toast.MakeText(this, toast, ToastLength.Long).Show();

    private JsonValue FetchMountain(string urlmountain)
    {
        // Create an HTTP web request using the URL:
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlmountain));
        request.ContentType = "application/json";
        request.Method = "GET";

        // Send the request to the server and wait for the response:
        using (WebResponse response = request.GetResponse())
        {
            // Get a stream representation of the HTTP web response:
            using (Stream stream = response.GetResponseStream())
            {
                // Use this stream to build a JSON document object:
                JsonValue jsonDoc1 = JsonObject.Load(stream);
                Console.Out.WriteLine("Response: {0}", jsonDoc1.ToString());

                // Return the JSON document:
                return jsonDoc1;
            }
        }
    }
    public class RootObject
    {
        public string ID { get; set; }

        public double? Latitude { get; set; }

        public double? Longitude { get; set; }

        public string Name { get; set; }

        public double? Height_m { get; set; }

        public double? Height_ft { get; set; }

        public double? temperature { get; set; }

        public double? humidity { get; set; }

        public double? snowCover { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

2 个答案:

答案 0 :(得分:4)

返回的json数据是一个对象数组,而不是单个对象,由开括号和右括号[]表示。您需要反序列化为数组或列表:

var mountains = JsonConvert.DeserializeObject<List<RootObject>>(json);

要从反序列化的有效负载访问第一个山,请使用.FirstOrDefault()

var mountain = mountains.FirstOrDefault();
if (mountain != null)
{
    string toast = mountain.Name;
    Toast.MakeText(this, toast, ToastLength.Long).Show();
}

答案 1 :(得分:3)

看起来你的JSON是一个对象数组。您应该能够反序列化数组并获得第一个数组:

.foo, .bar, .dan{
width:20%;
display: inline-block;
overflow: hidden;
vertical-align:top;
transition: height 1s ease-out;
}

.active {
height:50px;
}

.foo {
background:rgb(150, 100, 100);
}

.bar {
background:rgb(150, 150, 100);
}

.dan {
background:rgb(100, 150, 100);
}

有一点需要注意的是,你在这里混合技术。 <div class="foo"> <p>Works.</p> <p>Another paragraph.</p> <p>Third paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> </div> <div class="bar"> <p>Works.</p> <p>Another paragraph.</p> <p>Third paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> <p>Fourth paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> </div> <div class="dan"> <p>Works.</p> <p>Another paragraph.</p> <p>Third paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> <p>Fourth paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> <p>Fifth paragraph. This one is even longer. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, fugiat facere? Animi, odit et tempore debitis modi quae eaque, libero, dolores magni, voluptas tenetur tempora quidem alias ut praesentium sed.</p> </div>来自RootObject mountain = JsonConvert.DeserializeObject<RootObject[]>(json1)[0]; 命名空间,而JsonValue来自System.Json(即JSON.Net)命名空间。如果你想严格遵守JSON.Net,你可以这样做:

JsonConvert