C# JSON deserialize into complex class












0















I have a JSON like this



    {
"Application": [
{
"Office": "London",
"LogPath": [
"\\server1filepath\"
]
},
{
"Office": "Paris",
"LogPath": [
"\\server2\logpath1\",
"\\server2\logpath2\"
]
}
],
"MailSettings": {
"MailTo": "mymail@mydomain.it",
"MailSubject" : "Log Checker"
}
}


and i have create a custom class to read the json content :



public class RootObject
{
public Application Application { get; set; }
public MailSettings MailSettings { get; set; }
}

public class Application
{
public List<Offices> Offices { get; set; }
}

public class Offices
{
public string Office{ get; set; }
public IList<string> LogPath { get; set; }
}

public class MailSettings
{
public string MailTo { get; set; }
public string MailSubject { get; set; }
}


But when i try to deserialize the json with



RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);


I return the error :
the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.



Which is wrong with my custom class ?










share|improve this question























  • Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

    – jAC
    Nov 27 '18 at 11:15


















0















I have a JSON like this



    {
"Application": [
{
"Office": "London",
"LogPath": [
"\\server1filepath\"
]
},
{
"Office": "Paris",
"LogPath": [
"\\server2\logpath1\",
"\\server2\logpath2\"
]
}
],
"MailSettings": {
"MailTo": "mymail@mydomain.it",
"MailSubject" : "Log Checker"
}
}


and i have create a custom class to read the json content :



public class RootObject
{
public Application Application { get; set; }
public MailSettings MailSettings { get; set; }
}

public class Application
{
public List<Offices> Offices { get; set; }
}

public class Offices
{
public string Office{ get; set; }
public IList<string> LogPath { get; set; }
}

public class MailSettings
{
public string MailTo { get; set; }
public string MailSubject { get; set; }
}


But when i try to deserialize the json with



RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);


I return the error :
the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.



Which is wrong with my custom class ?










share|improve this question























  • Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

    – jAC
    Nov 27 '18 at 11:15
















0












0








0








I have a JSON like this



    {
"Application": [
{
"Office": "London",
"LogPath": [
"\\server1filepath\"
]
},
{
"Office": "Paris",
"LogPath": [
"\\server2\logpath1\",
"\\server2\logpath2\"
]
}
],
"MailSettings": {
"MailTo": "mymail@mydomain.it",
"MailSubject" : "Log Checker"
}
}


and i have create a custom class to read the json content :



public class RootObject
{
public Application Application { get; set; }
public MailSettings MailSettings { get; set; }
}

public class Application
{
public List<Offices> Offices { get; set; }
}

public class Offices
{
public string Office{ get; set; }
public IList<string> LogPath { get; set; }
}

public class MailSettings
{
public string MailTo { get; set; }
public string MailSubject { get; set; }
}


But when i try to deserialize the json with



RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);


I return the error :
the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.



Which is wrong with my custom class ?










share|improve this question














I have a JSON like this



    {
"Application": [
{
"Office": "London",
"LogPath": [
"\\server1filepath\"
]
},
{
"Office": "Paris",
"LogPath": [
"\\server2\logpath1\",
"\\server2\logpath2\"
]
}
],
"MailSettings": {
"MailTo": "mymail@mydomain.it",
"MailSubject" : "Log Checker"
}
}


and i have create a custom class to read the json content :



public class RootObject
{
public Application Application { get; set; }
public MailSettings MailSettings { get; set; }
}

public class Application
{
public List<Offices> Offices { get; set; }
}

public class Offices
{
public string Office{ get; set; }
public IList<string> LogPath { get; set; }
}

public class MailSettings
{
public string MailTo { get; set; }
public string MailSubject { get; set; }
}


But when i try to deserialize the json with



RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);


I return the error :
the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.



Which is wrong with my custom class ?







javascript c# json serialization json.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 11:09









lpernicelpernice

4018




4018













  • Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

    – jAC
    Nov 27 '18 at 11:15





















  • Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

    – jAC
    Nov 27 '18 at 11:15



















Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

– jAC
Nov 27 '18 at 11:15







Your JSON does not match your class structure. For example, your JSON has an array of Application properties while your class structure has one Application which has a List<String> offices.

– jAC
Nov 27 '18 at 11:15














4 Answers
4






active

oldest

votes


















0














Your custom classes should be like it.



 public class Application
{
public string Office { get; set; }
public List<string> LogPath { get; set; }
}

public class MailSettings
{
public string MailTo { get; set; }
public string MailSubject { get; set; }
}

public class RootObject
{
public List<Application> Application { get; set; }
public MailSettings MailSettings { get; set; }
}





share|improve this answer































    1














    You can generate types using json2csharp - to save hand-crafting a representation of the JSON object.



    public class RootObject
    {
    public List<Application> Application { get; set; }
    public MailSettings MailSettings { get; set; }
    }

    public class Application
    {
    public string Office { get; set; }
    public List<string> LogPath { get; set; }
    }

    public class MailSettings
    {
    public string MailTo { get; set; }
    public string MailSubject { get; set; }
    }


    Using Visual Studio "Paste JSON as Classes" gives you the equally functional, but slightly different:



        public class Rootobject
    {
    public Application Application { get; set; }
    public Mailsettings MailSettings { get; set; }
    }

    public class Mailsettings
    {
    public string MailTo { get; set; }
    public string MailSubject { get; set; }
    }

    public class Application
    {
    public string Office { get; set; }
    public string LogPath { get; set; }
    }


    Visual Studio Code users can get the same feature using the JSON as Code extension, powered by QuickType.






    share|improve this answer

































      0














      Copy your json string and use visual studio inbuild feature to create custom class for respective json.



      - Goto Edit -> Special Paste -> Paste JSON as Class



      This will create custom class for you.
      Now using Newtonsoft.Json library, you can deserialize your json.






      share|improve this answer































        0














        I tested your code.your problem is in your RootObject model.
        In your RootObject you have a collection of application not single application.
        i changed it and it wrok correctly.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498345%2fc-sharp-json-deserialize-into-complex-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your custom classes should be like it.



           public class Application
          {
          public string Office { get; set; }
          public List<string> LogPath { get; set; }
          }

          public class MailSettings
          {
          public string MailTo { get; set; }
          public string MailSubject { get; set; }
          }

          public class RootObject
          {
          public List<Application> Application { get; set; }
          public MailSettings MailSettings { get; set; }
          }





          share|improve this answer




























            0














            Your custom classes should be like it.



             public class Application
            {
            public string Office { get; set; }
            public List<string> LogPath { get; set; }
            }

            public class MailSettings
            {
            public string MailTo { get; set; }
            public string MailSubject { get; set; }
            }

            public class RootObject
            {
            public List<Application> Application { get; set; }
            public MailSettings MailSettings { get; set; }
            }





            share|improve this answer


























              0












              0








              0







              Your custom classes should be like it.



               public class Application
              {
              public string Office { get; set; }
              public List<string> LogPath { get; set; }
              }

              public class MailSettings
              {
              public string MailTo { get; set; }
              public string MailSubject { get; set; }
              }

              public class RootObject
              {
              public List<Application> Application { get; set; }
              public MailSettings MailSettings { get; set; }
              }





              share|improve this answer













              Your custom classes should be like it.



               public class Application
              {
              public string Office { get; set; }
              public List<string> LogPath { get; set; }
              }

              public class MailSettings
              {
              public string MailTo { get; set; }
              public string MailSubject { get; set; }
              }

              public class RootObject
              {
              public List<Application> Application { get; set; }
              public MailSettings MailSettings { get; set; }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 27 '18 at 11:17









              OfficalMesutOfficalMesut

              327413




              327413

























                  1














                  You can generate types using json2csharp - to save hand-crafting a representation of the JSON object.



                  public class RootObject
                  {
                  public List<Application> Application { get; set; }
                  public MailSettings MailSettings { get; set; }
                  }

                  public class Application
                  {
                  public string Office { get; set; }
                  public List<string> LogPath { get; set; }
                  }

                  public class MailSettings
                  {
                  public string MailTo { get; set; }
                  public string MailSubject { get; set; }
                  }


                  Using Visual Studio "Paste JSON as Classes" gives you the equally functional, but slightly different:



                      public class Rootobject
                  {
                  public Application Application { get; set; }
                  public Mailsettings MailSettings { get; set; }
                  }

                  public class Mailsettings
                  {
                  public string MailTo { get; set; }
                  public string MailSubject { get; set; }
                  }

                  public class Application
                  {
                  public string Office { get; set; }
                  public string LogPath { get; set; }
                  }


                  Visual Studio Code users can get the same feature using the JSON as Code extension, powered by QuickType.






                  share|improve this answer






























                    1














                    You can generate types using json2csharp - to save hand-crafting a representation of the JSON object.



                    public class RootObject
                    {
                    public List<Application> Application { get; set; }
                    public MailSettings MailSettings { get; set; }
                    }

                    public class Application
                    {
                    public string Office { get; set; }
                    public List<string> LogPath { get; set; }
                    }

                    public class MailSettings
                    {
                    public string MailTo { get; set; }
                    public string MailSubject { get; set; }
                    }


                    Using Visual Studio "Paste JSON as Classes" gives you the equally functional, but slightly different:



                        public class Rootobject
                    {
                    public Application Application { get; set; }
                    public Mailsettings MailSettings { get; set; }
                    }

                    public class Mailsettings
                    {
                    public string MailTo { get; set; }
                    public string MailSubject { get; set; }
                    }

                    public class Application
                    {
                    public string Office { get; set; }
                    public string LogPath { get; set; }
                    }


                    Visual Studio Code users can get the same feature using the JSON as Code extension, powered by QuickType.






                    share|improve this answer




























                      1












                      1








                      1







                      You can generate types using json2csharp - to save hand-crafting a representation of the JSON object.



                      public class RootObject
                      {
                      public List<Application> Application { get; set; }
                      public MailSettings MailSettings { get; set; }
                      }

                      public class Application
                      {
                      public string Office { get; set; }
                      public List<string> LogPath { get; set; }
                      }

                      public class MailSettings
                      {
                      public string MailTo { get; set; }
                      public string MailSubject { get; set; }
                      }


                      Using Visual Studio "Paste JSON as Classes" gives you the equally functional, but slightly different:



                          public class Rootobject
                      {
                      public Application Application { get; set; }
                      public Mailsettings MailSettings { get; set; }
                      }

                      public class Mailsettings
                      {
                      public string MailTo { get; set; }
                      public string MailSubject { get; set; }
                      }

                      public class Application
                      {
                      public string Office { get; set; }
                      public string LogPath { get; set; }
                      }


                      Visual Studio Code users can get the same feature using the JSON as Code extension, powered by QuickType.






                      share|improve this answer















                      You can generate types using json2csharp - to save hand-crafting a representation of the JSON object.



                      public class RootObject
                      {
                      public List<Application> Application { get; set; }
                      public MailSettings MailSettings { get; set; }
                      }

                      public class Application
                      {
                      public string Office { get; set; }
                      public List<string> LogPath { get; set; }
                      }

                      public class MailSettings
                      {
                      public string MailTo { get; set; }
                      public string MailSubject { get; set; }
                      }


                      Using Visual Studio "Paste JSON as Classes" gives you the equally functional, but slightly different:



                          public class Rootobject
                      {
                      public Application Application { get; set; }
                      public Mailsettings MailSettings { get; set; }
                      }

                      public class Mailsettings
                      {
                      public string MailTo { get; set; }
                      public string MailSubject { get; set; }
                      }

                      public class Application
                      {
                      public string Office { get; set; }
                      public string LogPath { get; set; }
                      }


                      Visual Studio Code users can get the same feature using the JSON as Code extension, powered by QuickType.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 27 '18 at 11:28

























                      answered Nov 27 '18 at 11:20









                      FentonFenton

                      155k44291315




                      155k44291315























                          0














                          Copy your json string and use visual studio inbuild feature to create custom class for respective json.



                          - Goto Edit -> Special Paste -> Paste JSON as Class



                          This will create custom class for you.
                          Now using Newtonsoft.Json library, you can deserialize your json.






                          share|improve this answer




























                            0














                            Copy your json string and use visual studio inbuild feature to create custom class for respective json.



                            - Goto Edit -> Special Paste -> Paste JSON as Class



                            This will create custom class for you.
                            Now using Newtonsoft.Json library, you can deserialize your json.






                            share|improve this answer


























                              0












                              0








                              0







                              Copy your json string and use visual studio inbuild feature to create custom class for respective json.



                              - Goto Edit -> Special Paste -> Paste JSON as Class



                              This will create custom class for you.
                              Now using Newtonsoft.Json library, you can deserialize your json.






                              share|improve this answer













                              Copy your json string and use visual studio inbuild feature to create custom class for respective json.



                              - Goto Edit -> Special Paste -> Paste JSON as Class



                              This will create custom class for you.
                              Now using Newtonsoft.Json library, you can deserialize your json.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 27 '18 at 11:22









                              Prasad TelkikarPrasad Telkikar

                              2,030419




                              2,030419























                                  0














                                  I tested your code.your problem is in your RootObject model.
                                  In your RootObject you have a collection of application not single application.
                                  i changed it and it wrok correctly.






                                  share|improve this answer




























                                    0














                                    I tested your code.your problem is in your RootObject model.
                                    In your RootObject you have a collection of application not single application.
                                    i changed it and it wrok correctly.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      I tested your code.your problem is in your RootObject model.
                                      In your RootObject you have a collection of application not single application.
                                      i changed it and it wrok correctly.






                                      share|improve this answer













                                      I tested your code.your problem is in your RootObject model.
                                      In your RootObject you have a collection of application not single application.
                                      i changed it and it wrok correctly.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 27 '18 at 11:36









                                      MRMFMRMF

                                      357




                                      357






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498345%2fc-sharp-json-deserialize-into-complex-class%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                          Calculate evaluation metrics using cross_val_predict sklearn

                                          Insert data from modal to MySQL (multiple modal on website)