2016-06-26 4 views
-1

JSON을 객체 "MarginBalance"로 비 직렬화하려고합니다. 이 개체에C# .NET 객체에 JSON 문자열의 비 직렬화

{"totalValue":"0.00091979","pl":"0.00000000","lendingFees":"0.00000000","netValue":"0.00091979","totalBorrowedValue":"0.00000000","currentMargin":"1.00000000"} 

:

public class MarginBalance : IMarginBalance 
{ 
    [JsonProperty("totalValue")] 
    public double TotalValue { get; set; } 

    [JsonProperty("pl")] 
    public double PL { get; set; } 

    [JsonProperty("lendingFees")] 
    public double LendingFees { get; set; } 

    [JsonProperty("netValue")] 
    public double NetValue { get; set; } 

    [JsonProperty("totalBorrowedValue")] 
    public double TotalBorrowedValue { get; set; } 

    [JsonProperty("currentMargin")] 
    public double CurrentMargin { get; set; } 
} 

이 인터페이스를 구현 :이 JSON을 역 직렬화 할 때

public interface IMarginBalance 
{ 
    double TotalValue { get; } 
    double PL { get; } 
    double LendingFees { get; } 
    double NetValue { get; } 
    double TotalBorrowedValue { get; } 
    double CurrentMargin { get; } 
} 

는 null를 돌려줍니다. 내 비 직렬화 코드는 다음과 같습니다.

var postData = new Dictionary<string, object>(); 
var data = PostData<IDictionary<string, MarginBalance>>("returnMarginAccountSummary", postData); 
if (data != null) 
{ 
    // never reaches here 
    var returnData = new Dictionary<string, IMarginBalance>(); 
    foreach (string key in data.Keys) 
    { 
     returnData.Add(key, data[key]); 
    } 
    return returnData; 
} 

[MethodImpl(MethodImplOptions.AggressiveInlining)] 
private T PostData<T>(string command, Dictionary<string, object> postData) 
{ 
    return ApiWebClient.PostData<T>(command, postData); 
} 

public T PostData<T>(string command, Dictionary<string, object> postData) 
{ 
    postData.Add("command", command); 
    postData.Add("nonce", Helper.GetCurrentHttpPostNonce()); 

    var jsonString = PostString(Helper.ApiUrlHttpsRelativeTrading, postData.ToHttpPostString()); 
    var output = JsonSerializer.DeserializeObject<T>(jsonString); 

    return output; 
} 

누군가가 해결할 수 있기를 바랍니다. 나는 하루 종일 노력 해왔다. 미리 감사드립니다.

+0

코드를 더 표시하십시오. 당신은 어떻게 deserialization을합니까? – dotctor

+0

@dotctor – Nickmccomb

+0

'JsonSerializer.DeserializeObject'를'JsonConvert.DeserializeObject'로 변경합니다. – dotctor

답변

1

Json의 숫자는 아포스트로피로 둘러 쌉니다. 즉, 숫자가 아닌 문자열입니다. Json 문자열의 숫자에서 아포스트로피를 제거하거나 필드 유형 양식 double을 개체의 문자열로 변경할 수 있습니다.

MarginBalance에서 사용 된 JsonProperty 속성은 Newtonsoft.Json.Serialization 네임 스페이스에 속합니다. 객체를 deserialize하려면 사용해야합니다. JsonConvert.DeserializeObject<MarginBalance>(jsonString)

+0

나는 이것을 시도하고 수신했다. Newtonsoft.Json.dll에서 'Newtonsoft.Json.JsonSerializationException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았다. 추가 정보 : 값을 변환하는 중 오류가 발생했습니다. "0.00091977"을 입력하여 'Jojatekok.PoloniexAPI.WalletTools.MarginBalance'를 입력하십시오. Path 'totalValue', line 1, position 26. – Nickmccomb

+1

@Nickmccomb 당신은 여전히 ​​IDictionary 로 deserialising하고 있습니다. 단 하나의 "여백 발란스"만 있다면 그렇게하지 마십시오. – hvd

+0

Json의 숫자는 아포스트로피로 둘러 쌉니다. 즉, 숫자가 아닌 문자열입니다. Json 문자열의 숫자에서 아포스트로피를 제거하거나 필드 유형 양식 double을 개체의 문자열로 변경할 수 있습니다. –