1

I've found similar problems but none of the solutions seem to help in my case.

My json looks like this:

{"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"}
]}

The relevant parts of my code for processing looks like this:

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);
    }
  }
}

and

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; }
  }
}

In memory, my json string looks as expected in json but obj is null.

I'm using .NET core 1.1 and Newtonsoft Json.net 10.0.3.

1
  • In your JSON the root object's only property is {"array": [ ... ]} but your MarketOrderList only has a property named MarketOrder -- not array. Commented Jul 30, 2017 at 2:49

1 Answer 1

1

Your are getting an empty object because you did not add a [JsonProperty] attribute to the MarketOrder property in your MarketOrderList class, and the property name does not match what is in the JSON. It should be like this:

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

You have another issue however. The order_id values in your JSON are too large to fit in an int. You will need to change the declaration of the order_id property in your MarketOrder class to be a long to get it to work.

public class MarketOrder
{
    ...

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

    ...
}

With those two changes, it will deserialize just fine.

Fiddle: https://dotnetfiddle.net/TcAfAq

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! That fixed it. Thanks for your in-depth answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.