Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem in deserializing a JSON object in C#

Problem in deserializing a JSON object in C#

Scheduled Pinned Locked Moved C#
csharphelpjsonlinqcom
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    Hi, When I try the following code, I receive an error which says:

    Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' 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 'data', line 2, position 8.'

    On this line:

    List items = JsonConvert.DeserializeObject>(json);

    Here is my Code:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Web.Script.Serialization;

    namespace Test
    {
    public class Program
    {
    public class News
    {

            public IList data { get; set; }
        }
    
        public class NewsItem
        {
    
            public IList title { get; set; }
            public IList body { get; set; }
        }
    
        public static void Main(string\[\] args)
        {
            string json = "";
            using (StreamReader r = new StreamReader("data3.json"))
            {
                json = r.ReadToEnd();
                List items = JsonConvert.DeserializeObject\>(json);
            }
    
            List  allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json);
    
            foreach (var item in allNews)
            {
    
                foreach (var record in item.data)
                {
                    Console.WriteLine(record.title);
                    Console.WriteLine(record.body);
                }
                Console.ReadLine();
            }
        }
    }
    

    }

    And here is my JSON sample object:

    {
    "data": [{
    "body": ["

    part 1-1

    ", "

    part 1-2

    "],
    "link": "http://www.test.com",
    "title": ["sample title 1"]
    },
    {
    "body": ["

    part 2-1

    ", "

    part 2-2

    "],
    "link": "http://www.test.com",
    "title": ["sample title 2"]
    }
    ]
    }

    Thank you in advance for your time and considera

    D L Richard DeemingR 3 Replies Last reply
    0
    • F Farhad Eft

      Hi, When I try the following code, I receive an error which says:

      Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' 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 'data', line 2, position 8.'

      On this line:

      List items = JsonConvert.DeserializeObject>(json);

      Here is my Code:

      using Newtonsoft.Json;
      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      using System.Text.RegularExpressions;
      using System.Web.Script.Serialization;

      namespace Test
      {
      public class Program
      {
      public class News
      {

              public IList data { get; set; }
          }
      
          public class NewsItem
          {
      
              public IList title { get; set; }
              public IList body { get; set; }
          }
      
          public static void Main(string\[\] args)
          {
              string json = "";
              using (StreamReader r = new StreamReader("data3.json"))
              {
                  json = r.ReadToEnd();
                  List items = JsonConvert.DeserializeObject\>(json);
              }
      
              List  allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json);
      
              foreach (var item in allNews)
              {
      
                  foreach (var record in item.data)
                  {
                      Console.WriteLine(record.title);
                      Console.WriteLine(record.body);
                  }
                  Console.ReadLine();
              }
          }
      }
      

      }

      And here is my JSON sample object:

      {
      "data": [{
      "body": ["

      part 1-1

      ", "

      part 1-2

      "],
      "link": "http://www.test.com",
      "title": ["sample title 1"]
      },
      {
      "body": ["

      part 2-1

      ", "

      part 2-2

      "],
      "link": "http://www.test.com",
      "title": ["sample title 2"]
      }
      ]
      }

      Thank you in advance for your time and considera

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Your data tag in the JSON is not an array yet your C# code it treating it as one.

      System.ItDidntWorkException: Something didn't work as expected. A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      1 Reply Last reply
      0
      • F Farhad Eft

        Hi, When I try the following code, I receive an error which says:

        Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' 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 'data', line 2, position 8.'

        On this line:

        List items = JsonConvert.DeserializeObject>(json);

        Here is my Code:

        using Newtonsoft.Json;
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Text.RegularExpressions;
        using System.Web.Script.Serialization;

        namespace Test
        {
        public class Program
        {
        public class News
        {

                public IList data { get; set; }
            }
        
            public class NewsItem
            {
        
                public IList title { get; set; }
                public IList body { get; set; }
            }
        
            public static void Main(string\[\] args)
            {
                string json = "";
                using (StreamReader r = new StreamReader("data3.json"))
                {
                    json = r.ReadToEnd();
                    List items = JsonConvert.DeserializeObject\>(json);
                }
        
                List  allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json);
        
                foreach (var item in allNews)
                {
        
                    foreach (var record in item.data)
                    {
                        Console.WriteLine(record.title);
                        Console.WriteLine(record.body);
                    }
                    Console.ReadLine();
                }
            }
        }
        

        }

        And here is my JSON sample object:

        {
        "data": [{
        "body": ["

        part 1-1

        ", "

        part 1-2

        "],
        "link": "http://www.test.com",
        "title": ["sample title 1"]
        },
        {
        "body": ["

        part 2-1

        ", "

        part 2-2

        "],
        "link": "http://www.test.com",
        "title": ["sample title 2"]
        }
        ]
        }

        Thank you in advance for your time and considera

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your NewsItem class is missing a definition of the link JSON entry.

        Richard DeemingR 1 Reply Last reply
        0
        • L Lost User

          Your NewsItem class is missing a definition of the link JSON entry.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          JSON.NET, and most other JSON libraries, will simply ignore any missing properties.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          L 1 Reply Last reply
          0
          • F Farhad Eft

            Hi, When I try the following code, I receive an error which says:

            Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' 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 'data', line 2, position 8.'

            On this line:

            List items = JsonConvert.DeserializeObject>(json);

            Here is my Code:

            using Newtonsoft.Json;
            using System;
            using System.Collections.Generic;
            using System.IO;
            using System.Linq;
            using System.Text.RegularExpressions;
            using System.Web.Script.Serialization;

            namespace Test
            {
            public class Program
            {
            public class News
            {

                    public IList data { get; set; }
                }
            
                public class NewsItem
                {
            
                    public IList title { get; set; }
                    public IList body { get; set; }
                }
            
                public static void Main(string\[\] args)
                {
                    string json = "";
                    using (StreamReader r = new StreamReader("data3.json"))
                    {
                        json = r.ReadToEnd();
                        List items = JsonConvert.DeserializeObject\>(json);
                    }
            
                    List  allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json);
            
                    foreach (var item in allNews)
                    {
            
                        foreach (var record in item.data)
                        {
                            Console.WriteLine(record.title);
                            Console.WriteLine(record.body);
                        }
                        Console.ReadLine();
                    }
                }
            }
            

            }

            And here is my JSON sample object:

            {
            "data": [{
            "body": ["

            part 1-1

            ", "

            part 1-2

            "],
            "link": "http://www.test.com",
            "title": ["sample title 1"]
            },
            {
            "body": ["

            part 2-1

            ", "

            part 2-2

            "],
            "link": "http://www.test.com",
            "title": ["sample title 2"]
            }
            ]
            }

            Thank you in advance for your time and considera

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Farhad Eft wrote:

            JsonConvert.DeserializeObject<List<NewsItem>>(json);

            Farhad Eft wrote:

            new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<News>>(json);

            The JSON you've posted is neither a List<NewsItem> nor a List<News>; it is simply a single News object.

            string json = File.ReadAllText("data3.json");
            News allNews = JsonConvert.DeserializeObject<News>(json);
            foreach (NewsItem item in allNews.data)
            {
            foreach (string title in item.title)
            {
            Console.WriteLine(title);
            }

            foreach (string body in item.body)
            {
                Console.WriteLine(body);
            }
            
            Console.WriteLine();
            

            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            F 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              JSON.NET, and most other JSON libraries, will simply ignore any missing properties.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Thanks, something I need to test. However, looking at the question again, shouldn't it be trying to deserialise int a News class, rather than NewsItem?

              Richard DeemingR 1 Reply Last reply
              0
              • L Lost User

                Thanks, something I need to test. However, looking at the question again, shouldn't it be trying to deserialise int a News class, rather than NewsItem?

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Richard MacCutchan wrote:

                shouldn't it be trying to deserialise int a News class, rather than NewsItem?

                Indeed. As I posted below. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Farhad Eft wrote:

                  JsonConvert.DeserializeObject<List<NewsItem>>(json);

                  Farhad Eft wrote:

                  new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<News>>(json);

                  The JSON you've posted is neither a List<NewsItem> nor a List<News>; it is simply a single News object.

                  string json = File.ReadAllText("data3.json");
                  News allNews = JsonConvert.DeserializeObject<News>(json);
                  foreach (NewsItem item in allNews.data)
                  {
                  foreach (string title in item.title)
                  {
                  Console.WriteLine(title);
                  }

                  foreach (string body in item.body)
                  {
                      Console.WriteLine(body);
                  }
                  
                  Console.WriteLine();
                  

                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  F Offline
                  F Offline
                  Farhad Eft
                  wrote on last edited by
                  #8

                  You guys are simply amazing!! Thank you! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups