如何在C#中使用动态嵌套JSON文档

我有一个嵌套的动态JSON模板文档,我需要对其进行反序列化并将其插入值以制成其中包含值的JSON内容。   我有一个适用于非嵌套JSON的工作逻辑,但是我不确定如何使该逻辑(递归或其他一些更简单的方法)适用于嵌套文档。

示例JSON模板

{
"LoanDate": [
    "Test"
],"borrowerdetail": [
    {
        "borrowid": [
            "Test"
        ],"Name": [
            "Test"
        ],"Type": [
            "Test"
        ],"Status": [
            "Test"
        ]
    }
],"loans": [
    {
        "bibdetails": [
            {
                "id": [
                    "Test"
                ],"title": [
                    "Test"
                ],"author": [
                    "Test","Test"
                ]
            }
        ],"Collection": [
            "Test"
        ],"ItemType": [
            "Test"
        ],"ItemNo": [
            "Test"
        ]
    }
],"LoansItemBib": [
    "Test"
],"LoansItem": [
    "Test"
]
}

当前逻辑

JavaScriptSerializer _objSolrDeserialized = new JavaScriptSerializer();
SolrResultTemp = "{";
dynamic SolrTempObject = _objSolrDeserialized.Deserialize<dynamic>(SolrResultTemp);
string Serialized = _objSolrDeserialized.Serialize(SolrTempObject);
int _iParentCount = 0;
foreach (var ObjSolr in SolrTempObject)
{
    var Type = ObjSolr.Value.GetType();
    if (Type.Name == "String")
    {
        string Value = ObjSolr.Value.Replace("$","").Replace("^","");
        List<string> ResString = ReturnConciseValEntry(Value,null,db2,FullTextPath,null);
        if (ResString.Count > 0)
        {
            foreach (var Res in ResString)
            {
                if (_iParentCount > 0)
                    SolrResultTemp = SolrResultTemp + ",";
                SolrResultTemp = SolrResultTemp + "\"" + ObjSolr.Key + "\": \"" + Res + "\"";
                _iParentCount += 1;
            }
        }
    }
    if (Type.Name == "Object[]")
    {
        string Key = ObjSolr.Key;
        List<string> _lstvalue = new List<string>();
        foreach (var x in ObjSolr.Value)
        {
            string Value = x.Replace("$","");
            List<string> ResString = ReturnConciseValEntry(Value,null);
            if (ResString.Count > 0)
                _lstvalue.AddRange(ResString);
        }
        if (_lstvalue.Count > 0)
        {
            if (_iParentCount > 0)
                SolrResultTemp = SolrResultTemp + ",";
            SolrResultTemp = SolrResultTemp + "\"" + ObjSolr.Key + "\" : [ \"" + string.Join("\",\"",_lstvalue) + "\" ]";
            _iParentCount += 1;
        }
    }
}
SolrResultTemp = SolrResultTemp + "}";
mars00721 回答:如何在C#中使用动态嵌套JSON文档

我必须使用递归方法来完成工作。这是工作代码。

代码

    public IDictionary<string,object> ProcessJson(IDictionary<string,object> inputDic,LoansLogData loandata,string itemno,string parentKey = "",string childKey = "",int cnt = 1)
    {
        dynamic flexible = new ExpandoObject();

        IDictionary<string,object> ResultDic = (IDictionary<string,object>)flexible;
        try
        {

            foreach (KeyValuePair<string,object> entry in inputDic)
            {

                List<string> ElementCollection = new List<string>();
                List<string> _lstValue = new List<string>();
                if (entry.Value is System.Collections.ICollection)
                {
                    var countProp = entry.Value.GetType().GetProperty("Count");
                    var count = (int)countProp.GetValue(entry.Value,null);
                    int loopcount = 0;
                    foreach (var item in (System.Collections.ICollection)entry.Value)
                    {
                        if (item is Dictionary<string,object>)
                        {
                            if (entry.Key != null && entry.Key.Trim().StartsWith("@"))
                            {
                                parentKey = entry.Key.Trim();
                                parentKey = parentKey.Substring(0,parentKey.IndexOf(' '));
                                childKey = entry.Key.Trim().Replace(parentKey,"");
                                childKey = childKey.Trim();
                            }
                            if (parentKey == "@Loans")
                            {
                                List<IDictionary<string,object>> catList = new List<IDictionary<string,object>>();
                                foreach (var singleBibItem in loandata.ItemNo)
                                {

                                    catList.Add(ProcessJson((Dictionary<string,object>)item,loandata,singleBibItem,parentKey,childKey,cnt));
                                    cnt++;
                                }
                                ResultDic.Add(childKey,catList);

                            }
                            else
                            {
                                List<IDictionary<string,object>> BorrowList = new List<IDictionary<string,object>>();
                                BorrowList.Add(ProcessJson((Dictionary<string,itemno,childKey));
                                ResultDic.Add(childKey,BorrowList);
                            }
                        }
                        else
                        {

                            List<string> ResString = ReturnLoanConciseValEntry(parentKey,(String)item,cnt);
                            if (ResString.Count > 0)
                            {
                                _lstValue.AddRange(ResString);
                            }
                            if (_lstValue.Count > 0)
                            {
                                if (((String)item == "LoansItemBib" || (String)item == "LoansItemNo"))
                                    _lstValue = _lstValue.Distinct().ToList();
                                loopcount += 1;
                                if (loopcount == count)
                                    ResultDic.Add(entry.Key,_lstValue);
                            }

                        }

                    }
                }

            }
            return ResultDic;
        }
        catch (Exception ex)
        {
            return ResultDic;
        }

    }
本文链接:https://www.f2er.com/3161925.html

大家都在问