2015-04-05 103 views
1

我有一个JSON字符串需要被反序列化到一个对象中。反序列化json字符串到对象c#.net

这是我曾尝试:

类:

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

但所有属性被初始化空值。

+0

退房 http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json -string-在净4 – 2015-04-05 17:08:05

回答

6

您的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_amountdiscount的值是double的值有点令人担忧 - 这实际上不适用于货币值。不幸的是,你可能无法控制。

1

是的@InvernoMuto说,你的JSON字符串是一个嵌套对象属性的对象,内部对象是trn对象。你的JSON字符串需要像这样工作:

string json = "{\"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}"; 

我认为乔恩的答案是更确定但是。

1

你JSON是Dictionary<string,trn>,尝试自己在这个Fiddle

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Newtonsoft.Json; 



public class Program 
{ 

    public static void Main() 
    { 
     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 p = JsonConvert.DeserializeObject<Dictionary<string,trn>>(json); 



     Console.WriteLine(p["trn"].party_code); 
    } 
    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; } 
    } 


}