serializing nested json c#











up vote
2
down vote

favorite












Big apologies for the long post. I need to create the below json format for a post to rest api in c#. The below call works and I have used successfully in Postman to add it to the target system.



{
"item": {
"attrs": {
"attr": [{
"name": "IP_Category",
"value": "Miscellaneous"
}, {
"name": "Description",
"value": "Picture of Rabbit"
}, {
"name": "Title",
"value": "A Rabbit"
}
]
},
"resrs": {
"res": [{
"filename": "Rabbit.jpg",
"base64": "/9j/4AAQSkZJR"
}
]
},
"acl": {
"name": "Submitter"
},
"entityName": "IP_Document"
}
}


Based on the research I've done I need to copy and "paste special" into a new class file in visual studio so it can create the class objects based on the json format (Pretty cool!). And this is what it creates:



namespace BasicWebApp
{

public class Rootobject
{
public Item item { get; set; }
}

public class Item
{
public Attrs attrs { get; set; }
public Resrs resrs { get; set; }
public Acl acl { get; set; }
public string entityName { get; set; }
}

public class Attrs
{
public Attr attr { get; set; }
}

public class Attr
{
public string name { get; set; }
public string value { get; set; }
}

public class Resrs
{
public Re res { get; set; }
}

public class Re
{
public string filename { get; set; }
public string base64 { get; set; }
}

public class Acl
{
public string name { get; set; }
}
}


Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?



Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?



var model = new Rootobject();
model.item = new Item
{
attrs = new Attrs
{
attr = new List<Attr>
{
**now what??**
}
}
}









share|improve this question


















  • 3




    res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
    – TheGeneral
    Nov 12 at 6:05








  • 1




    For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
    – Diwa
    Nov 12 at 6:06












  • And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
    – Diwa
    Nov 12 at 6:11















up vote
2
down vote

favorite












Big apologies for the long post. I need to create the below json format for a post to rest api in c#. The below call works and I have used successfully in Postman to add it to the target system.



{
"item": {
"attrs": {
"attr": [{
"name": "IP_Category",
"value": "Miscellaneous"
}, {
"name": "Description",
"value": "Picture of Rabbit"
}, {
"name": "Title",
"value": "A Rabbit"
}
]
},
"resrs": {
"res": [{
"filename": "Rabbit.jpg",
"base64": "/9j/4AAQSkZJR"
}
]
},
"acl": {
"name": "Submitter"
},
"entityName": "IP_Document"
}
}


Based on the research I've done I need to copy and "paste special" into a new class file in visual studio so it can create the class objects based on the json format (Pretty cool!). And this is what it creates:



namespace BasicWebApp
{

public class Rootobject
{
public Item item { get; set; }
}

public class Item
{
public Attrs attrs { get; set; }
public Resrs resrs { get; set; }
public Acl acl { get; set; }
public string entityName { get; set; }
}

public class Attrs
{
public Attr attr { get; set; }
}

public class Attr
{
public string name { get; set; }
public string value { get; set; }
}

public class Resrs
{
public Re res { get; set; }
}

public class Re
{
public string filename { get; set; }
public string base64 { get; set; }
}

public class Acl
{
public string name { get; set; }
}
}


Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?



Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?



var model = new Rootobject();
model.item = new Item
{
attrs = new Attrs
{
attr = new List<Attr>
{
**now what??**
}
}
}









share|improve this question


















  • 3




    res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
    – TheGeneral
    Nov 12 at 6:05








  • 1




    For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
    – Diwa
    Nov 12 at 6:06












  • And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
    – Diwa
    Nov 12 at 6:11













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Big apologies for the long post. I need to create the below json format for a post to rest api in c#. The below call works and I have used successfully in Postman to add it to the target system.



{
"item": {
"attrs": {
"attr": [{
"name": "IP_Category",
"value": "Miscellaneous"
}, {
"name": "Description",
"value": "Picture of Rabbit"
}, {
"name": "Title",
"value": "A Rabbit"
}
]
},
"resrs": {
"res": [{
"filename": "Rabbit.jpg",
"base64": "/9j/4AAQSkZJR"
}
]
},
"acl": {
"name": "Submitter"
},
"entityName": "IP_Document"
}
}


Based on the research I've done I need to copy and "paste special" into a new class file in visual studio so it can create the class objects based on the json format (Pretty cool!). And this is what it creates:



namespace BasicWebApp
{

public class Rootobject
{
public Item item { get; set; }
}

public class Item
{
public Attrs attrs { get; set; }
public Resrs resrs { get; set; }
public Acl acl { get; set; }
public string entityName { get; set; }
}

public class Attrs
{
public Attr attr { get; set; }
}

public class Attr
{
public string name { get; set; }
public string value { get; set; }
}

public class Resrs
{
public Re res { get; set; }
}

public class Re
{
public string filename { get; set; }
public string base64 { get; set; }
}

public class Acl
{
public string name { get; set; }
}
}


Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?



Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?



var model = new Rootobject();
model.item = new Item
{
attrs = new Attrs
{
attr = new List<Attr>
{
**now what??**
}
}
}









share|improve this question













Big apologies for the long post. I need to create the below json format for a post to rest api in c#. The below call works and I have used successfully in Postman to add it to the target system.



{
"item": {
"attrs": {
"attr": [{
"name": "IP_Category",
"value": "Miscellaneous"
}, {
"name": "Description",
"value": "Picture of Rabbit"
}, {
"name": "Title",
"value": "A Rabbit"
}
]
},
"resrs": {
"res": [{
"filename": "Rabbit.jpg",
"base64": "/9j/4AAQSkZJR"
}
]
},
"acl": {
"name": "Submitter"
},
"entityName": "IP_Document"
}
}


Based on the research I've done I need to copy and "paste special" into a new class file in visual studio so it can create the class objects based on the json format (Pretty cool!). And this is what it creates:



namespace BasicWebApp
{

public class Rootobject
{
public Item item { get; set; }
}

public class Item
{
public Attrs attrs { get; set; }
public Resrs resrs { get; set; }
public Acl acl { get; set; }
public string entityName { get; set; }
}

public class Attrs
{
public Attr attr { get; set; }
}

public class Attr
{
public string name { get; set; }
public string value { get; set; }
}

public class Resrs
{
public Re res { get; set; }
}

public class Re
{
public string filename { get; set; }
public string base64 { get; set; }
}

public class Acl
{
public string name { get; set; }
}
}


Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?



Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?



var model = new Rootobject();
model.item = new Item
{
attrs = new Attrs
{
attr = new List<Attr>
{
**now what??**
}
}
}






c# json rest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 5:56









Al Wol

112




112








  • 3




    res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
    – TheGeneral
    Nov 12 at 6:05








  • 1




    For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
    – Diwa
    Nov 12 at 6:06












  • And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
    – Diwa
    Nov 12 at 6:11














  • 3




    res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
    – TheGeneral
    Nov 12 at 6:05








  • 1




    For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
    – Diwa
    Nov 12 at 6:06












  • And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
    – Diwa
    Nov 12 at 6:11








3




3




res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
– TheGeneral
Nov 12 at 6:05






res is not a reserved word, however your Json to C# converter (in this case Visual Studio) is thinking res is plural for re's
– TheGeneral
Nov 12 at 6:05






1




1




For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
– Diwa
Nov 12 at 6:06






For your question "res JSON object to class Re", Visual Studio considered "Re" as the singular form of "Res". Similarly, you can check the same with Attr and Attrs.
– Diwa
Nov 12 at 6:06














And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
– Diwa
Nov 12 at 6:11




And for your problem 2, my personal suggestion would be to use 3rd party libraries such as Newtonsoft, which will make things easy.
– Diwa
Nov 12 at 6:11












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Don't have answer to your first question yet(but looking for it):



For your second question, at now what part you just do:



new Attr(){ name = "name1", value = "value1" },
new Attr(){ name = "name1", value = "value2" }


But that's not what i advise. I advise you to have your Attr collection ready then assign it. Like:



var model = new Rootobject();
model.item = new Item
{
attrs = yourAttrCollection
}


It also goes for everyting else you do. Have your stuff ready, then assign them. It increases readability in nested objects.






share|improve this answer




























    up vote
    0
    down vote














    Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?




    No Re is not reserved word in class.
    As mentioned by TheGeneral




    res is plural for re's




    You can use JsonProperty attribute to resolve this issue like



    public class Resrs
    {
    [JsonProperty("res")]
    public Re res { get; set; }
    }



    Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?




    var model = new Rootobject();
    model.item = new Item
    {
    attrs = new Attrs
    {
    attr = new List<Attr>
    {
    new Attr { name = "abc", value = "ABC" },
    new Attr { name = "pqr", value = "PQR" }
    }
    }
    }





    share|improve this answer





















    • I used the below code to create the json structure I needed. Thanks to all that responded:
      – Al Wol
      Nov 15 at 15:34


















    up vote
    0
    down vote













    Thanks to all that responded. It helped. I ended up using the below code to create the json object I needed.



    var model = new RootObject();
    model.item = new Item
    {
    attrs = new Attrs
    {
    attr = new List<Attr>
    {
    new Attr { name = "IP_Category", value = ddlContent.Text },
    new Attr { name = "Description", value = txtDesc.Text },
    new Attr { name = "Title", value = txtTitle.Text }
    }
    },
    resrs = new Resrs
    {
    res = new List<Re>
    {
    new Re { filename = fName, base64 = base64string},
    }
    },
    acl = new Acl
    {
    name = "Submitter"
    },
    entityName = "IP_Document"
    };





    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',
      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%2f53256566%2fserializing-nested-json-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Don't have answer to your first question yet(but looking for it):



      For your second question, at now what part you just do:



      new Attr(){ name = "name1", value = "value1" },
      new Attr(){ name = "name1", value = "value2" }


      But that's not what i advise. I advise you to have your Attr collection ready then assign it. Like:



      var model = new Rootobject();
      model.item = new Item
      {
      attrs = yourAttrCollection
      }


      It also goes for everyting else you do. Have your stuff ready, then assign them. It increases readability in nested objects.






      share|improve this answer

























        up vote
        0
        down vote













        Don't have answer to your first question yet(but looking for it):



        For your second question, at now what part you just do:



        new Attr(){ name = "name1", value = "value1" },
        new Attr(){ name = "name1", value = "value2" }


        But that's not what i advise. I advise you to have your Attr collection ready then assign it. Like:



        var model = new Rootobject();
        model.item = new Item
        {
        attrs = yourAttrCollection
        }


        It also goes for everyting else you do. Have your stuff ready, then assign them. It increases readability in nested objects.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Don't have answer to your first question yet(but looking for it):



          For your second question, at now what part you just do:



          new Attr(){ name = "name1", value = "value1" },
          new Attr(){ name = "name1", value = "value2" }


          But that's not what i advise. I advise you to have your Attr collection ready then assign it. Like:



          var model = new Rootobject();
          model.item = new Item
          {
          attrs = yourAttrCollection
          }


          It also goes for everyting else you do. Have your stuff ready, then assign them. It increases readability in nested objects.






          share|improve this answer












          Don't have answer to your first question yet(but looking for it):



          For your second question, at now what part you just do:



          new Attr(){ name = "name1", value = "value1" },
          new Attr(){ name = "name1", value = "value2" }


          But that's not what i advise. I advise you to have your Attr collection ready then assign it. Like:



          var model = new Rootobject();
          model.item = new Item
          {
          attrs = yourAttrCollection
          }


          It also goes for everyting else you do. Have your stuff ready, then assign them. It increases readability in nested objects.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 6:06









          Doruk

          597721




          597721
























              up vote
              0
              down vote














              Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?




              No Re is not reserved word in class.
              As mentioned by TheGeneral




              res is plural for re's




              You can use JsonProperty attribute to resolve this issue like



              public class Resrs
              {
              [JsonProperty("res")]
              public Re res { get; set; }
              }



              Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?




              var model = new Rootobject();
              model.item = new Item
              {
              attrs = new Attrs
              {
              attr = new List<Attr>
              {
              new Attr { name = "abc", value = "ABC" },
              new Attr { name = "pqr", value = "PQR" }
              }
              }
              }





              share|improve this answer





















              • I used the below code to create the json structure I needed. Thanks to all that responded:
                – Al Wol
                Nov 15 at 15:34















              up vote
              0
              down vote














              Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?




              No Re is not reserved word in class.
              As mentioned by TheGeneral




              res is plural for re's




              You can use JsonProperty attribute to resolve this issue like



              public class Resrs
              {
              [JsonProperty("res")]
              public Re res { get; set; }
              }



              Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?




              var model = new Rootobject();
              model.item = new Item
              {
              attrs = new Attrs
              {
              attr = new List<Attr>
              {
              new Attr { name = "abc", value = "ABC" },
              new Attr { name = "pqr", value = "PQR" }
              }
              }
              }





              share|improve this answer





















              • I used the below code to create the json structure I needed. Thanks to all that responded:
                – Al Wol
                Nov 15 at 15:34













              up vote
              0
              down vote










              up vote
              0
              down vote










              Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?




              No Re is not reserved word in class.
              As mentioned by TheGeneral




              res is plural for re's




              You can use JsonProperty attribute to resolve this issue like



              public class Resrs
              {
              [JsonProperty("res")]
              public Re res { get; set; }
              }



              Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?




              var model = new Rootobject();
              model.item = new Item
              {
              attrs = new Attrs
              {
              attr = new List<Attr>
              {
              new Attr { name = "abc", value = "ABC" },
              new Attr { name = "pqr", value = "PQR" }
              }
              }
              }





              share|improve this answer













              Problem 1: Why is vs renaming the res json object to to class Re? Is it a reserved word in c#?




              No Re is not reserved word in class.
              As mentioned by TheGeneral




              res is plural for re's




              You can use JsonProperty attribute to resolve this issue like



              public class Resrs
              {
              [JsonProperty("res")]
              public Re res { get; set; }
              }



              Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code?




              var model = new Rootobject();
              model.item = new Item
              {
              attrs = new Attrs
              {
              attr = new List<Attr>
              {
              new Attr { name = "abc", value = "ABC" },
              new Attr { name = "pqr", value = "PQR" }
              }
              }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 at 6:11









              er-shoaib

              4,6742516




              4,6742516












              • I used the below code to create the json structure I needed. Thanks to all that responded:
                – Al Wol
                Nov 15 at 15:34


















              • I used the below code to create the json structure I needed. Thanks to all that responded:
                – Al Wol
                Nov 15 at 15:34
















              I used the below code to create the json structure I needed. Thanks to all that responded:
              – Al Wol
              Nov 15 at 15:34




              I used the below code to create the json structure I needed. Thanks to all that responded:
              – Al Wol
              Nov 15 at 15:34










              up vote
              0
              down vote













              Thanks to all that responded. It helped. I ended up using the below code to create the json object I needed.



              var model = new RootObject();
              model.item = new Item
              {
              attrs = new Attrs
              {
              attr = new List<Attr>
              {
              new Attr { name = "IP_Category", value = ddlContent.Text },
              new Attr { name = "Description", value = txtDesc.Text },
              new Attr { name = "Title", value = txtTitle.Text }
              }
              },
              resrs = new Resrs
              {
              res = new List<Re>
              {
              new Re { filename = fName, base64 = base64string},
              }
              },
              acl = new Acl
              {
              name = "Submitter"
              },
              entityName = "IP_Document"
              };





              share|improve this answer

























                up vote
                0
                down vote













                Thanks to all that responded. It helped. I ended up using the below code to create the json object I needed.



                var model = new RootObject();
                model.item = new Item
                {
                attrs = new Attrs
                {
                attr = new List<Attr>
                {
                new Attr { name = "IP_Category", value = ddlContent.Text },
                new Attr { name = "Description", value = txtDesc.Text },
                new Attr { name = "Title", value = txtTitle.Text }
                }
                },
                resrs = new Resrs
                {
                res = new List<Re>
                {
                new Re { filename = fName, base64 = base64string},
                }
                },
                acl = new Acl
                {
                name = "Submitter"
                },
                entityName = "IP_Document"
                };





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Thanks to all that responded. It helped. I ended up using the below code to create the json object I needed.



                  var model = new RootObject();
                  model.item = new Item
                  {
                  attrs = new Attrs
                  {
                  attr = new List<Attr>
                  {
                  new Attr { name = "IP_Category", value = ddlContent.Text },
                  new Attr { name = "Description", value = txtDesc.Text },
                  new Attr { name = "Title", value = txtTitle.Text }
                  }
                  },
                  resrs = new Resrs
                  {
                  res = new List<Re>
                  {
                  new Re { filename = fName, base64 = base64string},
                  }
                  },
                  acl = new Acl
                  {
                  name = "Submitter"
                  },
                  entityName = "IP_Document"
                  };





                  share|improve this answer












                  Thanks to all that responded. It helped. I ended up using the below code to create the json object I needed.



                  var model = new RootObject();
                  model.item = new Item
                  {
                  attrs = new Attrs
                  {
                  attr = new List<Attr>
                  {
                  new Attr { name = "IP_Category", value = ddlContent.Text },
                  new Attr { name = "Description", value = txtDesc.Text },
                  new Attr { name = "Title", value = txtTitle.Text }
                  }
                  },
                  resrs = new Resrs
                  {
                  res = new List<Re>
                  {
                  new Re { filename = fName, base64 = base64string},
                  }
                  },
                  acl = new Acl
                  {
                  name = "Submitter"
                  },
                  entityName = "IP_Document"
                  };






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 at 15:37









                  Al Wol

                  112




                  112






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53256566%2fserializing-nested-json-c-sharp%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

                      Bressuire

                      Vorschmack

                      Quarantine