将JSON文本转换为对象

将JSON文本转换为对象

本文介绍了如何使用C#将JSON文本转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下JSON响应转换为C#对象?

How can I convert the following JSON response to a C# object?

{
    "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"]]]
    ]
}

推荐答案

我建议您使用 JSON.NET .它是一个开放源代码库,用于将c#对象序列化和反序列化为json,将Json对象序列化为.net对象...

I recommend you to use JSON.NET. it is an open source library to serialize and deserialize your c# objects into json and Json objects into .net objects ...

序列化示例:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": new Date(1230422400000),
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);

与其他JSON序列化技术的性能比较

这篇关于如何使用C#将JSON文本转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:11