我试图从这个json创建一个序列化字符串:
report_details = {
'reportTypeLang' : 'conversations',
'reportDirections' : {
'selected' : 'inbound'
},
'times' : {
'dateRange' : 'Last5Minutes'
},
'filters' : {
'sdfDips_0' : 'in_AC10033A_AC10033A-410'
},
'dataGranularity' : {
'selected' : 'auto'
}
到目前为止,我已经创建了这些类:
public class ReportDetails
{
public string reportTypeLang { get; set; }
public ReportDirections reportDirections { get; set; }
public Times times { get; set; }
public Filters filters { get; set; }
public DataGranularity dataGranularity { get; set; }
}
public class ReportDirections
{
public string selected { get; set; }
}
public class Times
{
public string dateRange { get; set; }
}
public class Filters
{
public string sdfDips_0 { get; set; }
}
public class DataGranularity
{
public string selected { get; set; }
}
并尝试使用此代码构建数据:
ReportDetails ReportDetails = new ReportDetails();
ReportDetails.reportTypeLang = "conversations";
ReportDirections reportDirections = new ReportDirections();
reportDirections.selected = "inbound";
Times Times = new Times();
Times.dateRange = "Last5Minutes";
Filters Filters = new Filters();
Filters.sdfDips_0 = "in_AC10033A_AC10033A-410";
DataGranularity DataGranularity = new DataGranularity();
DataGranularity.selected = "auto";
string report_details = JsonConvert.SerializeObject(ReportDetails);
但这似乎只会产生这个对象:
"{\"reportTypeLang\":\"conversations\",\"reportDirections\":null,\"times\":null,\"filters\":null,\"dataGranularity\":null}"
如何根据原始json弹出所有部分?
答案 0 :(得分:3)
您没有指定其他属性。因此,他们的序列化值保持为空。
只需添加它们,就像您分配了一个reportTypeLang:
ReportDirections reportDirections = new ReportDirections();
reportDirections.selected = "inbound";
ReportDetails ReportDetails = new ReportDetails();
ReportDetails.reportTypeLang = "conversations";
ReportDetails.reportDirections = reportDirections; // and so with the other props
作为旁注:有一个很酷的功能将JSON粘贴为类,如果你不想自己写下来的话,它会为你自动生成必要的课程:< / p>