3
我能够在数据库表中执行jSon反序列化和插入数据,但我想要在表(数据库)中插入正确的“ParentID & ParentFullPath”。如何使用递归方法或不递归。如何反序列化JSon复杂类型C#
我在按钮的Click事件中写道方法象下面这样:
protected void btnSave_Click(object sender, EventArgs e)
{
var converter = new ExpandoObjectConverter();
dynamic message = JsonConvert.DeserializeObject<ExpandoObject>(txtJsonData.Text, converter);
RecursiveMethod(message);
}
下面
是递推法::
string parentID = string.Empty;
string parentPath = string.Empty;
public void RecursiveMethod(ExpandoObject message)
{
foreach (var item in message)
{
//System.Collections.Generic.Dictionary<string, string> keyValue = new System.Collections.Generic.Dictionary<string, string>();
string K = string.Empty;
string V = string.Empty;
if (item.Value.GetType() == typeof(System.Dynamic.ExpandoObject))
{
parentID = item.Key;
parentPath += item.Key + @"\";
K = item.Key;
jData.Insert(Guid.NewGuid(), string.Empty, parentPath, K, string.Empty);
RecursiveMethod((ExpandoObject)item.Value);
}
else
{
K = item.Key;
V = item.Value.ToString();
jData.Insert(Guid.NewGuid(), parentID, parentPath, K, V);
}
}
}
我tbJSonData表的设计是这样的:
插入数据后的Json数据(但ParentID & ParentPath是不准确的按JsonData在下面给出):
{
"interrogation": {
"patient": {
"firstName": "testname",
"lastName": "testfamily",
"dob": "1982-01-01",
"gender": "MALE"
},
"device": {
"manufacturer": "abc",
"manufacturerContact": "John Smith",
"modelName": "model",
"modelNumber": "i-123",
"serialNumber": "EUY1242C",
"type": "CRTD",
"implantDate": "2015-01-01",
"implantingPhysician": "Adam Smith"
},
"leads": [
{
"manufacturer": "BIOTRONIK",
"type": "RA",
"serialNumber": "EUY1242",
"implantDate": "2015-01-01"
}
],
"location": "",
"uploadDate": "",
"shocksAbortedECLClearDate": "",
"shocksAbortedSinceLastReset": "",
"routingLocId": "",
"orderingPhysician": {
"id": "1"
}
},
"patient": {
"id": 1156,
"name": "jmartest",
"family": "jmartest",
"dob": "06-02-2008",
"gender": "MALE"
},
"patientLocation": {
"id": 1159,
"location": {
"id": "1"
},
"mrn": "123"
}
}