2013-08-14 2 views
0

JSON.NET의 JsonConvert 클래스를 사용하여 개체로 일부 JSON을 deserialize하려고합니다.JSON.NET으로 이상한 구조의 JSON을 역 직렬화

코드 나는 JSON 구조 샘플을 사용하고 있습니다 :

var desObj = JsonConvert.DeserializeObject<Market>("{\"success\":\"1\",\"return\": 
{\"sellorders\": 
[{\"sellprice\":\"0.00001099\",\"quantity\":\"60.00000000\",\"total\":\"0.00065940\"}, 
{\"sellprice\":\"0.00001100\",\"quantity\":\"1000.00000000\",\"total\":\"0.01100000\"}, 
{\"sellprice\":\"0.00001105\",\"quantity\":\"60.00000000\",\"total\":\"0.01200\"}]}}"); 

그리고 내 시장 클래스 :

class Market 
    { 
     [JsonProperty("success")] 
     public int Success { get; set; } 

     [JsonProperty("sellorders")] 
     public List<SellOrder> SellOrders {get; set;} 

     [JsonProperty("buyorders")] 
     public List<BuyOrder> BuyOrders {get; set;} 
    } 

    public class SellOrder 
    { 
     [JsonProperty("sellprice")] 
     public decimal SellPrice { get; set; } 

     [JsonProperty("quantity")] 
     public decimal Quantity { get; set; } 

     [JsonProperty("total")] 
     public decimal Total { get; set; } 
    } 

    public class BuyOrder 
    { 
     [JsonProperty("buyprice")] 
     public decimal BuyPrice { get; set; } 

     [JsonProperty("quantity")] 
     public decimal Quantity { get; set; } 

     [JsonProperty("total")] 
     public decimal Total { get; set; } 
    } 

나에게 문제를 일으키는 것은 데이터가 아래에 있다는 사실이다 ' return '키를 누르십시오. 리턴 키를 제거하면 완벽하게 작동합니다. 그런 식으로 매각/매입 주문을 검색 한 후, 반환 동적 속성 목록 만들기 실험

foreach(SellOrder sellorder in desObj.SellOrders) 
{ 
    Console.WriteLine(sellorder.total.ToString()); 
} 

나는 시도했다, 그러나 아무것도에 보인다 : 어떻게 이런 식으로 행동하는 내 시장 객체를 받고 가겠어요 작업. 어떤 아이디어?

+0

나의 현재 주위에 작업은 다음과 같이 원래 JSON을 수정하는 것입니다 : 결과 = result.Replace ("반환 \"을 {\ "" , ""); 결과 = result.Substring (0, result.Length - 1); ... 작동하지만, 확실히 해킹입니다. 하나 있다면 청소기 솔루션을 선호 할 것입니다! – Anonymous

답변

1

그런 일을 할 수 없습니까?

class Market 
    { 
     [JsonProperty("success")] 
     public int Success { get; set; } 
     [JsonProperty("return")] 
     public Container Container { get; set; } 

    } 
    class Container 
    { 
     [JsonProperty("sellorders")] 
     public List<SellOrder> SellOrders { get; set; } 

     [JsonProperty("buyorders")] 
     public List<BuyOrder> BuyOrders { get; set; } 
    } 

    public class SellOrder 
    { 
     [JsonProperty("sellprice")] 
     public decimal SellPrice { get; set; } 

     [JsonProperty("quantity")] 
     public decimal Quantity { get; set; } 

     [JsonProperty("total")] 
     public decimal Total { get; set; } 
    } 

    public class BuyOrder 
    { 
     [JsonProperty("buyprice")] 
     public decimal BuyPrice { get; set; } 

     [JsonProperty("quantity")] 
     public decimal Quantity { get; set; } 

     [JsonProperty("total")] 
     public decimal Total { get; set; } 
} 

하고 그런 당신에게 데이터에 액세스 :

foreach(SellOrder sellorder in desObj.Container.SellOrders) 
{ 
    Console.WriteLine(sellorder.total.ToString()); 
} 
+0

이것은 효과가 있었다 - 고마워. JsonProperty Annotation을 사용하는 영리한 솔루션이 있다면 좋을 것입니다. 제가하는 모든 API가 모든 요청에서이 작업을 수행하기 때문입니다. 다시 한번 감사드립니다. – Anonymous

+1

속성을 사용하여 Json의 반환 속성을 건너 뛸 수있는 방법이 없다고 생각합니다 .-( – Swift