Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

Convert simple json to string array in c#

I am new to C# REST API... I am just converting JSON to a string array

Here is my 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you use JSON.NET, it will certainly make things easier for you. My answer uses 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:

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...