2015-07-21 128 views
0

我试着去创建这个JSON一个序列化字符串:C#JSON newtonsoft转换

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来弹出所有部分?

回答

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作为类其中自动生成的neccessary类你,如果你不想把它们写下来自己:

enter image description here

+0

由于工作的一种享受:) –