I can see that your commented out json in line #16, starts with a object called data, and the value for that key is the json within an array. And you are trying to iterate within the “listOfCustomers.data”, but when you are serializing you are simply creating a Customer object and passing that as a parameter to jsonConvert.SerializeObject()-method.
You need to create a new Customer List and add the customer-object into that freshly created Customer List-key named “data” as a list of the type Customer. After that you could try to serialize the newly created customer list which consists of a list of the type customer.
Customer customer = new Customer { id = “234234”, name = “Mikael” };
CustomerList customerList = new CustomerList { data = new List<Customer> { customer } };
string outputJSON = JsonConvert.SerializeObject(customerList);
System.IO.File.WriteAllText(outputPath, outputJSON);
Also I can see that you are using two different classes for serialization and deserialization as of line # 23.
Instead of using the JavaScriptSerializer
which is a part of the .net-framework, you could simply use the JsonConvert.DeserializeObject
-method since you are already using other methods from the JsonConvert-class.
CustomerList listOfCustomers = JsonConvert.DeserializeObject<CustomerList>(json);
Here we are using the JsonConvert.DeserializeObject
-method and passing in a CustomerList-type.