4
"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

3

2 Answers 2

5

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

Comments

-1

How to install

Install-Package Newtonsoft.Json:

In Visual Studio, Tools menu -> Manage Nuget Package Manger Solution and type “JSON.NET” to search it online. Here's the figure:

enter image description here

Then create class with input parameters that you expect to get it as a JSON response.

public class Inputs
{
    public string input1 { get; set; }
    public string input2 { get; set; }
    public string input3 { get; set; }
    public string input4 { get; set; }
}

Now we will convert it to a .NET object using DeserializeObject() method of JsonConvert class. Here is the code:

private void JSONDeserilaize(string json)
{
    var obj = JsonConvert.DeserializeObject<Inputs>(json);
}

Now you have an object of type Inputs Deserialized successfully

5 Comments

Please do not give a general not specific for the question example - it does not answer the question. If you want to suggest some library a comment fits best
The poster specifically asks for how to Deserialize
Sorry, I've edited my answer to match rules.
I do not thin this answer is fully correct. Because OP states that input count can be any number
Yeah this is just wrong. Seems obvious the JSON is dynamic, having a class with input1, input2, etc would not work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.