1

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 ?

3
  • Are you sure that postData has string type and thus JObject.Parse(postData) is required? If you really need to make JObject.Parse(postData) then you still need to make second parsing dynamic filters =JObject.Parse(jObj.filters); instead of var filters = jObj.filters; Commented Apr 13, 2017 at 7:46
  • Thanks Oleg ! :) Commented Apr 13, 2017 at 7:54
  • You are welcome! Commented Apr 13, 2017 at 7:54

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

JSON string is coming from jqGrid, im not doing any modification with the string before sending it to C#. So string format should be correct. What are the mistakes in the filter field? Filter field i'm able to get (as i said in the main topic). Actually i'm not able to get "rules" field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.