2015-09-12 68 views
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表的設計是這樣的:

enter image description here

插入數據後的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" 
    } 
} 

回答

-1

我傳遞額外的參數:

enter image description here

下面是JSON數據,我需要解析& tbJSonData表中插入它點擊按鈕事件像按遞歸方法方法如下:

protected void btnSave_Click(object sender, EventArgs e) 
{ 
var converter = new ExpandoObjectConverter(); 
dynamic message = JsonConvert.DeserializeObject<ExpandoObject>(txtJsonData.Text, converter); 
    RecursiveMethod(message); 
} 

是低是更新的遞歸方法:

/// <summary> 
///  Method is used to INSERT data within JsonData table Recursively 
/// </summary> 
/// <author> 
///  Added  :: Author : Irfan Ahmad 
/// </author> 
/// <param name="message">JSonData Deserialized data as ExpandoObject</param> 
/// <param name="parentPath">Parent Path with "\" (Slash) seperated</param> 
/// <param name="ParentId">Parent ID</param> 
/// <param name="aFieldIndex">Field Index is used if Object is an Array</param> 
public void RecursiveMethod(ExpandoObject message, string parentPath, int ParentId, int aFieldIndex) 
{ 

    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; 
     string Path = ""; 
     if (item.Value.GetType() == typeof(System.Dynamic.ExpandoObject) || item.ToString().Contains("System.Collections.Generic.List")) 
     { 
      int pID = 0; 
      K = item.Key; 

      pID = jData.Insert(MsgID, ParentId, parentPath, K, string.Empty, aFieldIndex); 

      if (!string.IsNullOrEmpty(parentPath)) 
      { 
       Path = parentPath + "\\" + item.Key; 
      } 
      else 
      { 
       Path = item.Key; 
      } 
      if (item.ToString().Contains("System.Collections.Generic.List")) 
      { 
       IEnumerable temp = (IEnumerable)item.Value; 

       int fieldIndex = 0; 
       foreach (var innerItem in temp) 
       { 
        if (!string.IsNullOrEmpty(innerItem.ToString())) 
        { 
         InsertJsonDataRecursiveMethod((ExpandoObject)innerItem, Path, pID, fieldIndex); 
         fieldIndex += 1; 
        } 

       } 
      } 
      else 
      { 
       InsertJsonDataRecursiveMethod((ExpandoObject)item.Value, Path, pID, aFieldIndex); 
      } 

     } 
     else 
     { 
      K = item.Key; 
      V = item.Value.ToString(); 

      jData.Insert(MsgID, ParentId, parentPath, K, V, aFieldIndex); 
     } 
    } 
} 

它的作品就像一個魅力!

感謝您的建議「Eser」。

相關問題