Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Properties to Array C# [duplicate]

Tags:

json

c#

json.net

"inputs": {
    "input1": {
        "value": "abc"
    },
    "input2": {
        "value": "cde"
    },
    "input3": {
        "value": "efg"
    },
    "input4": {
        "value": "ghi"
    },      
}

Here number of properties in "inputs" may vary. How can I deserialize this into class:

class Inputs
{
    public Input[] Values{get; set;}
}

class Input
{
    public string input {get; set;}
}

One option is to change the json "inputs" as an array, but I dont have that choice now

like image 209
TheCoder Avatar asked Sep 03 '25 15:09

TheCoder


1 Answers

Your data matches the following data structure.

public class Data
{
    public Dictionary<string, Dictionary<string, string>> Inputs { get; set; }
}

Since you have not mentioned using any library for de/serializing JSON objects, I suggest pretty famous NewtonSoft library for .Net framework.

In you case you can simply deserialize your data with the following snippet.

var data = JsonConvert.DeserializeObject<Data>(YOUR_JSON_STRING);
like image 69
Hasan Emrah Süngü Avatar answered Sep 05 '25 06:09

Hasan Emrah Süngü