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

时间:2012-06-29 11:03:33

标签: c# json

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

] }

6 个答案:

答案 0 :(得分:40)

我建议您使用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序列化技术的性能比较 enter image description here

答案 1 :(得分:38)

首先创建一个表示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 classe的实例。您的类应该类似于JSON结构(属性)

答案 2 :(得分:37)

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

在Visual Sudio中,点击编辑&gt;粘贴特殊&gt;将Json粘贴为类。

答案 3 :(得分:1)

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

将使用以下cs文件生成一个cs类:

var generatecsResponce = JsonConvert.DeserializeObject(yourJson);

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

答案 4 :(得分:1)

df2['Expected_%']= df2['Sports'].map(df1.set_index('Sports')['Expected_%'])
summed = df2.groupby('Region')['Count'].transform('sum')
df2['Expected_count'] = summed.mul(df2['Expected_%']).div(100)
print (df2)
  Region    Sports  Count  Percentage  Expected_%  Expected_count
0  North   Cricket    800        75.0          70           700.0
1  North  Football     50         5.0          20           200.0
2  North    Tennis    150        20.0          10           100.0
3  South   Cricket   1300        65.0          70          1400.0
4  South  Football    550        27.5          20           400.0
5  South    Tennis    150         7.5          10           200.0

答案 5 :(得分:0)

这将获取一个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);
    }