Converting an object with nested arrays into its original form (Ajax request using C#)

I am struggling with the deserialization of an object that has two properties. One property is a simple string, while the other property is an array of arrays, with each element in an array having a name and a value.

The POST operation works fine until it reaches the server side. I can debug on the server side, but I am unable to deserialize the passed data correctly. I am attempting to deserialize the someData object to a RequestObject on the server side.

I would appreciate your advice on this as I have been spending hours trying to solve this issue.

On the client side: (JavaScript)

var dataArrays = [];
dataArrays[0] = [ {"name":"key","value":"xyz"} ];
dataArrays[1] = [ {"name":"firstname","value":"john"}, {"name":"lastname","value":"doe"} ];

var someData = {
someKey: "xyz",
dataArrays: dataArrays
};

$.ajax({
        type: 'POST',
        url: './mywebservice.asmx/Test',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(someData), ..);

On the server side: (C#)

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Test(string someKey, string dataArrays)
    {
        // someKey has the value: "xyz"
        // dataArrays is a string in the following format:
        // [[{"name":"key","value":"xyz"}],[{"name":"firstname","value":"john"}{"name":"lastname","value":"doe"}]]

        //unable to deserialize in here in any way
        // ?? RequestObject requestObj = JsonConvert.DeserializeObject<RequestObject> ??

    }

    [DataContract]
    public class StringData
    {
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string value { get; set; }
    }

    [DataContract]
    public class RequestObject
    {
        [DataMember]
        public string someKey { get; set; }
        [DataMember]
        public List<StringData[]> dataArrays{ get; set; }
    }

Answer №1

After some troubleshooting, I managed to resolve the issue by updating the RequestObject to the following:

 [DataContract]
    public class RequestObject
    {
        [DataMember]
        public string key { get; set; }
        [DataMember]
       public List<List<StringData>> arrays{ get; set; }
    }

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 a simulation to deactivate webpage elements and showcase the UpdateProgress control

I am currently attempting to disable certain page elements and display an update progress control in my application. In order to achieve this, I have applied a style sheet with the property filter: alpha(opacity=85); However, I encountered an error stati ...

What is causing my React Query query function to be activated even though it is supposed to be disabled?

My goal is to dynamically set parameters and select a fetching function within a React Query function (useSpeciesCodes.js). The decision of which API endpoint to fetch from (using either getSpeciesCodesByRegion or getSpeciesCodesByAddress) should be based ...

Is there a way to display customized values on a particular column in a Vuetify table?

In the column named conditions, I am looking to display the count of rules > details. Please Note: The array rules has a property details.length = 2 This is what I have attempted https://i.stack.imgur.com/2LoFb.png Here is the code snippet: header ...

What is the best way to transfer data from an AJAX GET request to a Node.js GET route?

Here is an example of an ajax request being made $.ajax({ url: '/reload', type: 'GET', contentType: "application/json", data: { Name: $("#inputName").val(), Link: $("#inputLink").val() }, succe ...

Error with AngularJS: IE not showing dropdown options for MultiSelect

My application features a multiselect dropdown menu that shows a list of countries. While this dropdown functions correctly in Chrome, it displays options differently in IE as shown below: https://i.stack.imgur.com/xhOmV.png I have attempted to adjust th ...

Tips on how to efficiently update or insert an object within an array in Vue JS 2

My current item list is displayed below. I am looking to add new items to the list, but if the ID matches an existing entry, I want to update the value of that object. For example: segmentValues: [ { id:1, value:'Foo' }, ...

Tips for using regular expressions with the find method in JavaScript?

Welcome to my Object: let data = [{ "title": "User info", "category": "personal", "userId": "abc12345" }, { "title": "Customer Info", "category": ...

Encountered issue while converting half of the string array to JSON using JavaScript

I encountered an issue with the code below while trying to convert an array into JSON. Here is my code: <html> <head> </head> <body style="text-align:center;" id="body"> <p id="GFG_UP1" style="font-size: 16px;"> </p ...

Remove any objects with nil values from an array using filter and flatMap in Swift 4

I am populating a UICollectionView with results retrieved from a query. Occasionally, the records returned do not contain images. I want to exclude these objects completely. How can I avoid displaying results from a JSON query that do not include any imag ...

Interactive table created with DataTables that automatically updates the dynamic JSON data source whenever changes are made to the table

Using the Datatables plugin, I am dynamically populating a table with HTML rendered from a JSON array. However, I need the table to update the model (datasource) stored client-side whenever an edit is made. When navigating to a new page on the table, it s ...

Tips for utilizing a personalized technique on an array

Can you explain why a custom method on an array is not functioning properly in this scenario? function runTestMethod() { alert("method running"); } String.prototype.testMethod = runTestMethod; var A = "A"; A = A.testMethod(); // This works ...

Issue with Ng-style not functioning properly when setting background color

I am struggling to set the background of an element using a configuration object with ng-style. For some unknown reason, I can't seem to make it work and I'm finding it really perplexing. The element I'm attempting to configure: <div id ...

How can I retrieve the value of a specific <span> element by referencing the class of another <span> within

I have come across the following HTML: <div class="calculator-section"> <p> <span class="x"></span> <span class="amount">30</span> </p> <p> <span class="y"></span ...

Creating secure RSA keys using a predetermined seed - a step-by-step guide

Is it possible to utilize a unique set of words as a seed in order to recover a lost private key, similar to how cryptocurrency wallets function? This method can be particularly beneficial for end-to-end encryption among clients, where keys are generated o ...

JSON representation of a Loopback Model Object

I'm currently using LoopbackJS 3 and I'm facing an issue with defining model JSON properties for a model named Answers. Specifically, I am struggling to understand how to handle an array of objects. Model.defineProperty('answers', ...

What is the best method for packaging a React component library?

Currently, I am working on developing a React component library that I aim to distribute via npm to reach a wide audience. In my development process, I utilize webpack and babel for packaging and code processing. However, as a newcomer to webpack, I am uns ...

What is the best way to retrieve a valid JSON output from a response in Zend Framework 3?

Recently, I have been working on developing a client to interact with an API... use Zend\Http\Client; use Zend\Http\Request; use Zend\Json\Json; ... $request = new Request(); $request->getHeaders()->addHeaders([ &ap ...

Execute code after the CSS has been loaded, but before the images start downloading

My website is experiencing a challenge. Downloading images is taking too long, with the complete website taking around 20 seconds to fully load on my system. A loader is displayed but only hides once the complete website is loaded (after 20 seconds). Whe ...

Performing three consecutive Ajax requests in a Rails application

Recently, I developed a custom admin system for a local gym to manage payments, attendance, mailing, sales, and more. While everything is functioning smoothly, there seems to be an issue with the attendance feature. Occasionally, when taking attendance, an ...

Trouble arises when attempting to delete rows from my database with the use of HTML, PHP, and

I am developing an application where I have implemented this table: <?php require_once 'Connect2db3.php'; ?> <form> <fieldset> <article class="rondehoeken"> <header> <div class="streep1"></div& ...