이 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에 따라 모든 섹션을 어떻게 채웁니까?
다른 속성을 할당하지 않았습니다. 따라서 직렬화 된 값은 null로 유지됩니다.
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을 클래스로 붙여 넣는 멋진 기능이 있어 필요한 클래스를 직접 작성하고 싶지 않은 경우 자동 생성됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다