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

时间:2015-09-12 14:02:55

标签: c# json deserialization

我能够在数据库表中执行jSon反序列化和插入数据,但我想插入正确的" ParentID& ParentFullPath"在表(数据库)中也是如此。如何使用递归方法或没有递归。

我在按钮的Click事件上写了方法如下:

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

下面是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);
        }
    }
}

我有这样的tbJSonData表设计:

enter image description here

在数据中插入后的Json数据(但是根据下面的JsonData给出,ParentID&amp; ParentPath不准确):

enter image description here

以下是我需要解析的jSon数据。将其插入tbJSonData表中:

{
  "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 个答案:

答案 0 :(得分:-1)

我在按钮的Click事件中传递了额外的参数,如下面的RecursiveMethod方法:

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

它就像一个魅力!

谢谢&#34; Eser&#34;为了你的建议。