Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.net list serialization to JSON Array

I'm using json.net to serialize an object to a json string. Now I have a list of Objects which I like to serialize into a Json array. However, I'm unable to do that with json.net and hope someone can point out my mistake.

I have the following classes:

class PeopleList {
    public Person inputs { get; set; }
}

class Person {
    public String name { get; set; }
    public int age { get; set; }
}

I'm using the following code to serialize the objects:

var json = new List<PeopleList>();
Person p1 = new Person { name = "Name 1", age = 20 };
json.Add(new PeopleList { inputs = p1 });
Person p2 = new Person { name = "Name 2", age = 30 };
json.Add(new PeopleList { inputs = p2 });


        string jsonString = JsonConvert.SerializeObject(json, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });

This gives me the following output:

[
  {
    "inputs": {
      "name": "Name 1",
      "age": 20
    }
  },
  {
    "inputs": {
      "name": "Name 2",
      "age": 30
    }
  }
]

Here is what I actually want:

[
  {
    "inputs": [
      {
        "name": "Name 1",
        "age": 20
      }
    ]
  },
  {
    "inputs": [
      {
        "name": "Name 2",
        "age": 30
      }
    ]
  }
]

As you see I need every object in my list encapsulated with []. How can I achieve that with Json.net? Thanks!

like image 308
thunder Avatar asked Apr 26 '16 15:04

thunder


People also ask

Can you serialize a list in JSON?

Json.NET has excellent support for serializing and deserializing collections of objects. To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.

What does Jsonconvert DeserializeObject do?

DeserializeObject Method. Deserializes the JSON to a . NET object.

How do you serialize a JSON list in Python?

Use json. dumps() to serialize a list into a JSON object. Use json. dumps(list) to serialize list into a JSON string.

What is JSON serialization?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.


1 Answers

If you want your inputs to be an array, you need to declare it as an array in your object :

class PeopleList {
    public List<Person> inputs { get; set; }
}

Then you can use it :

var json = new List<PeopleList>();
List<Person> p1 = new List<Person> { new Person { name = "Name 1", age = 20 } };
json.Add(new PeopleList { inputs = p1 });
List<Person> p2 = new List<Person> { new Person { name = "Name 2", age = 30 } };
json.Add(new PeopleList { inputs = p2 });

string jsonString = JsonConvert.SerializeObject(json, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });
like image 169
PMerlet Avatar answered Oct 12 '22 14:10

PMerlet