c# - 我需要序列化包含xml的json文件
问题描述
我有 JSON,我需要反序列化它。Json 文件在里面包含 XML。有什么建议么?
{"nt":0,"r":true,"o":[{"test":"20fgfgdfgdfAZ20AIgdg151","fddf":"ZregrIPgdffgfSgfg","d":"<DataPDU xmlns="urn:cma:stp:xsd:stp.1.0">
<Body>
</AppHdr>
<Document xmlns="urn:iso:">
.....
</Document></Body>
</DataPDU>"}]}
解决方案
您的 JSON 字符串似乎无效。o[0].d
您在、 或 XML 部分中有未转义的引号。我在下面提供了带有转义引号的 JSON 版本。
{
"nt": 0,
"r": true,
"o": [
{
"test": "20fgfgdfgdfAZ20AIgdg151",
"fddf": "ZregrIPgdffgfSgfg",
"d": "<DataPDU xmlns=\"urn:cma:stp:xsd:stp.1.0\"><Body></<Document xmlns=\"urn:iso:\"> ..... </Document></Body></DataPDU>"
}
]
}
使用 .NET Core 3.1 和System.Text.Json
命名空间,您可以使用以下内容反序列化上述 json:
async Task Main()
{
string fileName = "ExampleJson.txt";
Example example = null;
using (FileStream fs = File.OpenRead(fileName))
{
example = await JsonSerializer.DeserializeAsync<Example>(fs);
}
}
System.Text.Json
可以在此处找到有关使用命名空间的文档。
推荐阅读
- javascript - 如何在 Javascript (Node.js) 中将 .jl 文件转换为 .json 文件
- mysql - 确定我可以用我的原料烹饪的食谱的优雅方式
- angular - Angular 6多个不同尺寸的过滤对象
- c# - 如何在 Xamarin.Android 中“卸载”本机库?
- sorting - 按字典顺序查找具有定义数量的数字
- symfony - 如何在 Symfony 4 中使用 datetimeNormalizer?
- php - 如何将菜单项及其内容限制为 Woocommerce 中的登录用户
- python - 我在使用 tensorflow 在 python 中执行卷积神经网络程序时遇到错误,错误是
- java - 我的控制器在运行时工作,但 mockkmvc 测试因依赖项而失败,该依赖项甚至不在类中
- c++ - 由于 [abi:cxx11],两个 boost 库之间的符号不匹配