C# 反序列化此json的正确方法是什么?
我有一个Json文件,看起来像这样C# 反序列化此json的正确方法是什么?,c#,json,C#,Json,我有一个Json文件,看起来像这样 { "itemDetails": [ { "quantity": 2, "name": "1 Hour Massage", "description": null, "code": null, "price": 44.99, "value": 49.99, "expiresOn": null, "expiresInMonths": null,
{
"itemDetails": [
{
"quantity": 2,
"name": "1 Hour Massage",
"description": null,
"code": null,
"price": 44.99,
"value": 49.99,
"expiresOn": null,
"expiresInMonths": null,
"overrideExpiry": false,
"sku": "",
"id": null
}
],
"purchaserEmail": "jane@example.com",
"purchaserName": "Jane Smith",
"recipientDetails": {
"recipientName": "Tommy Smith",
"recipientEmail": "tommy@example.com",
"message": "Happy Holidays!",
"scheduledFor": "2020-12-25T00:00:00"
},
"disableAllEmails": null,
"orderDate": null
}
namespace Api
{
class Order
{
ItemDetails [] itemDetails { get; set; }
string purchaserEmail { get; set; }
string purchaserName { get; set; }
bool disableAllEmails { get; set; }
string orderDate { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Api
{
class ItemDetails
{
int quantity {get; set;}
string name{get; set;}
string description { get; set; }
string code { get; set; }
double price { get; set; }
double value { get; set; }
string expiresOn { get; set; }
string expiresInMonths { get; set; }
bool overrideExpiry { get; set; }
string sku { get; set; }
string id { get; set; }
}
}
我如何去序列化这个Json呢?我已经尝试过为数组创建这样的对象
{
"itemDetails": [
{
"quantity": 2,
"name": "1 Hour Massage",
"description": null,
"code": null,
"price": 44.99,
"value": 49.99,
"expiresOn": null,
"expiresInMonths": null,
"overrideExpiry": false,
"sku": "",
"id": null
}
],
"purchaserEmail": "jane@example.com",
"purchaserName": "Jane Smith",
"recipientDetails": {
"recipientName": "Tommy Smith",
"recipientEmail": "tommy@example.com",
"message": "Happy Holidays!",
"scheduledFor": "2020-12-25T00:00:00"
},
"disableAllEmails": null,
"orderDate": null
}
namespace Api
{
class Order
{
ItemDetails [] itemDetails { get; set; }
string purchaserEmail { get; set; }
string purchaserName { get; set; }
bool disableAllEmails { get; set; }
string orderDate { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Api
{
class ItemDetails
{
int quantity {get; set;}
string name{get; set;}
string description { get; set; }
string code { get; set; }
double price { get; set; }
double value { get; set; }
string expiresOn { get; set; }
string expiresInMonths { get; set; }
bool overrideExpiry { get; set; }
string sku { get; set; }
string id { get; set; }
}
}
像这样
{
"itemDetails": [
{
"quantity": 2,
"name": "1 Hour Massage",
"description": null,
"code": null,
"price": 44.99,
"value": 49.99,
"expiresOn": null,
"expiresInMonths": null,
"overrideExpiry": false,
"sku": "",
"id": null
}
],
"purchaserEmail": "jane@example.com",
"purchaserName": "Jane Smith",
"recipientDetails": {
"recipientName": "Tommy Smith",
"recipientEmail": "tommy@example.com",
"message": "Happy Holidays!",
"scheduledFor": "2020-12-25T00:00:00"
},
"disableAllEmails": null,
"orderDate": null
}
namespace Api
{
class Order
{
ItemDetails [] itemDetails { get; set; }
string purchaserEmail { get; set; }
string purchaserName { get; set; }
bool disableAllEmails { get; set; }
string orderDate { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Api
{
class ItemDetails
{
int quantity {get; set;}
string name{get; set;}
string description { get; set; }
string code { get; set; }
double price { get; set; }
double value { get; set; }
string expiresOn { get; set; }
string expiresInMonths { get; set; }
bool overrideExpiry { get; set; }
string sku { get; set; }
string id { get; set; }
}
}
但这不起作用???我该怎么做对我来说最大的问题是数组,因为我得到的其余部分只是我无法访问aray的数据有没有办法使用ReadAsASync来做到这一点 使用Newtonsoft.Json
public partial class RootObject
{
[JsonProperty("itemDetails")]
public ItemDetail[] ItemDetails { get; set; }
[JsonProperty("purchaserEmail")]
public string PurchaserEmail { get; set; }
[JsonProperty("purchaserName")]
public string PurchaserName { get; set; }
[JsonProperty("recipientDetails")]
public RecipientDetails RecipientDetails { get; set; }
[JsonProperty("disableAllEmails")]
public object DisableAllEmails { get; set; }
[JsonProperty("orderDate")]
public object OrderDate { get; set; }
}
public partial class ItemDetail
{
[JsonProperty("quantity")]
public long Quantity { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("description")]
public object Description { get; set; }
[JsonProperty("code")]
public object Code { get; set; }
[JsonProperty("price")]
public double Price { get; set; }
[JsonProperty("value")]
public double Value { get; set; }
[JsonProperty("expiresOn")]
public object ExpiresOn { get; set; }
[JsonProperty("expiresInMonths")]
public object ExpiresInMonths { get; set; }
[JsonProperty("overrideExpiry")]
public bool OverrideExpiry { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("id")]
public object Id { get; set; }
}
public partial class RecipientDetails
{
[JsonProperty("recipientName")]
public string RecipientName { get; set; }
[JsonProperty("recipientEmail")]
public string RecipientEmail { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("scheduledFor")]
public DateTimeOffset ScheduledFor { get; set; }
}
使用
var Data = JsonConvert.DeserializeObject<RootObject>(json);
var Data=JsonConvert.DeserializeObject(json);
将所有财产标记为公共财产。默认作用域级别是内部的,对于序列化和反序列化,您需要公共属性
另外,如果您使用的是Newtonsoft.Json,由于Json属性大小写是camelCase,请使用如下代码:
JsonConvert.DeserializeObject<Order>(json_string, new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()})
JsonConvert.DeserializeObject(json_字符串,新的JsonSerializerSettings{ContractResolver=new CamelCasePropertyNamesContractResolver()})
相反,您可以在应用程序的开头定义JSON设置,并避免在应用程序中的任何地方通过反序列化对象传递JSON设置
或者您可以使用JsonProperty装饰每个属性,就像Sushant Yelpale在这里提到的那样。如果出于某些原因您想序列化私有字段,我建议这样做。
否则,Sushant Yelpale的答案是正确的,请注意,如果Json中的名称与类中的名称匹配(并且不区分大小写),则您不需要特别使用这些
[JsonProperty(“disableAllEmails”)]
标记。我相信您的Json显示了ItemDetails的数组,但您的类只有一个对象。尝试将itemDetails
更改为itemDetails[]itemDetails{get;set;}
,看看这是否能解决问题。说“不起作用”对我们没有帮助。为什么不起作用?你有错误吗?你的输出错误吗?在什么方面是错误的?如果您不确定要创建什么类,请使用任何将json转换为c#类的网站,json2csharp.comi使用ReadAsAsync反序列化有什么方法吗?确实将单个对象更改为数组,但它给了我一个错误,即没有将ObjeTReference指定给对象的实例。Json非常区分大小写。我可以确保如果您有一个带有“日期”的Json您可以轻松地将Json.net转换为DateTime日期{get;set;}
,至少可以反序列化我正在使用ReadAsAsync来反序列化它是否还有继续使用的余地?我不确定您想做什么,但我想如果您等待ReadAsync返回的任务
,应该可以。我想您只需要确保将一个块中的数据提供给Json.net。