将值传递给类对象使用json2csharp创建

时间:2016-12-12 13:59:31

标签: c# json json2csharp

我使用json2csharp创建了json下面的类。

{  
   "saveExistingRetailCustomerReq":{  
      "reqHdr":{  
         "consumerContext":{  
            "applicationId":"CRM",
            "terminalId":"12345"
         },
         "serviceContext":{  
            "uniqueMsgId":"111121362162",
            "reqMsgDateTime":"2016-09-02T11:19:15.754",
            "timeZone":"2016-09-02T11:19:15.754",
            "serviceName":"saveExistingRetailCustomer",
            "serviceVersion":"1",
            "conversationId":"2121211",
            "parentMsgId":" 3132121"
         },
         "providerContext":{  
            "providerId":"CBS"
         },
         "userContext":{  
            "userId":" 132123"
         },
         "additionalDetails":{  
            "details":"1"
         }
      },
      "body":{  
         "customerDtls":{  
            "custId":"22551",
            "firstName":"First",
            "lastName":"Last",
            "middleName":"Middle",
            "customerStatus":"DCSED",
            "dateOfNotification":"2016-10-01T00:00:00.000",
            "dateOfDeath":"2016-10-01T00:00:00.000",
            "isSuspended":"Y",
            "isNegated":"N",
            "isBlacklisted":"N",
            "gender":"",
            "dateOfBirth":"1991-08-12T00:00:00.000",
            "nativeLanguage":"Language",
            "occupation":"",
            "preferredName":"preferred",
            "shortName":"short",
            "primarySolId":"1100",
            "title":"",
            "SMSBankingMobNum":"9092480244",
            "IsSMSBankingEnabled":"N",
            "IsEBankingEnabled":"N",
            "staffEmployeeID":"",
            "staffFlag":"N",
            "relatedDtls":{  
               "employmentStatus":"Salaried",
               "maritalStatus":"002",
               "nationality":"F"
            },
            "communicationDtls":{  
               "phoneDtls":{  
                  "cityCode":"91",
                  "countryCode":"91",
                  "mobileNumber":"9092480244",
                  "phoneOrEmail":"PHONE",
                  "prefFlag":"Y",
                  "type":"HOMEPH1"
               },
               "emailDtls":{  
                  "email":"test@email.com",
                  "type":"HOMEEML",
                  "phoneOrEmail":"EMAIL",
                  "prefFlag":"Y"
               }
            },
            "addressDtls":[  
               {  
                  "addressLine1":"AddressLine1",
                  "addressLine2":"AddressLine2",
                  "addressLine3":"AddressLine3",
                  "addressCategory":"Mailing",
                  "city":"C001",
                  "country":"IN",
                  "freeTextLabel":"RETAL",
                  "preferredAddress":"N",
                  "preferredFormat":"FREE_TEXT_FORMAT",
                  "startDt":"2005-07-02T00:00:00.000",
                  "state":"S001",
                  "postalCode":"507001"
               },
               {  
                  "addressLine1":"Line1Address",
                  "addressLine2":"Line2Address",
                  "addressLine3":"Line3Address",
                  "addressCategory":"Swift",
                  "city":"C001",
                  "country":"IN",
                  "freeTextLabel":"RETAL",
                  "preferredAddress":"Y",
                  "preferredFormat":"FREE_TEXT_FORMAT",
                  "startDt":"2005-07-02T00:00:00.000",
                  "state":"S001",
                  "postalCode":"507001"
               }
            ],
            "documentDtls":[  
               {  
                  "countryOfIssue":"IN",
                  "docCode":"IDPC",
                  "issueDt":"2015-09-12T00:00:00.000",
                  "type":"ID",
                  "placeOfIssue":"C001",
                  "referenceNum":"XYZ123VW45",
                  "preferredUniqueId":"Y",
                  "idIssuedOrganisation":"GOI"
               },
               {  
                  "countryOfIssue":"IN",
                  "docCode":"IDVC",
                  "issueDt":"2015-08-12T00:00:00.000",
                  "type":"ID",
                  "placeOfIssue":"C001",
                  "referenceNum":"DEF123VW45",
                  "preferredUniqueId":"N",
                  "idIssuedOrganisation":"GOI"
               }
            ]
         }
      }
   }
}

以下是json2Csharp创建的类。

using System.Collections.Generic;

namespace CRMnext.Ujjivan.DLL
{
    public class ConsumerContext
    {
        public string applicationId { get; set; }
        public string terminalId { get; set; }
    }

    public class ServiceContext
    {
        public string uniqueMsgId { get; set; }
        public string reqMsgDateTime { get; set; }
        public string timeZone { get; set; }
        public string serviceName { get; set; }
        public string serviceVersion { get; set; }
        public string conversationId { get; set; }
        public string parentMsgId { get; set; }
    }

    public class ProviderContext
    {
        public string providerId { get; set; }
    }

    public class UserContext
    {
        public string userId { get; set; }
    }

    public class AdditionalDetails
    {
        public string details { get; set; }
    }

    public class ReqHdr
    {
        public ConsumerContext consumerContext { get; set; }
        public ServiceContext serviceContext { get; set; }
        public ProviderContext providerContext { get; set; }
        public UserContext userContext { get; set; }
        public AdditionalDetails additionalDetails { get; set; }
    }

    public class RelatedDtls
    {
        public string employmentStatus { get; set; }
        public string maritalStatus { get; set; }
        public string nationality { get; set; }
    }

    public class PhoneDtls
    {
        public string cityCode { get; set; }
        public string countryCode { get; set; }
        public string mobileNumber { get; set; }
        public string phoneOrEmail { get; set; }
        public string prefFlag { get; set; }
        public string type { get; set; }
    }

    public class EmailDtls
    {
        public string email { get; set; }
        public string type { get; set; }
        public string phoneOrEmail { get; set; }
        public string prefFlag { get; set; }
    }

    public class CommunicationDtls
    {
        public PhoneDtls phoneDtls { get; set; }
        public EmailDtls emailDtls { get; set; }
    }

    public class AddressDtl
    {
        public string addressLine1 { get; set; }
        public string addressLine2 { get; set; }
        public string addressLine3 { get; set; }
        public string addressCategory { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string freeTextLabel { get; set; }
        public string preferredAddress { get; set; }
        public string preferredFormat { get; set; }
        public string startDt { get; set; }
        public string state { get; set; }
        public string postalCode { get; set; }
    }

    public class DocumentDtl
    {
        public string countryOfIssue { get; set; }
        public string docCode { get; set; }
        public string issueDt { get; set; }
        public string type { get; set; }
        public string placeOfIssue { get; set; }
        public string referenceNum { get; set; }
        public string preferredUniqueId { get; set; }
        public string idIssuedOrganisation { get; set; }
    }

    public class CustomerDtls
    {
        public string custId { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string middleName { get; set; }
        public string customerStatus { get; set; }
        public string dateOfNotification { get; set; }
        public string dateOfDeath { get; set; }
        public string isSuspended { get; set; }
        public string isNegated { get; set; }
        public string isBlacklisted { get; set; }
        public string gender { get; set; }
        public string dateOfBirth { get; set; }
        public string nativeLanguage { get; set; }
        public string occupation { get; set; }
        public string preferredName { get; set; }
        public string shortName { get; set; }
        public string primarySolId { get; set; }
        public string title { get; set; }
        public string SMSBankingMobNum { get; set; }
        public string IsSMSBankingEnabled { get; set; }
        public string IsEBankingEnabled { get; set; }
        public string staffEmployeeID { get; set; }
        public string staffFlag { get; set; }
        public RelatedDtls relatedDtls { get; set; }
        public CommunicationDtls communicationDtls { get; set; }
        public List<AddressDtl> addressDtls { get; set; }
        public List<DocumentDtl> documentDtls { get; set; }
    }

    public class Body
    {
        public CustomerDtls customerDtls { get; set; }
    }

    public class SaveExistingRetailCustomerReq
    {
        public ReqHdr reqHdr { get; set; }
        public Body body { get; set; }
    }

    public class RootObject
    {
        public SaveExistingRetailCustomerReq saveExistingRetailCustomerReq { get; set; }

    }
}

在下一步中,我想在此创建的类中传递值。之后通过序列化这个类的对象,我想将序列化的Json传递给我正在使用的一个服务。我在下面通过引用Turn C# object into a JSON string in .NET 4尝试过。但是获取错误对象引用未设置为对象的实例。

RootObject rootObject = new RootObject();  
          rootObject.saveExistingRetailCustomerReq.reqHdr.consumerContext.applicationId = "CRM";
var jsonData = JsonConvert.SerializeObject(rootObject);

如果我做错了,那么如果有人能在这个过程中指导我,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要创建所有对象,而不仅仅是RootObject。

        RootObject rootObject = new RootObject();
        rootObject.saveExistingRetailCustomerReq = new SaveExistingRetailCustomerReq();
        rootObject.saveExistingRetailCustomerReq.reqHdr = new ReqHdr();
        rootObject.saveExistingRetailCustomerReq.reqHdr.consumerContext = new ConsumerContext();
        rootObject.saveExistingRetailCustomerReq.reqHdr.consumerContext.applicationId = "CRM";
        var jsonData = JsonConvert.SerializeObject(rootObject);