有没有办法序列化 Json,其中包含一个包含更多 json 作为 XML 字符串的字段



我从外部源接收 Json 数据。我需要将 Json 消息转换为 XML。

使用NewtonSoft库相对简单:

var placeHolder = $"{{"Data":  {jsonContent} }} ";
var xmlNode = JsonConvert.DeserializeXmlNode(placeHolder, "Root").OuterXml;
var doc = new XmlDocument();
doc.LoadXml(xmlNode);

我收到的关于 Json 的一件奇怪的事情打破了整个过程。

Json 看起来像这样:

[
{
"Type": "Application",
"Object": {
"Duration": 2,
"Request": {
"authReference": "..",
"IdEnrolment": {
"username": "test",
"password": "***",
"createNewUser": false
}
},
"Type": "XXX.SomeController",
"Logs": [
],
"Method": "IdEnrolments",
"Response": {
"IdUsername": null,
"result": {
"resultCode": 0,
"resultTitle": null,
"resultMessage": null
}
}
}
},
{
"Type": "Proxy",
"Object": {
"Assembly": null,
"Policy": null,
"Cache": null,
"Duration": 516,
"Request": "{"AuthenticateUserRequest":{"EnterpriseContext":{"ContextInfo":{"@xmlns":"http://example.xom","ProcessContextId":"adad","ExecutionContextId":"adac"}"}}",
"Type": "https://someserver/services/ent/informationandtechnologymanagement/company/v1",
"Logs": [
],
"Method": "AuthenticateUser",
"Response": "{"AuthenticateUserResponse":{"EnterpriseContext":{"ContextInfo":{"@xmlns":"http://example.com","ProcessContextId":"adad","ExecutionContextId":"adad"}}}}"
}
}
]

数组中的第一个对象是完美的,并且可以正确序列化。数组中的第二个对象会导致混乱(可以理解(。此对象的"请求"和"响应"字段实际上是字符串,它们包含 JSON 代码。

有没有办法正确反序列化它?我意识到,要求图书馆足够灵活地做到这一点是很多 - 但我只是对如何使用它有点困惑。

我收到的所有消息看起来大致相同 - 但具体来说,请求和响应对象的内容因特定的请求/响应而异。

构建这个的人没有任何模式或契约,除了他们发送 Json 文本并且他们似乎不愿意或无法更改 JSON 的创建方式。

任何建议将不胜感激。

注意:该行为似乎不是随机的。我是否得到一个正确的对象或字符串由属性:">类型":"应用程序">决定。有许多不同的"类型" - 有些是正确序列化的,有些则没有。 啊!!!啊挫折!!!!

你的 JSON 在根/类型:代理/对象/请求嵌套中是砖块的

"Request": "{"AuthenticateUserRequest":{"EnterpriseContext":{"ContextInfo":{"@xmlns":"http://example.xom","ProcessContextId":"adad","ExecutionContextId":"adac"}"}}",

在这里,您打开了 4 个大括号,但只有 3 个关闭。

你必须处理的 JSON 非常非常不规则和糟糕,所以你只需要将所有这些分解为 C# 接口、类和结构,如下所示:

#region Fields
enum JsonObjType 
{
Application,
Proxy,
SomeController,
}
#endregion
#region Classes
// Application
public class AppObj : JsonObjMaster
{
public readonly JsonObjType Type_Ind {get;}
public int Duration {get; set;}
public AppRequest Request {get; set;}
public AppObjController Controller {get; set;}
//you need a default constuctor for classes, cause else no json parser will work with them
public AppObj()
{
Type_Ind = JsonObjType.Application;
Duration = 0;
AppResult = String.Empty;
Request = new AppRequest();
Controller = new AppObjController();
}
}
public class AppObjController : JsonObjSlave
{
public readonly JsonObjType Type_Ind {get;}
public string[] Logs {get; set;}
public Func</*whatever is supposed to be here*/> Method {get; set;}
public AppResult Result {get; set;}
public AppObjController()
{
Type_Ind = JsonObjType.SomeController;
Log = Array.Empty<string>();
Method = (/*whatever is supposed to be here*/) => null; //may need to change depending on what the function is for
Result = new AppResult();
}
}
// Proxy
public class ProxyObj : JsonObjMaster
{
public readonly JsonObjType Type_Ind {get;}
public int Duration {get; set;}
public string Assembly {get; set;}  //I am assuming strings here since no type is supplied
public string Policy {get; set;}
public string Cache {get; set;}
public string Request {get; set;}
public string Type {get; set;}
public string[] Logs {get; set;}
public Func</*whatever is supposed to be here*/> Method {get; set}
public string Response {get; set;}
public ProxyObj()
{
Type_Ind = JsonObjType.Proxy;
Duration = 0; //not needed but to stay consistent
Assembly = String.Empty();
Policy = String.Empty();
Cache = String.Empty();
Request = String.Empty();
Type = String.Empty();
Logs = Array.Empty<string>();
Method = (/*whatever is supposed to be here*/) => null; //may need to change depending on what the function is for
Response = String.Empty();
}
}
#endregion
#region Structs
// Interfaces
public interface JsonObjMaster
{
public readonly JsonObjType Type_Ind {get;}
public int Duration {get; set;}
}
public interface JsonObjSlave
{
public readonly JsonObjType Type_Ind {get;}
}
// Structs
public struct IdEnrolment
{
public string Username {get; set;}
public string Password {get; set;}
public bool CreateNewUser {get; set;}
}
public struct AppResult
{
public int Code {get; set;}
public string Title {get; set;}
public string Message {get; set;}
}
public struct AppRequest
{
public string AuthRefernece {get; set;}
public IdEnrolment {get; set;}
}
#endregion

为了将 JSON 加载到类中,现在需要在 JSON 文件的上下文中向 parer 提供 C# 对象属性的名称,以便解析器可以链接它们。这可能看起来像这样:

[MyJsonparser.JsonName("Type")]
public string Type {get; set;}

但它高度依赖于您使用的 JSON 库,因此您需要在他们的文档中查找它。

如果你能成功地将某人(希望不是你(所做的 JSON转换为 C# 对象,那么你可以通过移动到另一层类和结构来解决剩余的 JSON 字符串,然后你可以使用这些类和结构进行处理。

现在,在您将该 JSON 作为 C# ocjects 之后,您可以轻松地将其序列化为 XML,但遗憾的是,由于 JSON 的实现有多糟糕,如果不使用 C# 对象作为元媒介,您将无法自动执行此操作。

最新更新