将json字符串反序列化为对象c#.net
我有一个JSON字符串,需要反序列化为一个对象 这就是我尝试过的: 类别:将json字符串反序列化为对象c#.net,c#,.net,json,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
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);
string json=“{\'trn\”:{“访问日期”:“2015-04-05\”,“参与方代码”:“8978a1bf-c88b-11e4-a815-00ff2dce0943\”,“响应”:“原因5\”,“响应类型”:“无订单”,“时间戳”:“2015-04-05 18:27:42\,“trans-U id\”:“8E15B288A70E60A08U:“总折扣金额”;“F960.0”;
trn model2=new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(json);
以及使用json.net
trn model = JsonConvert.DeserializeObject<trn>(json);
trn model=JsonConvert.DeserializeObject(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);
}
}
使用系统;
使用System.IO;
使用Newtonsoft.Json;
公共类事务
{
[JsonProperty(“访问日期”)]
公共日期时间访问日期{get;set;}
[JsonProperty(“第三方代码”)]
公共字符串PartyCode{get;set;}
[JsonProperty(“响应”)]
公共字符串响应{get;set;}
[JsonProperty(“响应类型”)]
公共字符串响应类型{get;set;}
[JsonProperty(“时间戳”)]
公共日期时间时间戳{get;set;}
[JsonProperty(“trans_id”)]
公共字符串TransactionId{get;set;}
[JsonProperty(“总金额”)]
公共双重合计金额{get;set;}
[JsonProperty(“折扣”)]
公共双折扣{get;set;}
}
公共类TransactionWrapper
{
[JsonProperty(“trn”)]
公共事务{get;set;}
}
课堂测试
{
静态void Main(字符串[]参数)
{
字符串json=“{\'trn\”:{\'visited\u date\”:“2015-04-05\”,“party\u code\”:“8978a1bf-c88b-11e4-a815-00ff2dce0943\”,“response\”:“response\u type\”,“response\u type\”:“NoOrder\”,“time\u stamp\”:“2015-04-05 18:27:42\,“trans\u id\”:“8E15F00B288A70E60A08F968\”,“total\0\”,“折扣金额”;“总金额”;
var wrapper=JsonConvert.DeserializeObject(json);
Console.WriteLine(wrapper.Transaction.PartyCode);
}
}
请注意,我是如何使用
[JsonProperty]
属性来允许属性名称本身成为.NET的惯用名称,但仍然可以适当地使用JSON属性名称的。我还更改了事务
和访问日期
的类型。最后,值得注意的是,total_amount
和discount
是double
的值-这确实不适用于货币值。不幸的是,您可能无法控制它。您的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);
}
}
使用系统;
使用System.IO;
使用Newtonsoft.Json;
公共类事务
{
[JsonProperty(“访问日期”)]
公共日期时间访问日期{get;set;}
[JsonProperty(“第三方代码”)]
公共字符串PartyCode{get;set;}
[JsonProperty(“响应”)]
公共字符串响应{get;set;}
[JsonProperty(“响应类型”)]
公共字符串响应类型{get;set;}
[JsonProperty(“时间戳”)]
公共日期时间时间戳{get;set;}
[JsonProperty(“trans_id”)]
公共字符串TransactionId{get;set;}
[JsonProperty(“总金额”)]
公共双重合计金额{get;set;}
[JsonProperty(“折扣”)]
公共双折扣{get;set;}
}
公共类TransactionWrapper
{
[JsonProperty(“trn”)]
公共事务{get;set;}
}
课堂测试
{
静态void Main(字符串[]参数)
{
字符串json=“{\'trn\”:{\'visited\u date\”:“2015-04-05\”,“party\u code\”:“8978a1bf-c88b-11e4-a815-00ff2dce0943\”,“response\”:“response\u type\”,“response\u type\”:“NoOrder\”,“time\u stamp\”:“2015-04-05 18:27:42\,“trans\u id\”:“8E15F00B288A70E60A08F968\”,“total\0\”,“折扣金额”;“总金额”;
var wrapper=JsonConvert.DeserializeObject(json);
Console.WriteLine(wrapper.Transaction.PartyCode);
}
}
请注意,我是如何使用
[JsonProperty]
属性来允许属性名称本身成为.NET的惯用名称,但仍然可以适当地使用JSON属性名称的。我还更改了事务
和访问日期
的类型。最后,值得注意的是,total_amount
和discount
是double
的值-这确实不适用于货币值。不幸的是,您可能无法控制它。是的,正如@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}";
然而,我认为Jon的答案更明确。是的,正如@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}";
不过,我认为Jon的答案要明确得多。你的JSON是一本
字典,你可以试试这个
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Newtonsoft.Json;
公共课程
{
公共静态void Main()
{
字符串json=“{\'trn\”:{\'visited\u date\”:“2015-04-05\”,“party\u code\”:“8978a1bf-c88b-11e4-a815-00ff2dce0943\”,“response\”:“response\u type\”,“response\u type\”:“NoOrder\”,“time\u stamp\”:“2015-04-05 18:27:42\,“trans\u id\”:“8E15F00B288A70E60A08F968\”,“total\0\”,“折扣金额”;“总金额”;
var p=JsonConvert.DeserializeObject(json);
Console.WriteLine(p[“trn”]。第三方代码);
}
公共类trn
{
访问的公共字符串\u日期{get;set;}
公共字符串参与方代码{get;set;}
公共字符串