我正在开发一个涉及位置的 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.
你的问题的第一部分是 为什么我会用这个代码得到一个 JsonReaderException?,但是这个(我的)答案中最相关的部分是:
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, butJToken
is. So even though you could say,
object i = new int[0];
在 C# 中,你不能说,
in C#, you can't say,
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.小路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!