2016-11-01 6 views
3

웹 서버에서 Json 데이터를 가져오고 있지만 객체에 비 직렬화하려고하면 데이터가 없습니다. json으로 문자열은 다음과 같습니다 recommendationJson 비 직렬화에서 객체 데이터를 가져 오지 않습니다.

나는이 클래스 만드는 데 사용 : json2csharp

{"success":true,"data":[{"Id":6,"CustomerGuid":"70b390d8-82d5-4bba-aa68-fc8268a1b1ff","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472393)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472393)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":5,"CustomerGuid":"eb9e6f24-f362-4c10-942a-366e2919dc11","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472363)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472363)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":4,"CustomerGuid":"9f46dbae-6942-410c-90b8-9b38a0890064","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472317)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472317)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":3,"CustomerGuid":"6277386b-13ee-427b-9cfe-4ebfa487c340","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472253)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472253)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":2,"CustomerGuid":"241f45f1-b38c-4e22-8c5a-743fa3276620","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472207)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472207)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":1,"CustomerGuid":"a940dc03-5f52-47d2-9391-8597b3b31cf2","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":1,"Name":"Administrators","SystemName":"Administrators"},{"Id":2,"Name":"Forum Moderators","SystemName":"ForumModerators"},{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":true,"Active":true,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":"71.185.255.7","CreatedOnUtc":"\/Date(1472933470783)\/","LastLoginDateUtc":"\/Date(1477522483903)\/","LastActivityDateUtc":"\/Date(1477523996553)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[{"Id":1,"StoreId":1,"ShoppingCartTypeId":1,"CustomerId":1,"ProductId":18,"AttributesXml":null,"CustomerEnteredPrice":0.0000,"Quantity":1,"CreatedOnUtc":"\/Date(1473801903447)\/","UpdatedOnUtc":"\/Date(1473803336207)\/","IsFreeShipping":false,"IsShipEnabled":true,"AdditionalShippingCharge":0.0000,"IsTaxExempt":false}]}]}

내가이 링크에 주어진 권고에서 이러한 클래스를 생성

응답 클래스 :

class Response 
{ 
    bool success; 
    IList<Customer> data; 
} 

고객 클래스 :

class Customer 
{ 
    public int Id { get; set; } 
    public string CustomerGuid { get; set; } 
    public string UserName { get; set; } 
    public string Email { get; set; } 
    public List<CustomerRole> CustomerRoles { get; set; } 
    public object AdminComment { get; set; } 
    public bool IsTaxExempt { get; set; } 
    public int AffiliateId { get; set; } 
    public int VendorId { get; set; } 
    public bool HasShoppingCartItems { get; set; } 
    public bool Active { get; set; } 
    public bool Deleted { get; set; } 
    public bool IsSystemAccount { get; set; } 
    public object SystemName { get; set; } 
    public string LastIpAddress { get; set; } 
    public DateTime CreatedOnUtc { get; set; } 
    public DateTime? LastLoginDateUtc { get; set; } 
    public DateTime LastActivityDateUtc { get; set; } 
    public List<object> ExternalAuthenticationRecords { get; set; } 
    public List<object> ShoppingCartItems { get; set; } 

} 

CustomerRole 클래스 :

class CustomerRole 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string SystemName { get; set; } 

} 

ExternalAuthenticationRecord 클래스 :

class ExternalAuthenticationRecord 
{ 
    public int Id { get; set; } 
    public int CustomerId { get; set; } 
    public string Email { get; set; } 
    public object ExternalIdentifier { get; set; } 
    public object ExternalDisplayIdentifier { get; set; } 
    public object OAuthToken { get; set; } 
    public object OAuthAccessToken { get; set; } 
    public string ProviderSystemName { get; set; } 
} 

ShoppingCartItem 클래스 :

class ShoppingCartItem 
{ 
    public int Id { get; set; } 
    public int StoreId { get; set; } 
    public int ShoppingCartTypeId { get; set; } 
    public int CustomerId { get; set; } 
    public int ProductId { get; set; } 
    public object AttributesXml { get; set; } 
    public double CustomerEnteredPrice { get; set; } 
    public int Quantity { get; set; } 
    public DateTime CreatedOnUtc { get; set; } 
    public DateTime UpdatedOnUtc { get; set; } 
    public bool IsFreeShipping { get; set; } 
    public bool IsShipEnabled { get; set; } 
    public double AdditionalShippingCharge { get; set; } 
    public bool IsTaxExempt { get; set; } 

} 
,536,913 63,210

내가 JSON 문자열 deserialzie이 문을 사용하고 있습니다 : 내가 디버거를 중지하면 Response res = (Response)JsonConvert.DeserializeObject(customerJson, (typeof(Response)));

를,이 데이터로 "고해상도"를 보여줍니다 널과 성공 : 거짓.

오류가 발생하지 않습니다. Json 문자열의 데이터를 제공하지 않습니다.

누구나 내가 "입술"에서 원하는 데이터를 얻지 못하는 이유를 알아낼 수있는 도움을 주시면 감사하겠습니다.

감사합니다, 토니

+1

입니다. 질문을 [mcve]로 수정하려면 잠시 시간을내어 JSON과 모든 코드의 형식을 지정하고 최소한으로 만들고 우리가 문제를 재현 할 수 있도록 완료하십시오. –

+2

'Response res = JsonConvert.DeserializeObject (customerJson); ' – Equalsk

+0

@Equalsk - 당신의 방법을 시도했는데 같은 결과를 얻었습니다. 내 성명을 작성하는 데이 가이드를 사용했습니다. [http://www.newtonsoft.com/json/help/html/DeserializeObject.htm]. 감사. – Tony

답변

4

문제는 당신 Response 클래스의 접근성 수준과 관련이 있습니다. 기본적으로 필드, 속성 및 메서드는 private이므로 JsonConvert는 속성을 채울 수 없습니다.
변경 다음과 같은 클래스 :

class Response 
{ 
    public bool success {get; set;} 
    public IList<Customer> data {get; set;} 
} 

그리고 작품을 줘야 해.
또 다른 개선점은 JsonConvert 사용과 관련이 있습니다. 명시 적 형 변환을 피하려면이 유형 변환을 사용하십시오. JsonConvert.DeserializeObject<T>(string) 여기서 T는 Response

+0

나는 당신이 추천 한 것을 시도했고 나는 같은 결과를 얻었다. 데이터 : null 및 성공 : false. 당신의 도움을 주셔서 감사합니다. – Tony

+0

죄송합니다. 속성을 '공개'하는 것을 잊어 버렸습니다.그것은 당신이 권장하는대로 작동합니다. 나는 지금 Json 문자열로부터 데이터를 얻고있다. 당신의 도움을 주셔서 감사합니다. – Tony