C# 新System.Text.Json中的JObjec.Parse

C# 新System.Text.Json中的JObjec.Parse,c#,system.text.json,C#,System.text.json,我试图在我以前的解决方案中将Newtonsoft.Json.Linq更改为new System.Text.Json。我使用JObject.Parse(jsonstring)将Json字符串转换为Json对象,并将其返回到OpenApi V3 UI 通过JObject.Pase,我能够实现以下目标: JObject.Parse(Regex.Unescape($"{{{System.Environment.NewLine}\t\"Name\": \"{Name

我试图在我以前的解决方案中将Newtonsoft.Json.Linq更改为new System.Text.Json。我使用JObject.Parse(jsonstring)将Json字符串转换为Json对象,并将其返回到OpenApi V3 UI 通过JObject.Pase,我能够实现以下目标:

JObject.Parse(Regex.Unescape($"{{{System.Environment.NewLine}\t\"Name\": \"{Name}\",{System.Environment.NewLine}\t\"Version\": \"{Version}\",{System.Environment.NewLine}\t\"Platform\": \"{Platform}\"{((IsPuppetAgentServiceControllerRequired == false) ? String.Empty : $",{System.Environment.NewLine}\t\"IsPuppetAgentServiceControllerRequired\": true")}{((AgentUrl == null) ? String.Empty : $",{System.Environment.NewLine}\t\"AgentUrl\": \"{AgentUrl}\"")}{getModules()}{getModuleSources()}{getEnvironments()}}}"));
对此

然而,我不能对JsonDocument.Parse(jsonstring)做同样的事情。你知道我怎样才能做到这一点吗?此问题正在影响JsonSerializer.Deserialize,因为它似乎不支持JObject类型

使用上面JObject.Parse(…)中的字符串作为引用,使用JsonDocument.Parse的输出是

为了澄清我在JObject.Parse和JsonDocument.Parse中发送的字符串是相同的,它是:

{\n\t“名称”:“xxxx”、\n\t“版本”:“V1”、\n\t“平台”: “xxxx”、\n\t“模块”:{\n\t\t“模块1”:{}、\n\t\t“模块2”: {\n\t\t\t“property1”:“value1”,\n\t\t\t“property2”: “value2”,\n\t\t}\n\t}\n\t“模块资源”: {\n\t\t“模块资源1”:{\n\t\t\t“url”: "http://www.example.com“\n\t\t}\n\t\t”模块资源2”: {\n\t\t\t“url”: "http://www.example.com“\n\t\t}\n\t},\n\t“环境”: [“env1”、“env2”]\n}

它是格式良好的,否则JObject.Parse应该无法正确解析它,并向我提供上面显示的输出

添加代码测试:

工作

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JObject Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JObject.Parse(str);
}
///请求已成功。
///服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。
///内部服务器错误
[HttpGet]
[产品响应类型(StatusCodes.Status200OK)]
[产品响应类型(StatusCodes.Status400BadRequest)]
[产品响应类型(StatusCodes.Status500InternalServerError)]
公共工程
{
String str=$“{{System.Environment.NewLine}\t\'Name\”:“Name\'uxxxx\”,{System.Environment.NewLine}\t\'Version\:“vesion\'uxxxx\”,{System.Environment.NewLine}\t\'Platform\:“Platform\'uxxxx\”,{System.Environment.NewLine}\t\'agentur\:“https://www.example.com\“,{System.Environment.NewLine}\t\“Modules\”:{{{{System.Environment.NewLine}\t\t\”module1\“:{{},{System.Environment.NewLine}\t\t\“module2\”:{{{{System.Environment.NewLine}\t\t\“property1\”:“value1\”,{System.Environment.NewLine}\t\t\“property2\:“value2\”}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\“ModuleSources\”:{System.Environment.NewLine}\t\“ModuleSources\”{\t\t\t\t\“url\”:\“https://www.example.com\“},{System.Environment.NewLine}\t\t\t\“modulesource2\”:{{{{System.Environment.NewLine}\t\t\t\“url\”:\”https://www.example.com\“}{System.Environment.NewLine}\t},{System.Environment.NewLine}\t\'Environments\\:[\'env1\',\'env2\']{System.Environment.NewLine}}”;
返回JObject.Parse(str);
}
这对于JObject.Parse是有效的,但是对于JsonDocument.Parse则不行 调试时的字符串与上述相同

调试时在visual studio的文本查看器中查看它

不起作用

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JsonDocument Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JsonDocument.Parse(str);
}
///请求已成功。
///服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。
///内部服务器错误
[HttpGet]
[产品响应类型(StatusCodes.Status200OK)]
[产品响应类型(StatusCodes.Status400BadRequest)]
[产品响应类型(StatusCodes.Status500InternalServerError)]
公共JsonDocument Get()
{
String str=$“{{System.Environment.NewLine}\t\'Name\”:“Name\'uxxxx\”,{System.Environment.NewLine}\t\'Version\:“vesion\'uxxxx\”,{System.Environment.NewLine}\t\'Platform\:“Platform\'uxxxx\”,{System.Environment.NewLine}\t\'agentur\:“https://www.example.com\“,{System.Environment.NewLine}\t\“Modules\”:{{{{System.Environment.NewLine}\t\t\”module1\“:{{},{System.Environment.NewLine}\t\t\“module2\”:{{{{System.Environment.NewLine}\t\t\“property1\”:“value1\”,{System.Environment.NewLine}\t\t\“property2\:“value2\”}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\“ModuleSources\”:{System.Environment.NewLine}\t\“ModuleSources\”{\t\t\t\t\“url\”:\“https://www.example.com\“},{System.Environment.NewLine}\t\t\t\“modulesource2\”:{{{{System.Environment.NewLine}\t\t\t\“url\”:\”https://www.example.com\“}{System.Environment.NewLine}\t},{System.Environment.NewLine}\t\'Environments\\:[\'env1\',\'env2\']{System.Environment.NewLine}}”;
返回JsonDocument.Parse(str);
}

我花了一些时间,但我终于找到了解决办法。我只需要在Startup.cs中的ConfigureService函数上删除/注释旧的NewtonsoftJson控制器。似乎有了两个控制器,第二个就可以了。。。。。没有生效

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers();
     //services.AddControllers().AddNewtonsoftJson();
     services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
     ...
一旦我删除了Json,它就正确地显示在OpenApi/Swagger上

另外,如果不想显示rootElement,只需按如下方式更改函数:

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JsonElement Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JsonDocument.Parse(str).RootElement;
}
///请求已成功。
///服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。
///内部服务器错误
[HttpGet]
[产品响应类型(StatusCodes.Status200OK)]
[产品响应类型(StatusCodes.Status400BadRequest)]
[产品响应类型(StatusCodes.Status500InternalServerError)]
公共JsonElement Get()
{
String str=$“{{System.Environment.NewLine}\t\'Name\”:“Name\'uxxxx\”,{System.Environment.NewLine}\t\'Version\:“vesion\'uxxxx\”,{System.Environment.NewLine}\t\'Platform\:“Platform\'uxxxx\”,{System.Environment.NewLine}\t\'agentur\:“https://www.example.com\“,{System.Environment.NewLine}\t\“Modules\”:{{{{System.Environment.NewLine}\t\t\”module1\“:{{},{System.Environment.NewLine}\t\t\“module2\”:{{{{System.Environment.NewLine}\t\t\“property1\”:“value1\”,{System.Environment.NewLine}\t\t\“property2\:“value2\”}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\“ModuleSources\”:{System.Environment.NewLine}\t\“ModuleSources\”{\t\t\t\t\“url\”:\“https://www.example.com\"}