如何将JSON ARRAY从IRESTRESPONSE转换为C#数组

时间:2019-07-12 09:35:10

标签: c# arrays json

我想知道一种将这个json转换为要在c# winform中处理的数组的方法。 这些是我的尝试,但是很切合实际,此时我想获取数据,但是由于我没有json的经验,因此无法获取。

sentense的最后一个代码给我错误:

  

未为“ WindowsFormsApplication2.Person []”的类型定义无参数构造函数。

这是重新放置的内容:

JSON

"{\"Results\":{\"output1\":[{\"Class\":\"10\",\"age\":\"9.06725552044283\",\"menopause\":\"18.5605772106407\",\"tumor-size\":\"5.62041822714077\",\"inv-nodes\":\"2.50477840015836\",\"node-caps\":\"5.29640554298695\",\"deg-malig\":\"1.77286724180295\",\"breast\":\"2.67776376618299\",\"breast-quad\":\"5.61570098311485\",\"irradiat\":\"4.84969010222608\",\"Scored Labels\":\"1\",\"Scored Probabilities\":\"0.999995118824755\"}]}}"

班级

public class Person
{
    public string Class { get; set; }
    public string age { get; set; }

    public string menopause { get; set; }
    public string tumor_size { get; set; }

    public string inv_nodes { get; set; }
    public string node_caps { get; set; }

    public string deg_malig { get; set; }
    public string breast { get; set; }

    public string breast_quad { get; set; }
    public string irradiat { get; set; }

    public string output { get; set; }
    public string possiblity { get; set; }
}

客户

var client = new RestClient("https://ussouthcentral.services.azureml.net/workspaces/3d904b8d125f457fa269fc848989e8da/services/3873cde13fdf459c807a71f975dc4c3e/execute?api-version=2.0&format=swagger");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "1ffba0bb-194c-7aa4-0348-7a1a46855693");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Bearer 312mkibrneyPAdV9SiWbCJTs09VwCE9v2JBXds8SYmSSPp6OdwtKTb78Bk9n6RPZ91r/SVRs1idJDkDBBawIoA==");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\r\n        \"Inputs\": {\r\n                \"input1\":\r\n                [\r\n                    {\r\n                            'Class': \"10\",   \r\n                            'age': \"30\",   \r\n                            'menopause': \"60\",   \r\n                            'tumor-size': \"20\",   \r\n                            'inv-nodes': \"10\",   \r\n                            'node-caps': \"15\",   \r\n                            'deg-malig': \"10\",   \r\n                            'breast': \"10\",   \r\n                            'breast-quad': \"20\",   \r\n                            'irradiat': \"10\",   \r\n                    }\r\n                ],\r\n        },\r\n    \"GlobalParameters\":  {\r\n    }\r\n}\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content.ToString());

JavaScriptSerializer js = new JavaScriptSerializer();
var yourOjbect = new JavaScriptSerializer().DeserializeObject(response.Content);
Person[] persons = js.Deserialize<Person[]>(response.Content);

1 个答案:

答案 0 :(得分:1)

对于您的特定JSON字符串,您可以使用解决方案中提供的Model类,并且反序列化过程也正在完成:

有效的演示在这里:https://dotnetfiddle.net/2VsyYL

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string json=@"{'Results':{'output1':[{'Class':'10','age':'9.06725552044283','menopause':'18.5605772106407','tumor-size':'5.62041822714077','inv-nodes':'2.50477840015836','node-caps':'5.29640554298695','deg-malig':'1.77286724180295','breast':'2.67776376618299','breast-quad':'5.61570098311485','irradiat':'4.84969010222608','Scored Labels':'1','Scored Probabilities':'0.999995118824755'}]}}";
        var Sresponse = JsonConvert.DeserializeObject<RootObject>(json);

        foreach(var result in Sresponse.Results.output1)
        {    
            Console.WriteLine(result.Class);
            Console.WriteLine(result.age);
            Console.WriteLine(result.deg_malig);
            Console.WriteLine(result.Scored_Labels);

        }       
    }
}

public class Results
{
    public List<Output1> output1 { get; set; }
}

public class RootObject
{
    public Results Results { get; set; }
}


public class Output1
{
    public string Class { get; set; }
    public string age { get; set; }
    public string menopause { get; set; }

    [JsonProperty("tumor-size")]
    public string tumor_size { get; set; }

    [JsonProperty("inv-nodes")]
    public string inv_nodes { get; set; }

    [JsonProperty("node-caps")]
    public string node_caps { get; set; }

    [JsonProperty("deg-malig")]
    public string deg_malig { get; set; }

    public string breast { get; set; }

    [JsonProperty("breast-quad")]
    public string breast_quad { get; set; }

    public string irradiat { get; set; }

    [JsonProperty("Scored Labels")]
    public string Scored_Labels { get; set; }

    [JsonProperty("Scored Probabilities")]
    public string Scored_Probabilities { get; set; }
}

这将输出:

10
9.06725552044283
1.77286724180295
1