如何在文本框中显示JSON



我有一个示例JSON文件。如何在文本框上逐行显示,不需要缩进?为每个字段创建一个变量是否更好?

{
"basics": {
"name": "Your first and last name",
"label": "",
"picture": "",
"email": "Your email address",
"phone": "A phone number, with any formatting you like. E.g. (555) 555-5555.",
"degree": "",
"website": "Your website URL",
"summary": "A one-sentence to one-paragraph overview text. Do not include any line-breaks.",
"location": {
"address": "Your street address or mailing address",
"postalCode": "Your postal code (ZIP in the U.S.)",
"city": "Your city",
"countryCode": "Your country (e.g. USA)",
"region": "Your region (state in the U.S.)"
},
"profiles": [
{
"network": "A social media or other profile that you would like to include (e.g. LinkedIn, Twitter)",
"username": "Your username on this network",
"url": "A URL to your user profile page"
}
]
},

创建一个JSON类。https://json2csharp.com/

public class Basics
{
public string name { get; set; }
public string label { get; set; }
public string picture { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string degree { get; set; }
public string website { get; set; }
public string summary { get; set; }
public Location location { get; set; }
public List<Profile> profiles { get; set; }
}
public class Location
{
public string address { get; set; }
public string postalCode { get; set; }
public string city { get; set; }
public string countryCode { get; set; }
public string region { get; set; }
}
public class Profile
{
public string network { get; set; }
public string username { get; set; }
public string url { get; set; }
}
public class Root
{
public Basics basics { get; set; }
}

你必须在使用JSON之前对它进行反序列化。

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse)

最新更新