[英]Convert simple json to string array in c#
I am new to C# REST API... I am just converting JSON to a string array我是 C# REST API 的新手……我只是将 JSON 转换为字符串数组
Here is my JSON这是我的 JSON
[{"Id":1000,"Name":"May","Address":"Atlanda","Country":"USA","Phone":12345}}
convert array like below code像下面的代码一样转换数组
string[] details={1000,May,Atlanda,USA,12345};
Help me to solve this problem帮我解决这个问题
My code我的代码
public class details
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public int Phone { get; set; }
}
This my class这是我的课
var client = new RestClient("http://localhost:3000/customer/1000");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
string json = new JavaScriptSerializer().Serialize(response.Content);
You can deserialize response.Content
to details
class as shown below using the new System.Text.Json APIs您可以使用新的System.Text.Json API 将response.Content
反序列化为details
类,如下所示
JsonSerializer.Parse<details>(response.Content);
For further information you can read Try the new System.Text.Json APIs .有关更多信息,您可以阅读尝试新的 System.Text.Json APIs 。
If you use JSON.NET, it will certainly make things easier for you.如果您使用 JSON.NET,它肯定会让您更轻松。 My answer uses JSON.NET:我的回答使用 JSON.NET:
string str = "[{\"Id\":1000,\"Name\":\"May\",\"Address\":\"Atlanda\",\"Country\":\"USA\",\"Phone\":12345}]";
var listOfDetails = JsonConvert.DeserializeObject<List<details>>(str);
foreach (var detail in listOfDetails)
{
var arr = detail.ToArr();
}
Following is the details
class:以下是details
类:
public class details
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public int Phone { get; set; }
public string[] ToArr()
{
List<string> list = new List<string> { Id.ToString(), Name, Address, Country, Phone.ToString() };
return list.ToArray();
}
}
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.