【发布时间】:2019-11-05 06:15:12
【问题描述】:
我有一个嵌套的动态 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, 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("$", "").Replace("^", "");
List<string> ResString = ReturnConciseValEntry(Value, null, db2, FullTextPath, null, 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 + "}";
【问题讨论】:
标签: c# json javascriptserializer