简体   繁体   English

如何反序列化JSon复杂类型C#

[英]How to Deserialization JSon complex type C#

I'm able to do jSon Deserialised and also Insertion data within Database table, but i want to insert the correct "ParentID & ParentFullPath" as well within table(Database). 我能够在数据库表中执行jSon反序列化和插入数据,但我想在表(数据库)中插入正确的“ParentID&ParentFullPath”。 How to do it with recursive method or without recursion. 如何使用递归方法或没有递归。

I wrote method on Click event of button like below: 我在按钮的Click事件上写了方法,如下所示:

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

below is the Recursive Method:: 下面是递归方法::

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);
        }
    }
}

I have tbJSonData table design like this: 我有这样的tbJSonData表设计:

在此输入图像描述

Json data after insertion within data (but ParentID & ParentPath is not accurate as per JsonData give below): 插入数据后的Json数据(但是ParentID和ParentPath不准确,如下面的JsonData给出):

在此输入图像描述

Below is the jSon data which i need to parse & Insert it within tbJSonData table: 下面是我需要解析并在tbJSonData表中插入的jSon数据:

{
  "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"
  }
}

I passed extra parameters on Click Event of Button like within RecursiveMethod method like below: 我在按钮的Click事件中传递了额外的参数,就像在RecursiveMethod方法中一样,如下所示:

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

Below is the updated Recursive method: 下面是更新的递归方法:

/// <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);
        }
    }
}

It works like a charm! 它就像一个魅力!

Thanks "Eser" for your suggestions. 感谢“Eser”的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM