2017-07-30 160 views
1

我找到了similar problems,但沒有一個解決方案似乎對我有幫助。C#使用newtonsoft將json數組轉換爲對象列表json.net

我的JSON是這樣的:

{"array": [ 
    {"order_id": 4923887462, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 130000000.0, "is_buy_order": false, "duration": 90, "issued": "2017-07-25T16:40:18Z", "range": "region"}, 
    {"order_id": 4926414947, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 92000000.0, "is_buy_order": false, "duration": 90, "issued": "2017-07-29T06:47:29Z", "range": "region"}, 
    {"order_id": 4927013184, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 91999989.82, "is_buy_order": false, "duration": 90, "issued": "2017-07-29T22:26:05Z", "range": "region"}, 
    {"order_id": 4927082974, "type_id": 19297, "location_id": 60008494, "volume_total": 2, "volume_remain": 2, "min_volume": 1, "price": 91999989.81, "is_buy_order": false, "duration": 90, "issued": "2017-07-30T00:22:36Z", "range": "region"} 
]} 

的我的處理代碼的相關部分看起來是這樣的:

using System; 
using System.Threading.Tasks; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
using System.Collections.Generic; 

namespace sqlite_test 
{ 
    public class Program 
    { 
    public static void Main() 
    { 
     int item = 19297; 

     string json = ESIHelper.MarketFetch.getMarketData((int)ESIHelper.Regions.Domain,item,"sell",1).Result; 
     ESIHelper.Models.MarketOrderList obj = JsonConvert.DeserializeObject<ESIHelper.Models.MarketOrderList>(json); 
    } 
    } 
} 

using System.Collections.Generic; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

namespace ESIHelper.Models 
{ 
    public class MarketOrder 
    { 
    [Newtonsoft.Json.JsonProperty(PropertyName = "duration")] 
    public int duration { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "is_buy_order")] 
    public bool is_buy_order { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "issued")] 
    public string issued { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "location_id")] 
    public int location_id { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "min_volume")] 
    public int min_volume { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "order_id")] 
    public int order_id { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "price")] 
    public double price { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "range")] 
    public string range { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "type_id")] 
    public int type_id { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "volume_remain")] 
    public int volume_remain { get; set; } 
    [Newtonsoft.Json.JsonProperty(PropertyName = "volume_total")] 
    public int volume_total { get; set; } 
    } 

    public class MarketOrderList 
    { 
    public List<MarketOrder> MarketOrder { get; set; } 
    } 
} 

在內存中,我的JSON字符串看起來如預期的json,但obj爲空。

我使用.NET核心1.1和Newtonsoft Json.net 10.0.3。

+0

在你的JSON根對象的唯一屬性是'{ 「數組」:[...]}'但是你'MarketOrderList'只有一個名爲'MarketOrder'財產 - 不'陣列'。 – dbc

回答

1

你得到的是否是空的對象,因爲你沒有在你的MarketOrderList[JsonProperty]屬性添加到MarketOrder屬性和屬性名稱不匹配什麼是在JSON。它應該是這樣的:

public class MarketOrderList 
{ 
    [JsonProperty(PropertyName = "array")] 
    public List<MarketOrder> MarketOrder { get; set; } 
} 

然而,你有另一個問題。您的JSON中的order_id值太大,無法放入int。您需要將MarketOrder類中的order_id屬性的聲明更改爲long以使其正常工作。

public class MarketOrder 
{ 
    ... 

    [JsonProperty(PropertyName = "order_id")] 
    public long order_id { get; set; } 

    ... 
} 

有了這兩個改變,它將反序列化就好了。

小提琴:https://dotnetfiddle.net/TcAfAq

+0

太棒了!這解決了它。感謝您的深入解答。 – eiton

+0

很高興我能幫到你。 –