数据从一个对象类传输到另一个具有不同格式和属性名称的对象类



我被一个问题困了一个多星期了。我正在使用c#,我正在与我的应用程序进行Zoho集成,我正在将JSON字符串从我的应用程序传递到中间层,中间层将调用Zoho API并发送所需的JSON格式以在Zoho模块中插入记录。

我从我的应用程序发送JSON是:

{
"TonerRequestID": 0,
"CustAccountId": 0,
"CRMAccountId": null,
"CRMODId": null,
"BillTo": "200173",
"Asset": null,
"AssetId": null,
"CreatedDateTime": null,
"CreatedIP": null,
"CreatedBy": null,
"ModifiedBy": null,
"ModifiedIP": null,
"ModifiedDateTime": null,
"Remarks": "",
"CurrStockQty": 0,
"CurrReqQty": 0,
"PrevColorCount": 0,
"PrevBWCount": 0,
"Attachment1": null,
"Attachment1StoredPath": null,
"Serial": "Manual Byepass MB-504         -VIKAS56789                    ",
"SerialId": "371.0",
"lsttonerRequestDetails": [
{
"TonerRequestID": "0",
"CRMProductID": null,
"AssetId": null,
"ItemCode": "80636.0",
"CurrStockQty": "4",
"CreatedDateTime": null,
"RequiredQty": "5",
"PrevStockQty": "12",
"PrevRequiredQty": "3",
"dtLastReqDate": null
},
{
"TonerRequestID": "0",
"CRMProductID": null,
"AssetId": null,
"ItemCode": "80637.0",
"CurrStockQty": "8",
"CreatedDateTime": null,
"RequiredQty": "9",
"PrevStockQty": "6",
"PrevRequiredQty": "7",
"dtLastReqDate": null
},
{
"TonerRequestID": "0",
"CRMProductID": null,
"AssetId": null,
"ItemCode": "80638.0",
"CurrStockQty": "12",
"CreatedDateTime": null,
"RequiredQty": "13",
"PrevStockQty": "10",
"PrevRequiredQty": "11",
"dtLastReqDate": null
},
{
"TonerRequestID": "0",
"CRMProductID": null,
"AssetId": null,
"ItemCode": "80639.0",
"CurrStockQty": "16",
"CreatedDateTime": null,
"RequiredQty": "17",
"PrevStockQty": "14",
"PrevRequiredQty": "15",
"dtLastReqDate": null
},
{
"TonerRequestID": "0",
"CRMProductID": null,
"AssetId": null,
"ItemCode": "80640.0",
"CurrStockQty": "20",
"CreatedDateTime": null,
"RequiredQty": "21",
"PrevStockQty": "18",
"PrevRequiredQty": "19",
"dtLastReqDate": null
}
]
}

现在我想操作这个JSON并创建ZOHO模块想要的实际格式。例如

{    
"data": [
{
"Layout": {
"name" : "Toner",
"id": "207069000003373769"
},
"Owner": {
"id": "207069000001122001"
},
"Asset": {
"id": "207069000025605299"
},
"Account_Name": {
"id": "207069000019985406"
},
"Related_To": {
"id": "207069000026124057"
},
"Contact": {
"id": "207069000026124057"
},
"Current_Black_and_white_Count": "12",
"Current_Color_Count": "12",
"Current_Reading1": "12",
"Current_Reading": "12",
"Last_Spare_Request_Click_Count": "12",
"Last_Spare_Request_Date": "2022-06-09",
"Minimum_Yield": "12",
"No_of_comments": "12",
"Order_No": "12",
"Previous_Reading": "12",
"Previous_Reading1": "12",
"Subject": "Azeem Test Details 2",
"System_Yield": "12",
"Current_Reading_Total": "12",
"Previous_Reading_Total": "12",
"Priority": "Medium",
"Requested_Date_Time": "2022-06-17T18:00:00+05:30",
"Solution": "Complex functionality",
"Standard_Yield": "12",
"Actual_Yield": "12",
"Case_Origin": "12",
"Status": "NEW",            
"Toner_Request_Details": [{
"Current_Stock1": "456",
"Description": "456",
"Item_Code1": {"id":"207069000024880067"},
"Quantity_Req": "456"
},
{
"Current_Stock1": "789",
"Description": "789",
"Item_Code1": {"id":"207069000024880067"},
"Quantity_Req": "789"
}
]
}
]
}

我想让上面的Zoho JSON结构从我的应用程序的JSON,我把在开始。由于每个字段名称都有所不同,因此我使用mapper表来映射来自两个JSON的字段。如:

Image: Fields Mapping for both JSON

所以我要做的是,我将遍历源json字段并设置一个条件来匹配源json字段和目的字段,如果名称匹配,我会将源json字段的值分配给目的字段。

我定义了一个类来反序列化源json,并定义了一个类来将值放入其中并序列化为json并发送给ZOHO。

源类:

public class TonerRequestMaster
{
public Int64 TonerRequestID { get; set; }
public Int64 CustAccountId { get; set; }
public string CRMAccountId { get; set; }
public string CRMODId { get; set; }
public string BillTo { get; set; }
public string Asset { get; set; }
public string AssetId { get; set; }
public DateTime? CreatedDateTime { get; set; }
public string CreatedIP { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public string ModifiedIP { get; set; }
public DateTime? ModifiedDateTime { get; set; }
public string Remarks { get; set; }
public decimal CurrStockQty { get; set; }
public decimal CurrReqQty { get; set; }
public decimal PrevColorCount { get; set; }
public decimal PrevBWCount { get; set; }
public string Attachment1 { get; set; }
public string Attachment1StoredPath { get; set; }
public string Serial { get; set; }
public string SerialId { get; set; }
public List<TonerRequestDetails> lsttonerRequestDetails { get; set; }
}
public class TonerRequestDetails
{
public string TonerRequestID { get; set; }
public string CRMProductID { get; set; }
public string AssetId { get; set; }
public string ItemCode { get; set; }
public string CurrStockQty { get; set; }
public DateTime? CreatedDateTime { get; set; }
public string RequiredQty { get; set; }
public string PrevStockQty { get; set; }
public string PrevRequiredQty { get; set; }
public string dtLastReqDate { get; set; }
}

目的类:

{
public class AccountName
{
public string id { get; set; }
}
public class Asset
{
public string id { get; set; }
}
public class Contact
{
public string id { get; set; }
}
public class Datum
{
public Layout Layout { get; set; }
public Owner Owner { get; set; }
public Asset Asset { get; set; }
public AccountName Account_Name { get; set; }
public RelatedTo Related_To { get; set; }
public Contact Contact { get; set; }
public string Current_Black_and_white_Count { get; set; }
public string Current_Color_Count { get; set; }
public string Current_Reading1 { get; set; }
public string Current_Reading { get; set; }
public string Last_Spare_Request_Click_Count { get; set; }
public string Last_Spare_Request_Date { get; set; }
public string Minimum_Yield { get; set; }
public string No_of_comments { get; set; }
public string Order_No { get; set; }
public string Previous_Reading { get; set; }
public string Previous_Reading1 { get; set; }
public string Subject { get; set; }
public string System_Yield { get; set; }
public string Current_Reading_Total { get; set; }
public string Previous_Reading_Total { get; set; }
public string Priority { get; set; }
public DateTime Requested_Date_Time { get; set; }
public string Solution { get; set; }
public string Standard_Yield { get; set; }
public string Actual_Yield { get; set; }
public string Case_Origin { get; set; }
public string Status { get; set; }
public List<TonerRequestDetail> Toner_Request_Details { get; set; }
}
public class ItemCode1
{
public string id { get; set; }
}
public class Layout
{
public string name { get; set; }
public string id { get; set; }
}
public class Owner
{
public string id { get; set; }
}
public class RelatedTo
{
public string id { get; set; }
}
public class TonerRequestZoho
{
public List<Datum> data { get; set; }
}
public class TonerRequestDetail
{
public string Current_Stock1 { get; set; }
public string Description { get; set; }
public ItemCode1 Item_Code1 { get; set; }
public string Quantity_Req { get; set; }
}
}

请帮我一下,我将不胜感激。

如果你创建了像JSON一样的c#模型层次结构,它会有所帮助,并且你必须使模型具有与JSON相同的名称,例如:


public class Layout 
{
public string Name { get; set; }
public long Id { get; set; }
}
public class Owner 
{
public long Id { get; set; }
}
public class Zoho 
{
public List<ZohoModel> Data { get; set; }
}
public class ZohoModel 
{
public Layout Layout { get; set; }
public Owner Owner { get; set; }
public string Current_Black_and_white_Count { get; set; }
.
.
.
}

相关内容

  • 没有找到相关文章

最新更新