【问题标题】:How to convert JSON text into objects using C#如何使用 C# 将 JSON 文本转换为对象
【发布时间】:2012-06-29 11:03:32
【问题描述】:

如何将以下 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"]]]
    ]
}

【问题讨论】:

标签: c# json


【解决方案1】:

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

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

【讨论】:

  • 令人惊讶的是,每个人都跳过了这个答案。赞成。
  • 是的,我认为这确实是当时所要求的。在 VS2012 中这是一种迂回的事情(见这里:stackoverflow.com/questions/18526659/…)现在在 VS2017 中,非常简单。
  • 非常有用,在使用 JsonConvert 进行转换时,您可能必须将委托创建为数组。就我而言,这是我必须做的才能将 JSON 字符串转换为类。 JsonConvert.DeserializeObject(FootnoteJSON.Value)
  • 我不认为这是被问到的
  • 我最近发现需要使用 JSON 并偶然发现了这个网站 json2csharp.com,现在我发现 VS 可以为你做这件事。有很好的信息。
【解决方案2】:

首先创建一个类来表示您的 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 序列化器Deserialize a json string 转换为它对应的类对象。

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 结构(属性)

【讨论】:

  • 我无法为该 json 响应构建类
  • @shyju - 太棒了。非常简单直观
【解决方案3】:

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

序列化示例:

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 序列化技术的性能比较

【讨论】:

  • 嗨,Talha,我的问题是我无法构建包含此 json 结果的类的数据结构
  • @Vish:你应该有主类,例如;带有属性的 FlightInfo,其中一个属性应该是 Schedule 列表,例如;列表。附表是第二类,它也有一些属性。正确查看您的 json 字符串
  • @Talha:Ya 但是 schedule 包含什么类型的属性,因为在 schedule 中有数组里面的数组
  • 这不能回答问题
【解决方案4】:

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

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

var generatedcsResponce = JsonConvert.DeserializeObject(yourJson);

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

【讨论】:

【解决方案5】:

这将接受一个 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);
    }

【讨论】:

    【解决方案6】:
    class Program
    {
        static void Main(string[] args)
        {
            var res = Json; //Json that has to be converted
    
            Response resp = new Response();
            resp = JsonSerializer.Deserialize<Response>(res);
    
            Console.WriteLine(res);
        }
    }
    
    public class Response
    {
        public bool isValidUser { get; set; }
        public string message { get; set; }
        public int resultKey { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2016-04-24
      相关资源
      最近更新 更多