Riccardo Pezzolati
Riccardo Pezzolati

Reputation: 359

Deserialising json with JSON.NET - Cannot deserialise because the type requires a JSON array c#

This is my json

{
   "odata.metadata" : "",
   "value" : [
      {
         "ItemCode" : "NUOVO_ELEMENT1406",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT14506",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT1455106",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT1455a106",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT145574a106",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT16",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT1d6",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT433",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT1d464645454546",
         "ItemName" : "UPDATE",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT433787079",
         "ItemName" : "Prova3",
         "QuantityOnStock" : 0.0
      },
      {
         "ItemCode" : "NUOVO_ELEMENT43389898989787079",
         "ItemName" : "Prova3121",
         "QuantityOnStock" : 0.0
      }
   ]
}

this is Entity which has the task of mapping the json

public class Item
    {
        [JsonProperty(PropertyName ="ItemCode")]
        public string ItemCode { get; set; }
        [JsonProperty(PropertyName ="ItemName")]
        public string ItemName { get; set; }
        [JsonProperty(PropertyName ="QuantityOnStock")]
        public decimal QuantityOnStock { get; set; }
    }

With this class, I thought of the json

internal class JsonParser
    {

        internal void Deserialize(string v)
        {

            List<Item> it = JsonConvert.DeserializeObject<List<Item>>(v);
            foreach (Item item in it)
            {
                Console.WriteLine("{0},{1}", item.ItemName, item.ItemCode);
            }

        }
    }

I tried to remove List and use only Item, if my json contains only one element of course no problem, but in cases where I have more  elements and I try to use the List I receive the error described in the title.

Upvotes: 3

Views: 343

Answers (4)

user7159857
user7159857

Reputation:

If you don't want to setup a class to map to, you can use dynamic objects.

dynamic it = JsonConvert.DeserializeObject(v);

Then, to access a value, use

string itemCode1 = it.value[0].ItemCode.Value;

Upvotes: 0

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

As @Marc-Gravell answered, your json is different from the Item class. To parse your json, you should create another class that has a value property of a List of Item class type:

public class SomeClass
{
     public List<Item> value { get; set; }
}

EDIT: And about the "odata.metadata", maybe you can add to "SomeClass" a property like this:

[JsonProperty("odata.metadata")]
public string metadata { get; set; }

Upvotes: 0

FaizanHussainRabbani
FaizanHussainRabbani

Reputation: 3439

It is because the object you are trying to deserialize is not correct, you should try following class:

public class Rootobject
{
    [JsonProperty("odata.metadata")]
    public string odatametadata { get; set; }
    public Value[] value { get; set; }
}

public class Value
{
    public string ItemCode { get; set; }
    public string ItemName { get; set; }
    public float QuantityOnStock { get; set; }
}

var items = JsonConvert.DeserializeObject<Rootobject>(json);

Output:

enter image description here

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062945

A List<Item> would look like:

[
  {
     "ItemCode" : "NUOVO_ELEMENT14506",
     "ItemName" : "Prova3",
     "QuantityOnStock" : 0.0
  },
  {
     "ItemCode" : "NUOVO_ELEMENT1455106",
     "ItemName" : "Prova3",
     "QuantityOnStock" : 0.0
  }
]

You don't have that - the root object is a {...} object, not a {...} array, so you will need something like:

class SomeWrapper
{
    public List<Item> value { get; set; }
}

and use DeserializeObject<SomeWrapper>, then access the .value.

Upvotes: 1

Related Questions