2016-12-12 13 views
0

json2csharpを使用してjson以下のクラスを作成しました。クラスオブジェクトに値を渡すjson2csharpを使用して作成

{ 
    "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":"[email protected]", 
        "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を、私が消費している1つのサービスに渡したいと思います。私は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); 

私が間違って何をやっている場合は、誰もが、この過程で私を導くことができれば、私は感謝。

+0

http://stackoverflow.com/questions/29376491/deserializing-this-json-response-to-c-sharp – McNets

答えて

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); 
+0

あなたは、このプロセスで行わありがとうございました。 –

関連する問題