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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11260631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:10:13  来源:igfitidea点击:

Load JSON text into class object in c#

c#json

提问by Vishwajeet

How can I convert the following Json Response to a C# object?

如何将以下 Json 响应转换为 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"]]]

] }

采纳答案by Shyju

First create a class to represent your json data.

首先创建一个类来表示您的 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
}

Using Newtonsoft JSON serializerto Deserialize a json stringto it's corresponding class object.

使用 Newtonsoft JSON 序列化程序将 json 字符串反序列化为其相应的类对象。

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

Or Use JavaScriptSerializerto convert it to a class(not recommended as the newtonsoft json serializer seems to perform better).

JavaScriptSerializer用于将其转换为类(不推荐,因为 newtonsoft json 序列化程序似乎性能更好)。

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

Assuming you want to convert it to a Customerclasse's instance. Your class should looks similar to the JSONstructure (Properties)

假设您想将其转换为类Customer的实例。你的类应该看起来类似于JSON结构(属性)

回答by Talha

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 ...

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

Serialization Example:

序列化示例:

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);

Performance Comparison To Other JSON serializiation Techniquesenter image description here

与其他 JSON 序列化技术的性能比较在此处输入图片说明

回答by ff0000e2

This will take a json string and turn it into any class you specify

这将采用一个 json 字符串并将其转换为您指定的任何类

public static T ConvertJsonToClass<T>(this string json)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return serializer.Deserialize<T>(json);
    }

回答by Ole EH Dufour

To create a json class off a string, copy the string.

要从字符串创建 json 类,请复制该字符串。

In Visual Sudio, click Edit > Paste special > Paste Json as classes.

在 Visual Sudio 中,单击编辑 > 选择性粘贴 > 将 Json 粘贴为类。

回答by Islam

copy your Json and paste at textbox on http://json2csharp.com/and click on Generate button,

复制您的 Json 并粘贴到http://json2csharp.com/上的文本框,然后单击“生成”按钮,

A cs class will be generated use that cs file as below:

将使用该 cs 文件生成一个 cs 类,如下所示:

var generatedcsResponce = JsonConvert.DeserializeObject(yourJson);

var generatecsResponce = JsonConvert.DeserializeObject(yourJson);

where RootObject is the name of the generated cs file;

其中 RootObject 是生成的 cs 文件的名称;