Reputation: 3450
I have some parameters in app.config
file of my project.
I want to do these parameters as simple as possible for customers
<add key="name" value="Incognito"/>
<add key="emails[0].type" value="Personal"/>
<add key="emails[0].email" value="[email protected]"/>
I need to do JSON from these parameters.
For now I use Dictionary
var parameters = new Dictionary<string, string>();
for (int i = 0; i < settings.Count; i++)
{
parameters.Add(settings.GetKey(i), settings[i]);
}
var jsonData = JsonConvert.SerializeObject(parameters);
In that case I have in result:
{ "name": "Incognito", "emails[0].type": "Personal", "emails[0].email": "[email protected]" }
But I want to see usual JSON array:
{ "name": "Incognito", "emails": [{"type": "Personal", "email": "[email protected]"}, {...}] }
How can I do that? How serialize it properly? Or maybe you know a way to write data in app.config
in human readable format?
Upvotes: 2
Views: 4624
Reputation: 13060
To get a JSON array you'll need to use something more like an array, say perhaps an Array
or List
of a type which has both type
and email
properties.
public class Parameters{
public string Name { get; set; }
public List<Email> Emails { get; set; }
}
public class Email{
public string Type { get; set; }
public string Email { get; set; }
}
Serialising an instance of Parameters
will get you the JSON structure you desire.
Upvotes: 4