如何在dotnet核心中将json文件递归转换为Dictionary <string,dynamic>

时间:2020-03-17 19:43:27

标签: c# json swagger openapi

我有一个庞大的doc json文件,我想将其转换为 密钥为属性名称和值的Dictionary<string,dynamic>可以是另一种Dictionary<string,dynamic>类型的字典,直到该值是原始值而不是json对象。

我从var dic = JSonObject.Parse("file.json").Properties().ToDictionary(p=>p.Name, p => p.Value);

开始

当值不是原始类型时,如何递归测试p.Value并将其转换为字典?

2 个答案:

答案 0 :(得分:0)

我刚刚找到了该软件包Microsoft.OpenApi.Readers,该软件包已经包含了我想解析大范围文档并像这样使用的所有内容

[localhost]
127.0.0.1

[backup]
host1
host2
host3

然后我可以将所有 string url = "https://localhost:10155/swagger/v1/swagger.json"; var httpClient = new HttpClient(); var stream = await httpClient.GetStreamAsync(url); var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic); return View(openApiDocument); 对象放到树中

答案 1 :(得分:0)

试试这个,它比异步概念更健壮......

private void GetServiceJSONDocument()
{
    Microsoft.OpenApi.Models.OpenApiDocument JsonDoc = new Microsoft.OpenApi.Models.OpenApiDocument();
    HttpWebResponse wresponse = GetServiceAPIRequest(false, SiteAPIURL).GetResponse() as HttpWebResponse;
    JsonDoc = new OpenApiStreamReader().Read(wresponse.GetResponseStream(), out var diagnostic); 
}

private static HttpWebRequest GetServiceAPIRequest(bool isPOST, string atvURL)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(atvURL);
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.ContentType = atvURL.ToLower().IndexOf(".json") >= 0  ? "application/json;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Headers.Add("SOAPAction", ActiveWSDLRequest);
    webRequest.Method = !isPOST ? "GET" : "POST";
    return webRequest;
}