【发布时间】:2022-01-14 22:51:00
【问题描述】:
我想在 newtonsoftjson 库中编写自定义 JSON 转换器以进行序列化:
要求sn-p:
{
"Roles":[
{
"Role":[
{
"LEAssociateTypeId":"101",
"LEAssociateTypeId_Value":"Client/Counterparty",
"LastUpdatedDate":"2021-11-30T08:35:01",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
},
{
"LEAssociateTypeId":"5501",
"LEAssociateTypeId_Value":"Principal",
"LastUpdatedDate":"2021-11-29T08:50:34",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
}
]
}
]
}
收件人:
{
"Roles":[
{
"Role":{
"LEAssociateTypeId":"101",
"LEAssociateTypeId_Value":"Client/Counterparty",
"LastUpdatedDate":"2021-11-30T08:35:01",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
}
},
{
"Role":{
"LEAssociateTypeId":"5501",
"LEAssociateTypeId_Value":"Principal",
"LastUpdatedDate":"2021-11-29T08:50:34",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
}
}
]
}
由于 Roles 是一个动态对象,不能为其定义对应的类。
我还检查了在线文档 Role is converted to a array as per as https://www.newtonsoft.com/json/help/html/convertingjsonandxml.html
源代码编写:
public class customconverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
JObject o = (JObject)t;
foreach(var a in _validcollectionList)
{
if (o[a] != null && o[a].Count() > 0)
{
var test = o[a][0];
var test1 = test["Role"];
var test2 = o[a] as JArray;
if (test1 != null && test1.Count() > 1)
{
foreach (var a1 in test1)
{
JObject obj = new JObject {
{ "Role", a1 }
};
test2.Add(obj);
}
}
test.Remove();
}
}
o.WriteTo(writer);
}
}
}
[2021 年 11 月 12 日] 更新:准确地说完整的 json 就像:
{
"Message": {
"MessageInfo": {
"Guid": "a2152d96-c202-4c08-a4a7-2331a648b586",
"SourceId": "101",
},
"Roles":[
{
"Role":[
{
"LEAssociateTypeId":"101",
"LEAssociateTypeId_Value":"Client/Counterparty",
"LastUpdatedDate":"2021-11-30T08:35:01",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
},
{
"LEAssociateTypeId":"5501",
"LEAssociateTypeId_Value":"Principal",
"LastUpdatedDate":"2021-11-29T08:50:34",
"LegalEntityRoleStatusId":"3",
"LegalEntityRoleStatusId_Value":"Active"
}
]
}
]
}
}
实体或属性的数量也是动态的,因此上述可能会有所不同。
已检查解决方案,以下代码行没有角色详细信息 =
var semiParsedJson = JObject.Parse(json);
只有 messageinfo 存在,所以它没有解析完整的 json。
我们也不能通过自定义 json 转换器来做,因为最初的目的是通过下面的代码行将 xml 转换为 json:
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
但不知何故,newtonsoftjson 库将同一级别的同名节点分组到一个数组中,这就是这个查询出现的原因。
请提出建议。
[12/12/2021]:示例 XML 片段:
<Message>
<MessageInfo>
<Guid>be190914-4b18-4454-96ec-67887dd4d7a7</Guid>
<SourceId>101</SourceId>
</MessageInfo>
<LegalEntities>
<LegalEntity>
<Roles>
<Role>
<LEAssociateTypeId>101</LEAssociateTypeId>
<LEAssociateTypeId_Value>Client/Counterparty</LEAssociateTypeId_Value>
<LastUpdatedDate>2021-08-07T23:05:17</LastUpdatedDate>
<LegalEntityRoleStatusId>3</LegalEntityRoleStatusId>
<LegalEntityRoleStatusId_Value>Active</LegalEntityRoleStatusId_Value>
</Role>
<Role>
<LEAssociateTypeId>6000</LEAssociateTypeId>
<LEAssociateTypeId_Value>Account Owner</LEAssociateTypeId_Value>
<LastUpdatedDate>2021-08-07T21:20:07</LastUpdatedDate>
<LegalEntityRoleStatusId>3</LegalEntityRoleStatusId>
<LegalEntityRoleStatusId_Value>Active</LegalEntityRoleStatusId_Value>
</Role>
<Role>
<LEAssociateTypeId>5003</LEAssociateTypeId>
<LEAssociateTypeId_Value>Investment Manager</LEAssociateTypeId_Value>
<LastUpdatedDate>2021-08-16T06:12:59</LastUpdatedDate>
<LegalEntityRoleStatusId>3</LegalEntityRoleStatusId>
<LegalEntityRoleStatusId_Value>Active</LegalEntityRoleStatusId_Value>
</Role>
</Roles>
</LegalEntity>
</LegalEntities>
</Message>
【问题讨论】:
-
欢迎来到 StackOverflow。我已将您的 json 样本修复为有效样本,希望您不要介意。
-
很抱歉,您的编辑带来的混乱多于清晰。现在您在谈论 xml 文件,还有一个新的 json 文件...我可以再次更改我提出的解决方案以适应您的需求,但我需要知道是否有任何其他要求或细节我需要注意。
-
由于角色下有动态字段,因此无法为此定义模型。自定义类型转换器用于特定类型以覆盖默认的序列化和反序列化逻辑。在您的情况下,您想将 json 转换为另一个,因此如果我对您的问题的理解正确,则不涉及序列化。
-
提供的初始 json 只是 sn-p 不是完整的 json ,除了角色实体之外,还可以有其他实体需要应用相同的逻辑,之后我可能可以循环遍历替代解决方案
-
添加了示例 XML 片段,请帮助