c# - 将json字符串反序列化为对象C#.net

标签 c# .net json

我有一个JSON字符串,需要将其反序列化为一个对象。

这是我尝试过的:

类:

public class trn
{
    public string visited_date { get; set; }
    public string party_code { get; set; }
    public string response { get; set; }
    public string response_type { get; set; }
    public string time_stamp { get; set; }
    public string trans_id { get; set; }
    public double total_amount { get; set; }
    public double discount { get; set; }
}


json字符串:

string json = "{\"trn\":{\"visited_date\":\"2015-04-05\",\"party_code\":\"8978a1bf-c88b-11e4-a815-00ff2dce0943\",\"response\":\"Reason 5\",\"response_type\":\"NoOrder\",\"time_stamp\":\"2015-04-05 18:27:42\",\"trans_id\":\"8e15f00b288a701e60a08f968a42a560\",\"total_amount\":0.0,\"discount\":0.0}}";

trn model2 = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<trn>(json);


并使用json.net

trn model = JsonConvert.DeserializeObject<trn>(json);


但是所有属性都使用空值初始化。

最佳答案

您的JSON用trn表示另一个对象内的属性的对象。因此,您也需要在代码中表示它。例如:

using System;
using System.IO;
using Newtonsoft.Json;

public class Transaction
{
    [JsonProperty("visited_date")]
    public DateTime VisitedDate { get; set; }
    [JsonProperty("party_code")]
    public string PartyCode { get; set; }
    [JsonProperty("response")]
    public string Response { get; set; }
    [JsonProperty("response_type")]    
    public string ResponseType { get; set; }
    [JsonProperty("time_stamp")]
    public DateTime Timestamp { get; set; }
    [JsonProperty("trans_id")]
    public string TransactionId { get; set; }
    [JsonProperty("total_amount")]    
    public double TotalAmount { get; set; }
    [JsonProperty("discount")]
    public double Discount { get; set; }
}

public class TransactionWrapper
{
    [JsonProperty("trn")]
    public Transaction Transaction { get; set; }
}

class Test
{
    static void Main(string[] args)
    {
        string json = "{\"trn\":{\"visited_date\":\"2015-04-05\",\"party_code\":\"8978a1bf-c88b-11e4-a815-00ff2dce0943\",\"response\":\"Reason 5\",\"response_type\":\"NoOrder\",\"time_stamp\":\"2015-04-05 18:27:42\",\"trans_id\":\"8e15f00b288a701e60a08f968a42a560\",\"total_amount\":0.0,\"discount\":0.0}}";
        var wrapper = JsonConvert.DeserializeObject<TransactionWrapper>(json);
        Console.WriteLine(wrapper.Transaction.PartyCode);
    }
}


请注意,我是如何使用[JsonProperty]属性允许属性名称本身对.NET而言是惯用的,但是JSON属性名称仍然可以正确使用。我还更改了TransactionVisitedDate的类型。最后,有点令人震惊的是total_amountdiscountdouble值-确实不适合货币值。不幸的是,您可能无法控制它。

关于c# - 将json字符串反序列化为对象C#.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459693/

相关文章:

C# 泛型类型 typedef/alias 和 InvalidCastException

c# - ASP.NET Core CRUD 工具 : command not found but actually installed

java - 断言以随机顺序输出的两个 JSON 结果

JSON 不返回 Spring Data REST 中的列

mysql - 如何在mysql中返回json字符串的值

c# - 使用证书身份验证访问 Web 服务和 HTTP 接口(interface)

c# - 无法让嵌套在 UpdatePanel 中的 WebControl 中的 ScriptManager.RegisterStartupScript 工作

c# - 用于监视多线程 .NET 应用程序的工具、提示和技巧

c# - 如何想出更清晰的接口(interface)名称?

c# - 如何从 C# 读取 Foxpro 8.0 数据库?