JSON.NET의 JsonConvert 클래스를 사용하여 일부 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; }
}
나에게 문제를 일으키는 것은 데이터가 '반환'키 아래에 있다는 사실입니다. 리턴 키를 제거하면 완벽하게 작동합니다. 내 시장 개체가 다음과 같이 작동하도록하려면 어떻게해야합니까?
foreach(SellOrder sellorder in desObj.SellOrders)
{
Console.WriteLine(sellorder.total.ToString());
}
return 속성을 동적 목록으로 만든 다음 그런 식으로 판매 / 구매 주문을 검색하는 실험을 시도했지만 아무것도 작동하지 않는 것 같습니다. 어떤 아이디어?
그렇게 할 수 없나요?
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());
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다