[英]How to convert JSON ARRAY from IRESTRESPONSE to C# array
I want to know a way to convert this json
to an array to deal with in c#
winform
. 我想知道一种方法,可以将此
json
转换为数组,以c#
winform
。 These are my tries but reach point and at this point I want to get the data but I cannot as I have no experience on json
. 这些是我的尝试,但是很切合时机,此时我想获取数据,但由于对
json
没有经验,所以无法获取。
The last code sentense give me error: 最后一个代码sendense给我错误:
No parameterless constructor defined for type of'WindowsFormsApplication2.Person[]'.
没有为“ WindowsFormsApplication2.Person []”的类型定义无参数的构造函数。
This is the respose content: 这是重新放置的内容:
JSON 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\"}]}}"
Class 类
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; }
}
Client 客户
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);
For your specific JSON string, you may use the Model
classes provided in the solution and also the deserialization process is being done: 对于您的特定JSON字符串,您可以使用解决方案中提供的
Model
类,并且反序列化过程也正在完成:
A working demo is here: https://dotnetfiddle.net/2VsyYL 一个有效的演示在这里: 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; }
}
This will output: 这将输出:
10
9.06725552044283
1.77286724180295
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.