小编典典

将JSON文本加载到C#中的类对象中

c#

如何将以下Json Response转换为C#对象?

    { "err_code": "0", "org": "CGK", "des": "SIN", "flight_date": "20120719",
"schedule":
[
["W2-888","20120719","20120719","1200","1600","03h00m","737-200","0",[["K","9"],["F","9"],["L","9"],["M","9"],["N","9"],["P","9"],["C","9"],["O","9"]]],
["W2-999","20120719","20120719","1800","2000","01h00m","MD-83","0",[["K","9"],["L","9"],["M","9"],["N","9"]]]

] }

阅读 273

收藏
2020-05-19

共1个答案

小编典典

首先创建一个代表您的json数据的类。

public class MyFlightDto
{
    public string err_code { get; set; }
    public string org { get; set; } 
    public string flight_date { get; set; }
    // Fill the missing properties for your data
}

使用Newtonsoft JSON序列
化程序将json字符串反

序列化 为其对应的类对象。

var jsonInput = "{ org:'myOrg',des:'hello'}"; 
MyFlightDto flight = Newtonsoft.Json.JsonConvert.DeserializeObject<MyFlightDto>(jsonInput);

JavaScriptSerializer用于将其转换为类( 不建议使用,因为newtonsoft json序列化器似乎表现更好 )。

string jsonInput="have your valid json input here"; //
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Customer objCustomer  = jsonSerializer.Deserialize<Customer >(jsonInput)

假设您要将其转换为类Customer的实例。您的课程应类似于JSON结构(属性)

2020-05-19