1

I've been trying to store a json object with a pretty nested structure that looks like this: (This is dummy data from the api documentation)

{
 "orders": [
{
  "id": 556729,
  "certificate": {
    "common_name": "digicert.com",
    "dns_names": [
      "digicert.com",
      "www.digicert.com"
    ],
    "valid_till": "2017-08-19",
    "signature_hash": "sha256"
  },
  "status": "issued",
  "date_created": "2014-08-19T18:16:07+00:00",
  "organization": {
    "id": 117483,
    "name": "DigiCert, Inc."
  },
  "validity_years": 3,
  "container": {
    "id": 5,
    "name": "College of Science"
  },
  "product": {
    "name_id": "ssl_plus",
    "name": "SSL Plus",
    "type": "ssl_certificate"
  },
  "price": 293
}, ... (lists more dummy orders, but you get the point, valid json structure with nested objects that hold arrays, etc)

I used json2csharp to convert this to the following object:

class Orders
{
    public class Certificate
    {
        public int id { get; set; }
        public string common_name { get; set; }
        public List<string> dns_names { get; set; }
        public string valid_till { get; set; }
        public string signature_hash { get; set; }
    }

    public class Organization
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class Container
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class Product
    {
        public string name_id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
    }

    public class Order
    {
        public int id { get; set; }
        public Certificate certificate { get; set; }
        public string status { get; set; }
        public bool is_renewed { get; set; }
        public string date_created { get; set; }
        public Organization organization { get; set; }
        public int validity_years { get; set; }
        public Container container { get; set; }
        public Product product { get; set; }
        public bool has_duplicates { get; set; }
        public int price { get; set; }
        public string product_name_id { get; set; }
    }

    public class Page
    {
        public int total { get; set; }
        public int limit { get; set; }
        public int offset { get; set; }
    }

    public class RootObject
    {
        public List<Order> orders { get; set; }
        public Page page { get; set; }
    }
}

This was all generated other than the class name Orders which holds all the other objects.

My problem seems to be coming from properly deserializing: I tried the way referenced in the JSON.net documentation, but it didn't include the root object(I assume it's not working without out). I will list a few of the ways I've tried to accomplish this below(The first is directly from the JSON.net documentation): I will Just number my attempts

1. List<Orders> orders = JsonConvert.DeserializeObject<List<Orders>>(jsonString); 

2. List<Orders.RootObject> orders = JsonConvert.DeserializeObject<List<Orders.RootObject>>(this.Json);

3. var root = JsonConvert.DeserializeObject<List<Orders.RootObject>>(this.Json); 

This seemed to be the predominant error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[APIinit.Orders+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'orders', line 1, position 10.

I would like to understand how to properly create this object.

  • 1
    try removing the List<> part.. http://stackoverflow.com/questions/21358493/cannot-deserialize-the-current-json-object-e-g-namevalue-into-type-sy – Adam May 15 '17 at 23:57
  • Deserialize by doing `var root = JsonConvert.DeserializeObject(this.Json);` as shown in the duplicate question. Incidentally, the outer class `Orders` has no properties and could be completely eliminated. – dbc May 16 '17 at 00:03

0 Answers0