I have to create something like the below, a JSON string:
{
"sync": {
"email": "[email protected]",
"first": "James",
"last": "Dawson",
"cell": "2120980989",
"valueOfField": [
{
"number": "1",
"content": "second"
},
{
"number": "10",
"content": "Y"
}
/* more as needed */
]
}
}
I can do everything up to valueOfField
.
Here is my code:
sync cu = new sync();
cu.first = "James";
cu.last = "Doe";
cu.email = "[email protected]";
cu.cell = "999-999-9999";
/*for (int i = 0; i < 2; i )
{
cu.valueOfField.Add("1", "106-93-0909");
cu.valueOfField.Add("5", "12/3/1995");
}*/
string json = Newtonsoft.Json.JsonConvert.SerializeObject(new { sync = cu });
...
public class sync
{
public string first;
public string last;
public string email;
public string cell;
/*public IDictionary<string, string> valueOfField;*/
}
I tried adding an IDictionary
but that didn't work.
Can I get some help in creating the valueOfField
.
CodePudding user response:
you need another class for the valueOfs
public class ValueOf{
public string number {get;set;}
public string content {get;set;}
}
then
public class sync
{
public string first;
public string last;
public string email;
public string cell;
public List<ValueOf> valueOfField = new List<ValueOf>();
}
now
cu.valueOfField.Add(new ValueOf{
Number="1", Content = "106-93-0909"
});
cu.valueOfField.Add(new ValueOf{
Number = "5", Content = "12/3/1995");
});