从JsonReader读取JObject时出错.当前JsonReader项不是对象:StartArray.小路 [英] Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path

查看:2235
本文介绍了从JsonReader读取JObject时出错.当前JsonReader项不是对象:StartArray.小路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理涉及位置的Windows Phone 8.1应用程序.我正在从我的API接收Json数据.我的API返回的数据如下:

I am working on a Windows Phone 8.1 application involving location. I am receiving Json data from my API. My API returns data that looks like:

[{
    "country": "India",
    "city": "Mall Road, Gurgaon",
    "area": "Haryana",
    "PLZ": "122002",
    "street": "",
    "house_no": "",
    "POI": "",
    "type": "17",
    "phone": "",
    "lng": 77.08972334861755,
    "lat": 28.47930118040612,
    "formatted_address": "Mall Road, Gurgaon 122002, Haryana, India"
},
{
    "country": "India",
    "city": "Mall Road, Kanpur",
    "area": "Uttar Pradesh",
    "PLZ": "208004",
    "street": "",
    "house_no": "",
    "POI": "",
    "type": "17",
    "phone": "",
    "lng": 80.35783410072327,
    "lat": 26.46026740300029,
    "formatted_address": "Mall Road, Kanpur 208004, Uttar Pradesh, India"
},
{
    "country": "India",
    "city": "Mall Road Area, Amritsar",
    "area": "Punjab",
    "PLZ": "143001",
    "street": "",
    "house_no": "",
    "POI": "",
    "type": "17",
    "phone": "",
    "lng": 74.87286686897278,
    "lat": 31.64115178002094,
    "formatted_address": "Mall Road Area, Amritsar 143001, Punjab, India"
},
{
    "country": "India",
    "city": "Vasant Kunj (Mall Road Kishan Garh), New Delhi",
    "area": "Delhi",
    "PLZ": "110070",
    "street": "",
    "house_no": "",
    "POI": "",
    "type": "18",
    "phone": "",
    "lng": 77.1434211730957,
    "lat": 28.51363217008815,
    "formatted_address": "Vasant Kunj (Mall Road Kishan Garh), New Delhi 110070, Delhi, India"
}]

我正在反序列化我的Json数据并将其放入名为LocationData的类中.当我运行代码时,它给我一个错误:

I am deserializing my Json data and putting it into a class named LocationData. When I run my code, it gives me an error:

从JsonReader读取JObject时出错.当前JsonReader项不是对象:StartArray.路径

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path

我要去哪里错了?这是我的代码:

Where am I going wrong? Here is my code:

private async void GetAPIData()
    {
        string _serviceUrl = "https://api.myweblinkapiprovider/v2&q=" + UserRequestedLocation;
        HttpClient client = new HttpClient();

        HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));

        if (responce.Content != null)
        {
            var respArray = JObject.Parse(await responce.Content.ReadAsStringAsync());
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            var rcvdData = JsonConvert.DeserializeObject<LocationData>(respArray.ToString(), settings);
            UpdateMapData(rcvdData);
            UpdateTextData(rcvdData);
        }
    }

我也尝试使用JArray.我的代码如下:

I also tried to use a JArray. My code is as below:

 private async void GetAPIData()
    {
        string _serviceUrl = "https://api.myweblinkprovider.com/v3?fun=geocode&lic_key=MyKey" + UserRequestedLocation;
        HttpClient client = new HttpClient();

        HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));

        JArray arr = JArray.Parse(await responce.Content.ReadAsStringAsync());

        foreach (JObject obj in arr.Children<JObject>())
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            var rcvdData = JsonConvert.DeserializeObject<LocationData>(arr.ToString(), settings);
            UpdateMapData(rcvdData);
            UpdateTextData(rcvdData);
        }
    }

这也给我一个错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型'MMI_SpeechRecog.Model.LocationData',因为该类型需要JSON对象(例如{"name":"value"})才能正确反序列化.

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MMI_SpeechRecog.Model.LocationData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

推荐答案

问题的第一部分是

The first part of your question is a duplicate of Why do I get a JsonReaderException with this code?, but the most relevant part from that (my) answer is this:

[A] JObject不是JSON.net中所有内容的基本基本类型,但JToken是.因此,即使您可以说,

[A] JObject isn't the elementary base type of everything in JSON.net, but JToken is. So even though you could say,

object i = new int[0];

在C#中,你不能说

JObject i = JObject.Parse("[0, 0, 0]");

在JSON.net中.

in JSON.net.

您想要的是JArray.Parse,它将接受您传递的数组(由API响应中的开头[表示).这就是错误消息中的"StartArray"告诉您的内容.

What you want is JArray.Parse, which will accept the array you're passing it (denoted by the opening [ in your API response). This is what the "StartArray" in the error message is telling you.

关于使用JArray时发生的情况,您使用的是arr而不是obj:

As for what happened when you used JArray, you're using arr instead of obj:

var rcvdData = JsonConvert.DeserializeObject<LocationData>(arr /* <-- Here */.ToString(), settings);

交换它,我相信它应该起作用.

Swap that, and I believe it should work.

尽管我很想直接将arr反序列化为IEnumerable<LocationData>,这样可以节省一些代码和遍历数组的工作.如果您不想单独使用已解析的版本,则最好避免使用它.

Although I'd be tempted to deserialize arr directly as an IEnumerable<LocationData>, which would save some code and effort of looping through the array. If you aren't going to use the parsed version separately, it's best to avoid it.

这篇关于从JsonReader读取JObject时出错.当前JsonReader项不是对象:StartArray.小路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆