As others have already pointed out, the reason you are not getting the results you expect is because your JSON does not match the class structure that you are trying to deserialize into. You either need to change your JSON or change your classes. Since others have already shown how to change the JSON, I will take the opposite approach here.

To match the JSON you posted in your question, your classes should be defined like those below. Notice I've made the following changes:

  1. I added a Wrapper class corresponding to the outer object in your JSON.
  2. I changed the Values property of the ValueSet class from a List<Value> to a Dictionary<string, Value> since the values property in your JSON contains an object, not an array.
  3. I added some additional [JsonProperty] attributes to match the property names in your JSON objects.

Class definitions:

class Wrapper
{
    [JsonProperty("JsonValues")]
    public ValueSet ValueSet { get; set; }
}

class ValueSet
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("values")]
    public Dictionary<string, Value> Values { get; set; }
}

class Value
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("diaplayName")]
    public string DisplayName { get; set; }
}

You need to deserialize into the Wrapper class, not the ValueSet class. You can then get the ValueSet from the Wrapper.

var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

Here is a working program to demonstrate:

class Program
{
    static void Main(string[] args)
    {
        string jsonString = @"
        {
            ""JsonValues"": {
                ""id"": ""MyID"",
                ""values"": {
                    ""value1"": {
                        ""id"": ""100"",
                        ""diaplayName"": ""MyValue1""
                    },
                    ""value2"": {
                        ""id"": ""200"",
                        ""diaplayName"": ""MyValue2""
                    }
                }
            }
        }";

        var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

        Console.WriteLine("id: " + valueSet.Id);
        foreach (KeyValuePair<string, Value> kvp in valueSet.Values)
        {
            Console.WriteLine(kvp.Key + " id: " + kvp.Value.Id);
            Console.WriteLine(kvp.Key + " name: " + kvp.Value.DisplayName);
        }
    }
}

And here is the output:

id: MyID
value1 id: 100
value1 name: MyValue1
value2 id: 200
value2 name: MyValue2
Answer from Brian Rogers on Stack Overflow
Top answer
1 of 9
22

As others have already pointed out, the reason you are not getting the results you expect is because your JSON does not match the class structure that you are trying to deserialize into. You either need to change your JSON or change your classes. Since others have already shown how to change the JSON, I will take the opposite approach here.

To match the JSON you posted in your question, your classes should be defined like those below. Notice I've made the following changes:

  1. I added a Wrapper class corresponding to the outer object in your JSON.
  2. I changed the Values property of the ValueSet class from a List<Value> to a Dictionary<string, Value> since the values property in your JSON contains an object, not an array.
  3. I added some additional [JsonProperty] attributes to match the property names in your JSON objects.

Class definitions:

class Wrapper
{
    [JsonProperty("JsonValues")]
    public ValueSet ValueSet { get; set; }
}

class ValueSet
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("values")]
    public Dictionary<string, Value> Values { get; set; }
}

class Value
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("diaplayName")]
    public string DisplayName { get; set; }
}

You need to deserialize into the Wrapper class, not the ValueSet class. You can then get the ValueSet from the Wrapper.

var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

Here is a working program to demonstrate:

class Program
{
    static void Main(string[] args)
    {
        string jsonString = @"
        {
            ""JsonValues"": {
                ""id"": ""MyID"",
                ""values"": {
                    ""value1"": {
                        ""id"": ""100"",
                        ""diaplayName"": ""MyValue1""
                    },
                    ""value2"": {
                        ""id"": ""200"",
                        ""diaplayName"": ""MyValue2""
                    }
                }
            }
        }";

        var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

        Console.WriteLine("id: " + valueSet.Id);
        foreach (KeyValuePair<string, Value> kvp in valueSet.Values)
        {
            Console.WriteLine(kvp.Key + " id: " + kvp.Value.Id);
            Console.WriteLine(kvp.Key + " name: " + kvp.Value.DisplayName);
        }
    }
}

And here is the output:

id: MyID
value1 id: 100
value1 name: MyValue1
value2 id: 200
value2 name: MyValue2
2 of 9
11

http://json2csharp.com/

I found the above link incredibly helpful as it corrected my C# classes by generating them from the JSON that was actually returned.

Then I called :

JsonConvert.DeserializeObject<RootObject>(jsonString); 

and everything worked as expected.

🌐
Thiago Passos
passos.com.au › converting-json-object-into-c-list
Converting JSON Objects into C# List<> - Thiago Passos
March 7, 2019 - The DataRequest would look something along these lines, but the Players property wouldn't be bound as it expects a JSON array instead of an object. public class DataRequest { public bool Status {get;set;} public string Version {get;set;} [JsonProperty("status_code")] public int StatusCode {get;set;} public TimeSpan Expires {get;set;} public List<Player> Player {get;set;} } There it comes a custom JsonConverter from Newtonsoft for the win. public class PlayersConverter : JsonConverter { // This is used when you're converting the C# List back to a JSON format public override void WriteJson(JsonW
Discussions

android - Converting JSONarray to ArrayList - Stack Overflow
ArrayList listdata = new ArrayList(); 2014-07-19T12:55:24.967Z+00:00 ... Good snippet. Just in case if anyone wants: there is a helper class that converts JSONObject/JSONArray to a standard Map/List on the github gist.github.com/codebutler/2339666 2014-08-27T01:54:5... More on stackoverflow.com
🌐 stackoverflow.com
[Help] JSON Array to List

Here's how to think about deserializing JSON.

There are two paths, and one is easier. The easier is to make a C# object graph that looks like the JSON object, then deserialize to it. The harder is to use the token parser or types like JObject to do the same thing with a little more effort. The harder paths are better if the JSON is really "weird" and requires special effort to get into sensible C# objects.

This isn't quite bad enough to need to use JArray. Obviously you can't make a C# object with a property named "1", "2", "3", and so on, but there's another way to go about this. Any JSON object is effectively a Dictionary<string, object>, so our first decision can be that this is our outer object.

The inner objects of this dictionary do have a well-defined type. They have four properties that are strings, though maybe for your logic "id" will be a number. So we can make an object to represent this and have a more specific Dictionary.

So I'd start with:

public class JsonTask
{
    public string Id { get; set; }
    public string Entrant { get; set; }
    public string Recipient { get; set; }
    public string Task { get; set; }
}

Then the code to get the dictionary is:

var dict = JsonConvert.DeserializeObject<Dictionary<string, JsonTask>>(json);

From there, getting an array could be:

var asArray = dict.Values.ToArray();

If order is super important, you might have to do something more complex. If you know all the properties are always sequential integers, a for loop over Keys is good enough. If they might not be ordered, you might try converting Keys to an array, sorting it, then using each key to fetch each value.

I don't think the other comment about deserializing to a list will work, generally you can only deserialize JSON arrays to lists, and this is more of an object being used as a dictionary.

More on reddit.com
🌐 r/csharp
6
1
September 11, 2018
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
November 18, 2024
Convert JSON String to C# Object List - Stack Overflow
I want to convert a JSON string to a Object list. It would be more helpful if done by Newtonsoft's JSON.NET. I tried, but it's not working. I don't want all the values of that JSON data, just the o... More on stackoverflow.com
🌐 stackoverflow.com
🌐
CMake Discourse
discourse.cmake.org › usage
How to convert json array to cmake list variable - Usage - CMake Discourse
May 1, 2022 - Suppose I have part of a json: "dir": ["a/b", "c/d" ] , how should I get a cmake list variable like: set(var "a/b" "c/d") via string(JSON If json is {"name":"foo"}, I can get foo from string(JSON cur_name GET ${MY_JSON…
🌐
Codegitz
codegitz.com › home › c# – how to convert json array to list?
C# - How To Convert JSON Array To List? - Codegitz
May 24, 2024 - using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; namespace codegitz { internal class Program { private static JArray GetArray() { JArray newArray = new(); newArray.Add("Example 1"); newArray.Add("Example 2"); newArray.Add("Example 3"); return newArray; } static void Main(string[] args) { // Dictionary to store data Dictionary<string, dynamic> KVP = new(); // The array used for demonstration JArray jArray = GetArray(); // Assign value to key KVP["ExampleKey"] = jArray; /* * Here we will take the value previously assigned to the key and assign it to a list of strings * But implic
🌐
CSharp Academy
csharp.academy › home › how to convert json array to list in c#
How to Convert JSON Array to List in C# - CSharp Academy
November 23, 2024 - Converting JSON arrays to C# lists is straightforward with Newtonsoft.Json. By defining appropriate model classes and using the JsonConvert.DeserializeObject method, you can effortlessly parse JSON data into structured, strongly-typed lists.
Top answer
1 of 16
204
ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
} 
2 of 16
85

I've done it using Gson (by Google).

Add the following line to your module's build.gradle:

dependencies {
  // ...
  // Note that `compile` will be deprecated. Use `implementation` instead.
  // See https://stackoverflow.com/a/44409111 for more info
  implementation 'com.google.code.gson:gson:2.8.2'
}

JSON string:

private String jsonString = "[\n" +
            "        {\n" +
            "                \"id\": \"c200\",\n" +
            "                \"name\": \"Ravi Tamada\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c201\",\n" +
            "                \"name\": \"Johnny Depp\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c202\",\n" +
            "                \"name\": \"Leonardo Dicaprio\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c203\",\n" +
            "                \"name\": \"John Wayne\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c204\",\n" +
            "                \"name\": \"Angelina Jolie\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c205\",\n" +
            "                \"name\": \"Dido\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c206\",\n" +
            "                \"name\": \"Adele\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c207\",\n" +
            "                \"name\": \"Hugh Jackman\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c208\",\n" +
            "                \"name\": \"Will Smith\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c209\",\n" +
            "                \"name\": \"Clint Eastwood\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2010\",\n" +
            "                \"name\": \"Barack Obama\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2011\",\n" +
            "                \"name\": \"Kate Winslet\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2012\",\n" +
            "                \"name\": \"Eminem\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        }\n" +
            "    ]";

ContactModel.java:

public class ContactModel {
     public String id;
     public String name;
     public String email;
}

Code for converting a JSON string to ArrayList<Model>:

Note: You have to import java.lang.reflect.Type;:

// Top of file
import java.lang.reflect.Type;

// ...

private void parseJSON() {
    Gson gson = new Gson();
    Type type = new TypeToken<List<ContactModel>>(){}.getType();
    List<ContactModel> contactList = gson.fromJson(jsonString, type);
    for (ContactModel contact : contactList){
        Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
    }
}

Hope this will help you.

🌐
LinkedIn
linkedin.com › pulse › c-json-array-object-mihamina-rakotomandimby
C# Json Array to Object
January 17, 2020 - List<Article> l = JsonSerializer.Deserialize<List<Article>>(j); If in "j" you had a regular JSON Objet (not an Array), you would do · Article a = JsonSerializer.Deserialize<Article>(j); You must know in advance either you are going to deal with an Object or with an Array of Objects. You have to know the JSON attributes of the Serialized JSON and build the model with the matching attributes, that could be painfull if you have much) Just with "JsonSerializer.Deserialize<T>()", you convert the JSON string to Object(s)
Find elsewhere
🌐
Google Groups
groups.google.com › g › vertx › c › 5fgz-e2rTbk
casting a JsonArray to a specific type
I think that should do it and return a Set<String>. Modify the map() call to convert each object to the type you want the generic set to specialize on. The Java 8 Stream API, in combination with lambdas, is so useful. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... In case someone is interested, when it is a more complex type. JsonArray jarr = new JsonArray(messageAsyncResult.result().bodyAsString()); List<Customer> cl = (List<Customer>) jarr.getList().stream().map(s ->
🌐
Reddit
reddit.com › r/csharp › [help] json array to list
r/csharp on Reddit: [Help] JSON Array to List
September 11, 2018 -

Hi all,

I'm playing around with JSON and I have a JSON array that I'd like to add into a Listbox. I've only ever used a single JSON before and I'm not sure how to do it with a JSON Array using NewtonSoft.

{
    "1": {
        "id": "628190",
        "entrant": "ENTRANT NAME",
        "recipient": "RECIPIENT NAME",
        "task": "Task Description"
    }
    "2": {
        "id": "628191",
        "entrant": "ENTRANT NAME",
        "recipient": "RECIPIENT NAME",
        "task": "Task Description"
    }
    "3": {
        "id": "628192",
        "entrant": "ENTRANT NAME",
        "recipient": "RECIPIENT NAME",
        "task": "Task Description"
    }
}

I know I'm missing a foreach or something like that. In the past I've only done something like this:

{
    "description": "Some description",
    "reason": "Some reason"
}

and I've just used this

Details detail = JsonConvert.DeserializeObject<Details>(webResult1);
Top answer
1 of 2
5

Here's how to think about deserializing JSON.

There are two paths, and one is easier. The easier is to make a C# object graph that looks like the JSON object, then deserialize to it. The harder is to use the token parser or types like JObject to do the same thing with a little more effort. The harder paths are better if the JSON is really "weird" and requires special effort to get into sensible C# objects.

This isn't quite bad enough to need to use JArray. Obviously you can't make a C# object with a property named "1", "2", "3", and so on, but there's another way to go about this. Any JSON object is effectively a Dictionary<string, object>, so our first decision can be that this is our outer object.

The inner objects of this dictionary do have a well-defined type. They have four properties that are strings, though maybe for your logic "id" will be a number. So we can make an object to represent this and have a more specific Dictionary.

So I'd start with:

public class JsonTask
{
    public string Id { get; set; }
    public string Entrant { get; set; }
    public string Recipient { get; set; }
    public string Task { get; set; }
}

Then the code to get the dictionary is:

var dict = JsonConvert.DeserializeObject<Dictionary<string, JsonTask>>(json);

From there, getting an array could be:

var asArray = dict.Values.ToArray();

If order is super important, you might have to do something more complex. If you know all the properties are always sequential integers, a for loop over Keys is good enough. If they might not be ordered, you might try converting Keys to an array, sorting it, then using each key to fetch each value.

I don't think the other comment about deserializing to a list will work, generally you can only deserialize JSON arrays to lists, and this is more of an object being used as a dictionary.

2 of 2
2

That... is not an array, is it? An array is using [], you just have an object with properties 1, 2, and 3. I don't think that'd even work.

To deserialize into a list, you'd do something like JsonConvert.DeserializeObject<List<Details>>(webResult1);

🌐
C# Corner
c-sharpcorner.com › UploadFile › vendettamit › parsing-list-of-json-elements-as-list-with-json-net
Parsing List of JSON Elements as List With JSON.Net
November 11, 2020 - And so you will get a complete list of correct items and corrupted data. We'll use a JArray class from the namespace Newtonsoft.Json.Linq to parse the data as a list of arrays of objects and then we'll convert one by one each item to a typed object and add it to the list.
Top answer
1 of 7
122

You can use json2csharp.com to Convert your json to object model

  • Go to json2csharp.com
  • Past your JSON in the Box.
  • Clik on Generate.
  • You will get C# Code for your object model
  • Deserialize by var model = JsonConvert.DeserializeObject<RootObject>(json); using NewtonJson

Here, It will generate something like this:

public class MatrixModel
{
    public class Option
    {
        public string text { get; set; }
        public string selectedMarks { get; set; }
    }

    public class Model
    {
        public List<Option> options { get; set; }
        public int maxOptions { get; set; }
        public int minOptions { get; set; }
        public bool isAnswerRequired { get; set; }
        public string selectedOption { get; set; }
        public string answerText { get; set; }
        public bool isRangeType { get; set; }
        public string from { get; set; }
        public string to { get; set; }
        public string mins { get; set; }
        public string secs { get; set; }
    }

    public class Question
    {
        public int QuestionId { get; set; }
        public string QuestionText { get; set; }
        public int TypeId { get; set; }
        public string TypeName { get; set; }
        public Model Model { get; set; }
    }

    public class RootObject
    {
        public Question Question { get; set; }
        public string CheckType { get; set; }
        public string S1 { get; set; }
        public string S2 { get; set; }
        public string S3 { get; set; }
        public string S4 { get; set; }
        public string S5 { get; set; }
        public string S6 { get; set; }
        public string S7 { get; set; }
        public string S8 { get; set; }
        public string S9 { get; set; }
        public string S10 { get; set; }
        public string ScoreIfNoMatch { get; set; }
    }
}

Then you can deserialize as:

var model = JsonConvert.DeserializeObject<List<MatrixModel.RootObject>>(json);
2 of 7
7
public static class Helper
{
    public static string AsJsonList<T>(List<T> tt)
    {
        return new JavaScriptSerializer().Serialize(tt);
    }
    public static string AsJson<T>(T t)
    {
        return new JavaScriptSerializer().Serialize(t);
    }
    public static List<T> AsObjectList<T>(string tt)
    {
        return new JavaScriptSerializer().Deserialize<List<T>>(tt);
    }
    public static T AsObject<T>(string t)
    {
        return new JavaScriptSerializer().Deserialize<T>(t);
    }
}
🌐
Stack Exchange
salesforce.stackexchange.com › questions › 375426 › json-array-string-into-list
apex - JSON Array String into List - Salesforce Stack Exchange
May 6, 2022 - Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... String stringJSON = '[{"action":"ASSIGN","value":"Router"},{"action":"DISABLE","value":true},{"action":"HIDE","value":false}]'; Can anyone help how do I convert this JSON string into List of Map?
Top answer
1 of 2
2

If your array continues with X4,Y4,Z4, you have a problem since, for deserializing the strong type class from the JSON, the array entries should be known. To deserialize the current JSON, use the following classes:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string X1 { get; set; }
    public string Y1 { get; set; }
    public string Z1 { get; set; }
    public string X2 { get; set; }
    public string Y2 { get; set; }
    public string Z2 { get; set; }
    public string X3 { get; set; }
    public string Y3 { get; set; }
    public string Z3 { get; set; }
}

You may read the JSON dynamically instead of deserializing the array:

    using System.Text.Json.Nodes;
    var jsonString = "[{\"X1\":\"x1\",\"Y1\":\"y1\",\"Z1\":\"z1\"},{\"X2\":\"x2\",\"Y2\":\"y2\",\"Z2\":\"z2\"},{\"X3\":\"x3\",\"Y3\":\"y3\",\"Z3\":\"z3\"}]";
    var jsonObject = JsonNode.Parse(jsonString);
    Console.WriteLine(jsonObject.ToString());
    Console.WriteLine(jsonObject[0]["X1"]);

Alon.

2 of 2
0

Hi @Julio Bello ,

[{"X1":"x1","Y1":"y1","Z1":"z1"},{"X2":"x2","Y2":"y2","Z2":"z2"},{"X3":"x3","Y3":"y3","Z3":"z3"},...]

From the above JSON string, we can see that each property (key-value pair, such as "X1":"x1", "X2":"x2") has a different property name or key value, in this scenario the property is not fixed so we can directly use it as the class's property.

So, for the above JSON string, I suggest you could deserialize it uses a Dictionary, you can refer to the following sample code:

       //required using System.Text.Json;  

        var values = new List>()  
        {  
              new Dictionary()  
                {  
                    {"X1", "x1"},{"Y1", "Y1"},{"Z1", "Z1"}  
                },  
                  new Dictionary()  
                {  
                    {"X2", "x2"},{"Y2", "Y2"},{"Z2", "Z2"}  
                }  
        };   
        var jsonstring = JsonSerializer.Serialize(values);  
        //jsonstring: [{"X1":"x1","Y1":"Y1","Z1":"Z1"},{"X2":"x2","Y2":"Y2","Z2":"Z2"}]  
        var reult1 = JsonSerializer.Deserialize>>(jsonstring);  

        var test = new TestModel()  
        {  
            Item = new List>()  
            {  
                new Dictionary()  
                {  
                    {"X1", "x1"},{"Y1", "Y1"},{"Z1", "Z1"}  
                },  
                  new Dictionary()  
                {  
                    {"X2", "x2"},{"Y2", "Y2"},{"Z2", "Z2"}  
                }  
            }  
        };  

        var jsonstring2 = JsonSerializer.Serialize(test);  
        //josnstring2: {"Item":[{"X1":"x1","Y1":"Y1","Z1":"Z1"},{"X2":"x2","Y2":"Y2","Z2":"Z2"}]}  

        var result2 = JsonSerializer.Deserialize(jsonstring2);  

The TestModel

public class TestModel  
{  
    public List> Item { get; set; }  
}  

The output is like this:

And this sample code:

        var jsonstr = "[{\"X1\":\"x1\",\"Y1\":\"Y1\",\"Z1\":\"Z1\"},{\"X2\":\"x2\",\"Y2\":\"Y2\",\"Z2\":\"Z2\"}]";  

        var result3 = JsonSerializer.Deserialize>>(jsonstr);  

The result:

After that you can find the data from the Dictionary. More detailed information about Dictionary, see Dictionary Class


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion

🌐
How to do in Java
howtodoinjava.com › home › convert json array to list: gson, jackson and org.json
Convert JSON Array to List: Gson, Jackson and Org.json
June 18, 2025 - Learn to use Gson, Jackson, and Org.json libraries to convert a JSON array into an ArrayList of objects with easy-to-understand examples.
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
JSON Arrays
Async Path Async Intro Async Timeouts Async Callbacks Async Promises Async Await Async Fetch Async Debug Async Reference JS Modules · Modules Intro Modules Export Modules Import Modules Namespace Modules Dynamic JS Meta & Proxy · Meta Programming Meta Reflect Meta Proxy Meta Reference JS Typed Arrays · Typed Arrays Typed Methods Typed Reference Array Buffers DataViews JS Atomics JS DOM Navigation · DOM Navigation DOM Nodes DOM Collections DOM Node Lists JS Windows