stianboe
stianboe

Reputation: 441

Linq JObject query

I have some problems with jobject and jarray linq query. I get this error:

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type     'Newtonsoft.Json.Linq.JArray'.

My code:

string fetchResult = JsonConvert.SerializeObject(sidebar, Formatting.Indented);
JObject rss = JObject.Parse(fetchResult);

var jsonModel = from item in (JArray)rss["RegistrationCase"]
                        select new DataList
                        {
                            RegistrationTypeName = item["RegistrationTypeName"].Value<string>(), };

If i remove (Jarray) i get: Cannot access child value on Newtonsoft.Json.Linq.JProperty.

Json, jobject: e.g: i want RegistrationTypeName, FirstName and the value of JournalNumber.

{
"Status": null,
"RegistrationCase": {
"RegistrationTypeName": " ",
"ExpireDate": null,
"PersonId": 7,
"Person": {
  "FirstName": " ",
  "GenderValue": 2,
  "Gender": 2,
},
"UserId": 7,
"User": {
  "UserName": "NO-DOM\\wme",
  "LastName": null,
  "Id": 7,
},
"Transactions": [],
"Comments": [],
"CustomData": [
  {
    "Key": "JournalNumber",
    "Value": "0654-84148-00000-25",
    "Id": 3,
  },
  {
    "Key": "IsConsentGiven",
    "Value": "False",
    "Id": 4,
  },
  {
],
"FileId": null,
"File": null,
"Id": 7,
 }
}

Upvotes: 1

Views: 5456

Answers (1)

AKD
AKD

Reputation: 3966

u can get these values directly like :

   var RegistrationTypeName = rss["RegistrationCase"]["RegistrationTypeName"];
   var FirstName = rss["RegistrationCase"]["Person"]["FirstName"];
   var JournalNumber = rss["RegistrationCase"]["CustomData"][0]["Value"];

Upvotes: 2

Related Questions