json C# 中的动态节点解析
json dynamic node parse in C#
我正在接收 json 基于扇区的动态节点,如“DEL-BOM”、“NYC-BOM”。
试图解析但正文为空。
我只是将字典和键添加为节点,将 class 添加为值,但仍然没有在 DeserializeObject.
中获取值
{
"fareRule": {
"DEL-BOM": {
"fr": {
"NO_SHOW": {
"DEFAULT": {
"policyInfo": "If Cancelled within 6 hrs of scheduled departure only statutory taxes will be Refunded."
}
},
"DATECHANGE": {
"DEFAULT": {
"amount": 3000.00,
"additionalFee": 50.00,
"policyInfo": "__nls__Changes permitted 06 Hrs before scheduled departure __nls__ Within 06-96 hrs Rs 3,000 + Fare Difference __nls__ Before 96 hrs Rs 2,500 + Fare Difference",
"fcs": {
"CRFT": 9.00,
"ARF": 3000.00,
"ARFT": 150.00,
"CRF": 50.00
}
}
},
"CANCELLATION": {
"DEFAULT": {
"amount": 3500.00,
"additionalFee": 50.00,
"policyInfo": "__nls__Cancellation permitted 06 Hrs before scheduled departure __nls__ Within 06-96 hrs Rs 3,500 __nls__ Before 96 hrs Rs 3,000",
"fcs": {
"ACFT": 175.00,
"CCFT": 9.00,
"ACF": 3500.00,
"CCF": 50.00
}
}
},
"SEAT_CHARGEABLE": {
"DEFAULT": {
"policyInfo": "Paid Seat"
}
}
}
}
},
"status": {
"success": true,
"httpStatus": 200
}
}
这是我的class
public class Response
{
public Dictionary<string, Fr> fareRule { get; set; }
public Status status { get; set; }
}
public class DEFAULT
{
public string policyInfo { get; set; }
public double amount { get; set; }
public double additionalFee { get; set; }
public Fcs fcs { get; set; }
}
public class NOSHOW
{
public DEFAULT DEFAULT { get; set; }
}
public class Fcs
{
public double ARFT { get; set; }
public double ARF { get; set; }
public double CRFT { get; set; }
public double CRF { get; set; }
public double CCF { get; set; }
public double ACF { get; set; }
public double CCFT { get; set; }
public double ACFT { get; set; }
}
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Fr
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
public class Sector
{
public Fr fr { get; set; }
}
public class FareRule
{
public Sector sector { get; set; }
}
public class Status
{
public bool success { get; set; }
public int httpStatus { get; set; }
}
我试过基于
键正在转换,但正文或值为空。
试试这个,它已经过测试并且工作正常
var jsonDeserialized = JsonConvert.DeserializeObject<Data>(json);
类
public class Data
{
public Dictionary<string,Dictionary<string, Mode>> fareRule { get; set; }
public Status status { get; set; }
}
public class Status
{
public bool? success { get; set; }
public int? httpStatus { get; set; }
}
public class DEFAULT
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string policyInfo { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public double? amount { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public double? additionalFee { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string,double?> fcs { get; set; }
}
public class NOSHOW
{
public DEFAULT DEFAULT { get; set; }
}
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Mode
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
使用Newtonsoft.Json进行反序列化
您的正确型号是。
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Fr
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
public class FareRule
{
[JsonProperty(PropertyName = "DEL-BOM")]
public DynamicBody DELBOM { get; set; }
[JsonProperty(PropertyName = "NYC-BOM")]
public DynamicBody NYCBOM { get; set; }
}
public class Status
{
public bool Success { get; set; }
public int HttpStatus { get; set; }
}
public class BaseResponseModel
{
public FareRule FareRule { get; set; }
public Status Status { get; set; }
}
public class DynamicBody
{
public Fr Fr { get; set; }
}
然后像这样反序列化代码
var responseModel = JsonConvert.DeserializeObject<BaseResponseModel>(jsonStr);
我正在接收 json 基于扇区的动态节点,如“DEL-BOM”、“NYC-BOM”。 试图解析但正文为空。 我只是将字典和键添加为节点,将 class 添加为值,但仍然没有在 DeserializeObject.
中获取值{
"fareRule": {
"DEL-BOM": {
"fr": {
"NO_SHOW": {
"DEFAULT": {
"policyInfo": "If Cancelled within 6 hrs of scheduled departure only statutory taxes will be Refunded."
}
},
"DATECHANGE": {
"DEFAULT": {
"amount": 3000.00,
"additionalFee": 50.00,
"policyInfo": "__nls__Changes permitted 06 Hrs before scheduled departure __nls__ Within 06-96 hrs Rs 3,000 + Fare Difference __nls__ Before 96 hrs Rs 2,500 + Fare Difference",
"fcs": {
"CRFT": 9.00,
"ARF": 3000.00,
"ARFT": 150.00,
"CRF": 50.00
}
}
},
"CANCELLATION": {
"DEFAULT": {
"amount": 3500.00,
"additionalFee": 50.00,
"policyInfo": "__nls__Cancellation permitted 06 Hrs before scheduled departure __nls__ Within 06-96 hrs Rs 3,500 __nls__ Before 96 hrs Rs 3,000",
"fcs": {
"ACFT": 175.00,
"CCFT": 9.00,
"ACF": 3500.00,
"CCF": 50.00
}
}
},
"SEAT_CHARGEABLE": {
"DEFAULT": {
"policyInfo": "Paid Seat"
}
}
}
}
},
"status": {
"success": true,
"httpStatus": 200
}
}
这是我的class
public class Response
{
public Dictionary<string, Fr> fareRule { get; set; }
public Status status { get; set; }
}
public class DEFAULT
{
public string policyInfo { get; set; }
public double amount { get; set; }
public double additionalFee { get; set; }
public Fcs fcs { get; set; }
}
public class NOSHOW
{
public DEFAULT DEFAULT { get; set; }
}
public class Fcs
{
public double ARFT { get; set; }
public double ARF { get; set; }
public double CRFT { get; set; }
public double CRF { get; set; }
public double CCF { get; set; }
public double ACF { get; set; }
public double CCFT { get; set; }
public double ACFT { get; set; }
}
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Fr
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
public class Sector
{
public Fr fr { get; set; }
}
public class FareRule
{
public Sector sector { get; set; }
}
public class Status
{
public bool success { get; set; }
public int httpStatus { get; set; }
}
我试过基于
键正在转换,但正文或值为空。
试试这个,它已经过测试并且工作正常
var jsonDeserialized = JsonConvert.DeserializeObject<Data>(json);
类
public class Data
{
public Dictionary<string,Dictionary<string, Mode>> fareRule { get; set; }
public Status status { get; set; }
}
public class Status
{
public bool? success { get; set; }
public int? httpStatus { get; set; }
}
public class DEFAULT
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string policyInfo { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public double? amount { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public double? additionalFee { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string,double?> fcs { get; set; }
}
public class NOSHOW
{
public DEFAULT DEFAULT { get; set; }
}
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Mode
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
使用Newtonsoft.Json进行反序列化 您的正确型号是。
public class DATECHANGE
{
public DEFAULT DEFAULT { get; set; }
}
public class CANCELLATION
{
public DEFAULT DEFAULT { get; set; }
}
public class SEATCHARGEABLE
{
public DEFAULT DEFAULT { get; set; }
}
public class Fr
{
public NOSHOW NO_SHOW { get; set; }
public DATECHANGE DATECHANGE { get; set; }
public CANCELLATION CANCELLATION { get; set; }
public SEATCHARGEABLE SEAT_CHARGEABLE { get; set; }
}
public class FareRule
{
[JsonProperty(PropertyName = "DEL-BOM")]
public DynamicBody DELBOM { get; set; }
[JsonProperty(PropertyName = "NYC-BOM")]
public DynamicBody NYCBOM { get; set; }
}
public class Status
{
public bool Success { get; set; }
public int HttpStatus { get; set; }
}
public class BaseResponseModel
{
public FareRule FareRule { get; set; }
public Status Status { get; set; }
}
public class DynamicBody
{
public Fr Fr { get; set; }
}
然后像这样反序列化代码
var responseModel = JsonConvert.DeserializeObject<BaseResponseModel>(jsonStr);