Learn how to retrieve a jqGrid ajax Nested Array of Json string in C# using Newtonsoft Json

I am attempting to parse a JSON string and extract the array values within 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: The string above is being passed from jqGrid Ajax to the WebMethod in C#.

I am facing challenges in extracting the value of filters->rules[0]->data.

What I have attempted :

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

Error received: 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'rules'.

dynamic jObj = JObject.Parse(postData);
var filters = jObj.filters; //Successful: able to retrieve 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 can I access the value within filters->rules AND filters->rules[0]->data ?

Answer №1

Below is an example of how to parse the internal object:

var innerObj = "{\"_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 jsonObject = JObject.Parse(innerObj);
var dataObject = JObject.Parse(jsonObject.filters.Value);
var rules = dataObject.rules;
Console.WriteLine(dataObject);
Console.ReadLine();

Answer №2

While C# isn't my expertise, I have dabbled in JavaScript

In the JSON provided, the filters field is not properly formatted as it is currently a string

I attempted to rectify this using JavaScript; perhaps this solution could be of assistance to 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)

The output shows:

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

This format is currently a string, so I re-parse it into JSON

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

Now, the result displays rules objects as intended

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using the Attribute ID Selector to Identify Field Values in Acumatica's Custom Fields

I have added a custom line level field to the AP303000 form and now I am looking to implement an Attribute ID selector for it. namespace MidayaCustomizations { public class APTranExt : PXCacheExtension<PX.Objects.AP.APTran> { #region ...

Troubleshooting problem with Material-UI and Next.JS in Webpack

I have encountered an error while trying to add the code from this file: https://github.com/mui-org/material-ui/blob/master/examples/nextjs-with-styled-components-typescript/next.config.js When I include the next.config.js code provided below, I receive ...

Unable to retrieve data from response using promise in Angular 2?

I am struggling to extract the desired data from the response. Despite trying various methods, I can't seem to achieve the expected outcome. facebookLogin(): void { this.fb.login() .then((res: LoginResponse) => { this.acce ...

Vue alert: Issue with rendering - TypeError: Unable to access property 'NomeStr' as it is undefined

I'm having trouble displaying the 'NameSrt' item array value in my template, and I keep encountering this issue: vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'NomeStr' of undefined" The ...

Asynchronously parsing CSV files in NodeJs using the `await` keyword within the `on('data')`

I have a specific code snippet that is designed to read lines from a .csv file one by one and then store each processed row into a database const csv = require('csv-parse') const errors = [] csv.parse(content, {}) .on('data', async ...

Efficiently Encoding Still Image Data for NextJS

Currently, I am experimenting with a completely static method in my new Next.js v12 project. I am creating 5-6 data files to use with getStaticProps. Here is an example of one of the data file structures (they are all similar): import SqAshland1 from &apos ...

Develop a precompiled library for Angular applications that utilizes Ahead-of-Time (AOT) compilation technology

My Angular 5 library is packaged for consumption by other apps in their node_modules. Currently, the app is compiled Just-in-Time(JIT) using rollup and gulp. I export it as a package, and developers use it in its JIT compiled form. After researching AOT ...

Challenges encountered in d3.js when parsing through data sets

For the past two days, I have been struggling to solve this error and I'm at a loss. Every time I try to add data to the visualization, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined It seem ...

Manipulate jQuery to set the table row containing empty table data cells to be lower than the row with

I am using HTML and jQuery to sort my table, including non-standard sorting with multi-tbody. The issue I am facing is that the prices (Цена) in the td are sorted ascending but not correctly. Why is this happening? Additionally, I need to move the tr&a ...

Why is my code executing twice rather than just once?

` import './App.css'; import ArrayState from './components/ArrayState'; import FetchApi from './components/FetchAPI/FetchApi'; import Login from './components/Login'; import Useeffect2 from './components/Use ...

Ways to store information in variables and use it across different blocks in Cypress

Is it feasible to store data in variables and reuse them in other blocks within Cypress.io? For instance, imagine I have a unique name for a device. I aim to retrieve this information and then verify if the title in a new window includes that particular de ...

Transmitting JSON data to a WebMethod via AJAX

Currently, I am retrieving data from an HTML form and converting it into a JSON object to be sent to a WebMethod. Although the response indicates success, I am unsure about how to verify if it is being correctly transmitted to the WebMethod. I have attemp ...

Vue- async function results in a Promise object with a status of <pending>

Hey everyone, I could use some assistance with my Vue code. Here's the issue: I'm attempting to retrieve data (specifically anime) from an online anime API. In my project, I have two files: Anime.vue (the view page) and getAnime.js (which house ...

showing the chosen item in a Bootstrap dropdown [limited to the first menu option and using the attr() method]

Is there a way to dynamically display the selected menu option using the bootstrap drop down? Initially, I was able to select the first menu option and apply it to the bootstrap drop-down button. However, I encountered an issue where I cannot change the ...

How do I customize the HOME page in JHipster to display unique content based on user roles?

I am currently developing a JHipster project where I need to display different home pages based on the role of the user logging in. Specifically, I am utilizing Angular 1.x for this project. For instance, I have roles such as ROLE_ADMIN and ROLE_USER, each ...

Learn the process of displaying Json in Utf8 format using PHP

Could someone please advise me on how to properly print Json using php? I have the following code that works fine for English language, but when it comes to other languages, the output shows unknown characters like ??????? ??????? ???? ?? - ????? - ?????? ...

Tips for utilizing the material ui auto-complete search feature

I am in search of an alternative to material-ui-search-bar because it is no longer being maintained. I have been suggested to try using Material UI's auto complete instead. However, from the examples I've seen, it seems like only text field struc ...

Concentrate on elements that are activated without the need for clicking

Is it possible to trigger an action when an input is focused without the focus event being initiated by a click? $('#input').focus(function(){ if(not triggered by click) { alert('Hello!'); } }); ...

Updating the state of Formik

Currently, I'm knee-deep in a React project that requires a slew of calculations. To manage my forms, I've turned to Formik, and for extra utility functions, I've enlisted the help of lodash. Here's a peek at a snippet of my code: impor ...

Error 404 occurred when trying to access the webpack file at my website address through next js

Check out my website at https://i.stack.imgur.com/i5Rjs.png I'm facing an error here and can't seem to figure it out. Interestingly, everything works fine on Vercel but not on Plesk VDS server. Visit emirpreview.vercel.app for comparison. ...