我必須創建如下所示的 JSON 字串:
{
"sync": {
"email": "[email protected]",
"first": "James",
"last": "Dawson",
"cell": "2120980989",
"valueOfField": [
{
"number": "1",
"content": "second"
},
{
"number": "10",
"content": "Y"
}
/* more as needed */
]
}
}
我可以做任何事情valueOfField。
這是我的代碼:
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;*/
}
我嘗試添加一個IDictionary,但沒有奏效。
我可以在創建valueOfField.
uj5u.com熱心網友回復:
你需要另一個類作為 valueOfs
public class ValueOf{
public string number {get;set;}
public string content {get;set;}
}
然后
public class sync
{
public string first;
public string last;
public string email;
public string cell;
public List<ValueOf> valueOfField = new List<ValueOf>();
}
現在
cu.valueOfField.Add(new ValueOf{
Number="1", Content = "106-93-0909"
});
cu.valueOfField.Add(new ValueOf{
Number = "5", Content = "12/3/1995");
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442252.html
