3

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.

3
  • Could you add your JSON to the question, not everyone will be able to access pastebin. Also pastebin could go away rendering the question incomplete. Commented Feb 26, 2018 at 11:38
  • the part which is the array of Item objects is within the value property in the JSON. So you either need to target that specifically for your deserialisation, or use a wrapper class to represent the object which encloses the array and deserialise to that. e.g. public class SomeClass { public List<Item> value { get; set; } etc Commented Feb 26, 2018 at 11:38
  • Your are passing Object here instead of JSON array to Deserialize() method. It will work fine with this input [{ItemCode:'ItemCode1',ItemName:'ItemName1',QuantityOnStock:'10.25'},{ItemCode:'ItemCode2',ItemName:'ItemName2',QuantityOnStock:'10.25'}] Commented Feb 26, 2018 at 11:51

4 Answers 4

1

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

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

4 Comments

that "odatametadata" will need a [JsonProperty("odata.metadata")] or similar to work, I suspect
var items = JsonConvert.DeserializeObject<Rootobject>(json); return NullReferenceException
@RiccardoPezzolati Did you try classes as I mentioned?
@RiccardoPezzolati Glad to help you, please mark the answer as correct because it will be helpful for others looking for an accepted answer.
1

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.

3 Comments

ItemWrapper it = JsonConvert.DeserializeObject<ItemWrapper>(v); it.value = new List<Item>(); foreach (Item item in it.value) { Console.WriteLine("{0},{1}", item.ItemName, item.ItemCode); }
@RiccardoPezzolati your it.value = new List<Item>() has thrown away the data you were interested in... just remove that line
ItemWrapper it = JsonConvert.DeserializeObject<ItemWrapper>(v); foreach (Item item in it.value) { Console.WriteLine("{0},{1}", item.ItemName, item.ItemCode); } add me NullReferenceException, what I wrong?
0

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

Comments

0

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.