2015-01-18 2 views
1

이것이 작동하며 매우 기본입니다. 내 JSON 문자열 (디버거에서)입니다 :단순한 JsonConvert.DeserializeObject가 속성을 지정하지 않습니다.

CompanyWrapper companyWrapper = JsonConvert.DeserializeObject<CompanyWrapper>(json, 
               new JsonSerializerSettings()); 

그리고 CompanyWrapper 클래스는 다음과 같습니다 : 다음과 같이

json "{\"companyId\":0,\"companyName\":\"Windward 3\",\"apiKey\":null,\"isEnabled\":false,\"isActive\":false,\"accruedRtusThisMonth\":0,\"billedRtusThisMonth\":0,\"overageChargesThisMonth\":0.0,\"pricingMode\":3,\"discount\":null,\"billingMode\":1,\"maxAdditionalMonthlyCharge\":123.0,\"billing\":{\"personId\":0,\"companyId\":0,\"isActive\":false,\"isAdmin\":false,\"isBilling\":false,\"firstName\":\"David\",\"lastName\":\"Thielen\",\"address1\":\"1 Main St.\",\"address2\":null,\"city\":\"Boulder\",\"state\":\"CO\",\"country\":\"USA\",\"postalCode\":\"80301\",\"phone\":\"123-456-7890\",\"email\":\"[email protected]\",\"password\":\"tree\"},\"creditCard\":{\"cardNumber\":\"4111111111111111\",\"expiration\":\"2015-02-18T23:37:01.3135786Z\",\"cvv\":\"123\",\"useCardPerson\":false,\"cardPerson\":null},\"nextBaseBillingDate\":\"0001-01-01T00:00:00\",\"nextOverageBillingDate\":\"0001-01-01T00:00:00\",\"billingStatus\":0,\"billingErrorDate\":null,\"deactivateDate\":null,\"deleteDate\":null}" string 

내 코드는

public class CompanyWrapper 
{ 
    /// <summary> 
    /// For the JSON population. 
    /// </summary> 
    public CompanyWrapper() 
    { 
    } 

    /// <summary> 
    /// For unit tests 
    /// </summary> 
    public CompanyWrapper(string companyName, PricingPlan.PRICING_MODE pricingMode, Company.BILLING_MODE billingMode, decimal maxAdditionalMonthlyCharge, PersonWrapper billing, CreditCardWrapper creditCard) 
    { 
     this.companyName = companyName; 
     this.pricingMode = pricingMode; 
     this.billingMode = billingMode; 
     this.maxAdditionalMonthlyCharge = maxAdditionalMonthlyCharge; 
     this.billing = billing; 
     this.creditCard = creditCard; 
    } 

    /// <summary> 
    /// The primary key. This is auto-generated in the database. 
    /// </summary> 
    public int companyId { get; private set; } 

    /// <summary> 
    /// The company name. This cannot be changed. 
    /// </summary> 
    public string companyName { get; private set; } 
... 
} 

반환 companyWrapper.companyName에 == 없는. 그건 배정되어야합니다. 내가 뭘 놓치고 있니?

감사합니다. dave

답변

2

속성을 공개로 설정해야합니다.

+0

Aargh - json.NET은 리플렉션을 사용하여 비공개 설정을 사용하지 않습니까? Dapper는 그렇게하며 모든 코드에 대해 비공개로 유지할 수 있기 때문에 정말 좋습니다. 어쨌든, 그게 다였습니다. 이제 모든 것이 작동합니다. 감사. –

관련 문제