Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken

Tags:

json.net

jira

I'm attempting to build off of Mike Jansen's JIRA REST Client, and I'm trying to pull in JIRA version information. I'm new to JSON, so I'm not sure if it's just a formatting issue or what.

When debugging, I have the following token:

{[
  {
    "self": "https://<company>.atlassian.net/rest/api/2/version/10101",
    "id": "10101",
    "name": "2012.3",
    "archived": false,
    "released": false,
    "releaseDate": "2012-10-08"
  },
  {
    "self": "https://<company>.atlassian.net/rest/api/2/version/10200",
    "id": "10200",
    "name": "2012.4",
    "archived": false,
    "released": false
  }
]}

and the following line of code

token.Children().Values<T>()

is throwing the following error

Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken

while trying to convert the two version tokens into the corresponding JiraVersion class:

using System;
namespace JiraRestClient
{
    public class JiraVersion : JiraObjectBase, IJiraVersion
    {
        public JiraVersion(IJiraRestResponse jiraResponse) : base(jiraResponse) { }
        public string Id { get { return Get<string>("Id", "id"); } }
        public string Name { get { return Get<string>("Name", "name"); } }
    }
}

Can somebody help me out?

like image 474
gopherr Avatar asked Dec 05 '25 12:12

gopherr


1 Answers

Those of you familiar with JSON may have noticed quickly that it was, in fact, a problem with the formatting (the extra curly brackets enclosing the array). Like I said, I'm new to JSON, but the end result of my research is that the JsonWrapper.TryGetPath(...) method attempts to traverse the JObject tree and does not produce correctly formatted JSON when what's being retrieved is an array.

My solution was to simplify things by removing JSON.Net from the solution and depending only on RestSharp (just because it makes it so easy to make requests) and the System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(response.Content) approach.

like image 70
gopherr Avatar answered Dec 09 '25 19:12

gopherr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!