简体   繁体   中英

Failed to Parse JSON in Windows Phone

I'm trying to parse a Json. I have successfully passed the Json onto string, but I can't convert it onto JObject. Here is my attempt code:

private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   string jsonStr = e.Result;
   if (!string.IsNullOrEmpty(jsonStr))
   {
      JObject objects = JObject.Parse(jsonStr); // this is when the error came at the first time. It says: An exception of type 'Newtonsoft.Json.JsonReaderException' occured in Newtonsoft.Json.DLL but was not handled in user code.
      JArray a = (JArray)objects[""];
      IList<Feeds.Topic> listFeeds = a.ToObject<IList<Feeds.Topic>>();
      this.DataContext = listFeeds;
   }
}

And here is the source of JSON: http://apibiru.herokuapp.com/v0.1/feeds/1?auth_token=64d362d2e483e8023c46595f83ca8d9555ff6d7cc700a2474fbdbd341c43c1fb

I appreciate your help if you can help me, thanks :)

Try this instead:

JArray a = JArray.Parse(jsonStr);

Don't try to parse into a JObject first as your data is an array as it starts with [ and ends with ].

you can use

JArray a = JArray.Parse(jsonStr);

I tried to parse your json using the Class that got from http://json2csharp.com/ . I think you have to parse it directly

List<RootObject> yourObject= JsonConvert.DeserializeObject<List<RootObject>>(jsonStr);

Since you need Json Object you can try the first option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM