Joe M
Joe M

Reputation: 13

Get jqGrid ajax Nested Array of Json string in C# by Newtonsoft Json

I'm trying to parse Json string and collect array values present inside it.

{"_search":true,"nd":1492064211841,"rows":30,"page":1,"sidx":"","sord":"asc","filters":"{\"groupOp\":\"OR\",\"rules\":[{\"field\":\"Emp_ID\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Name\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Designation\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"City\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"State\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Country\",\"op\":\"cn\",\"data\":\"ASAS\"}]}"}

PS: Above string is coming from jqGrid Ajax to the WebMethod in C#.

I'm not getting success on getting filters->rules[0]->data

What I've tried :

dynamic jObj = JObject.Parse(postData);
var data = jObj.filters.rules[0].data;

getting error: 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'rules'.

dynamic jObj = JObject.Parse(postData);
var filters = jObj.filters; //Sucess: getting filters here
var rules1 = filters["rules"]; //Error: 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'rules'.
var rules2 = filters.rules;  //Error: 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'rules'.

How to get value inside filters->rules AND filters->rules[0]->data ?

Upvotes: 1

Views: 300

Answers (2)

Quentin Roger
Quentin Roger

Reputation: 6538

You must parse the internal object like this :

var obj = "{\"_search\":true,\"nd\":1492064211841,\"rows\":30,\"page\":1,\"sidx\":\"\",\"sord\":\"asc\",\"filters\":\"{\\\"groupOp\\\":\\\"OR\\\",\\\"rules\\\":[{\\\"field\\\":\\\"Emp_ID\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"},{\\\"field\\\":\\\"Name\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"},{\\\"field\\\":\\\"Designation\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"},{\\\"field\\\":\\\"City\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"},{\\\"field\\\":\\\"State\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"},{\\\"field\\\":\\\"Country\\\",\\\"op\\\":\\\"cn\\\",\\\"data\\\":\\\"ASAS\\\"}]}\"}";
dynamic jObj = JObject.Parse(obj);
var data = JObject.Parse(jObj.filters.Value);
var test = data.rules;
Console.WriteLine(data);
Console.ReadLine();

Upvotes: 1

Man Programmer
Man Programmer

Reputation: 5356

I don't have the knowledge about C# but I have tried into it on JavaScript

In your json, in filters field not is'nt proper json it is string

I have do this on javascript might it will help's you

var a = {"_search":true,"nd":1492064211841,"rows":30,"page":1,"sidx":"","sord":"asc","filters":"{\"groupOp\":\"OR\",\"rules\":[{\"field\":\"Emp_ID\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Name\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Designation\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"City\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"State\",\"op\":\"cn\",\"data\":\"ASAS\"},{\"field\":\"Country\",\"op\":\"cn\",\"data\":\"ASAS\"}]}"} 

console.log(a.filters)

it is returns

"{"groupOp":"OR","rules":[{"field":"Emp_ID","op":"cn","data":"ASAS"},{"field":"Name","op":"cn","data":"ASAS"},{"field":"Designation","op":"cn","data":"ASAS"}

and it is string now i'm again parse into it on JSON

b = JSON.parse(a.filters)
console.log(b.rules)

now it returns rules objects

Upvotes: 1

Related Questions