Convert Json response to object array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I'm looking for the best solution.



Here is a response from server and i need to get Organizations list:



Content-Type:application/json;charset=UTF-8
{
"code": 0,
"message": "success",
"organizations": [

"organization_id": "10234695",
"name": "Zillum",
"contact_name": "John Smith",
"email": "johnsmith@zillum.com",
"is_default_org": false,
"language_code": "en",
"fiscal_year_start_month": 0,
"account_created_date": "2016-02-18",
"time_zone": "PST",
"is_org_active": true,
"currency_id": "460000000000097",
"currency_code": "USD",
"currency_symbol": "$",
"currency_format": "###,##0.00",
"price_precision": 2
,
...,
...
]


Here is my convert method:



var contentJson = await SendRequest(request);
var contentJo = (JObject)JsonConvert.DeserializeObject(contentJson);
var organizationsJArray = contentJo["organizations"]
.Value<JArray>();

var organizations = organizationsJArray.ToObject<List<Organization>>();


Code works, but I'm looking for a better Convert Method. Can I do without converting to JArray?







share|improve this question





















  • You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
    – Adriano Repetti
    Jun 4 at 9:35










  • You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
    – t3chb0t
    Jun 4 at 9:38










  • @t3chb0t it cannot be convert directly, because root object it's not an Array
    – Nikita Goncharuk
    Jun 4 at 10:15











  • oh, ok, then put it in another object that has a property of this type.
    – t3chb0t
    Jun 4 at 10:16










  • @AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
    – Nikita Goncharuk
    Jun 4 at 10:23
















up vote
3
down vote

favorite












I'm looking for the best solution.



Here is a response from server and i need to get Organizations list:



Content-Type:application/json;charset=UTF-8
{
"code": 0,
"message": "success",
"organizations": [

"organization_id": "10234695",
"name": "Zillum",
"contact_name": "John Smith",
"email": "johnsmith@zillum.com",
"is_default_org": false,
"language_code": "en",
"fiscal_year_start_month": 0,
"account_created_date": "2016-02-18",
"time_zone": "PST",
"is_org_active": true,
"currency_id": "460000000000097",
"currency_code": "USD",
"currency_symbol": "$",
"currency_format": "###,##0.00",
"price_precision": 2
,
...,
...
]


Here is my convert method:



var contentJson = await SendRequest(request);
var contentJo = (JObject)JsonConvert.DeserializeObject(contentJson);
var organizationsJArray = contentJo["organizations"]
.Value<JArray>();

var organizations = organizationsJArray.ToObject<List<Organization>>();


Code works, but I'm looking for a better Convert Method. Can I do without converting to JArray?







share|improve this question





















  • You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
    – Adriano Repetti
    Jun 4 at 9:35










  • You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
    – t3chb0t
    Jun 4 at 9:38










  • @t3chb0t it cannot be convert directly, because root object it's not an Array
    – Nikita Goncharuk
    Jun 4 at 10:15











  • oh, ok, then put it in another object that has a property of this type.
    – t3chb0t
    Jun 4 at 10:16










  • @AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
    – Nikita Goncharuk
    Jun 4 at 10:23












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm looking for the best solution.



Here is a response from server and i need to get Organizations list:



Content-Type:application/json;charset=UTF-8
{
"code": 0,
"message": "success",
"organizations": [

"organization_id": "10234695",
"name": "Zillum",
"contact_name": "John Smith",
"email": "johnsmith@zillum.com",
"is_default_org": false,
"language_code": "en",
"fiscal_year_start_month": 0,
"account_created_date": "2016-02-18",
"time_zone": "PST",
"is_org_active": true,
"currency_id": "460000000000097",
"currency_code": "USD",
"currency_symbol": "$",
"currency_format": "###,##0.00",
"price_precision": 2
,
...,
...
]


Here is my convert method:



var contentJson = await SendRequest(request);
var contentJo = (JObject)JsonConvert.DeserializeObject(contentJson);
var organizationsJArray = contentJo["organizations"]
.Value<JArray>();

var organizations = organizationsJArray.ToObject<List<Organization>>();


Code works, but I'm looking for a better Convert Method. Can I do without converting to JArray?







share|improve this question













I'm looking for the best solution.



Here is a response from server and i need to get Organizations list:



Content-Type:application/json;charset=UTF-8
{
"code": 0,
"message": "success",
"organizations": [

"organization_id": "10234695",
"name": "Zillum",
"contact_name": "John Smith",
"email": "johnsmith@zillum.com",
"is_default_org": false,
"language_code": "en",
"fiscal_year_start_month": 0,
"account_created_date": "2016-02-18",
"time_zone": "PST",
"is_org_active": true,
"currency_id": "460000000000097",
"currency_code": "USD",
"currency_symbol": "$",
"currency_format": "###,##0.00",
"price_precision": 2
,
...,
...
]


Here is my convert method:



var contentJson = await SendRequest(request);
var contentJo = (JObject)JsonConvert.DeserializeObject(contentJson);
var organizationsJArray = contentJo["organizations"]
.Value<JArray>();

var organizations = organizationsJArray.ToObject<List<Organization>>();


Code works, but I'm looking for a better Convert Method. Can I do without converting to JArray?









share|improve this question












share|improve this question




share|improve this question








edited Jun 4 at 9:39









t3chb0t

31.9k54195




31.9k54195









asked Jun 4 at 8:53









Nikita Goncharuk

183




183











  • You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
    – Adriano Repetti
    Jun 4 at 9:35










  • You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
    – t3chb0t
    Jun 4 at 9:38










  • @t3chb0t it cannot be convert directly, because root object it's not an Array
    – Nikita Goncharuk
    Jun 4 at 10:15











  • oh, ok, then put it in another object that has a property of this type.
    – t3chb0t
    Jun 4 at 10:16










  • @AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
    – Nikita Goncharuk
    Jun 4 at 10:23
















  • You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
    – Adriano Repetti
    Jun 4 at 9:35










  • You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
    – t3chb0t
    Jun 4 at 9:38










  • @t3chb0t it cannot be convert directly, because root object it's not an Array
    – Nikita Goncharuk
    Jun 4 at 10:15











  • oh, ok, then put it in another object that has a property of this type.
    – t3chb0t
    Jun 4 at 10:16










  • @AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
    – Nikita Goncharuk
    Jun 4 at 10:23















You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
– Adriano Repetti
Jun 4 at 9:35




You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?)
– Adriano Repetti
Jun 4 at 9:35












You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
– t3chb0t
Jun 4 at 9:38




You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first.
– t3chb0t
Jun 4 at 9:38












@t3chb0t it cannot be convert directly, because root object it's not an Array
– Nikita Goncharuk
Jun 4 at 10:15





@t3chb0t it cannot be convert directly, because root object it's not an Array
– Nikita Goncharuk
Jun 4 at 10:15













oh, ok, then put it in another object that has a property of this type.
– t3chb0t
Jun 4 at 10:16




oh, ok, then put it in another object that has a property of this type.
– t3chb0t
Jun 4 at 10:16












@AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
– Nikita Goncharuk
Jun 4 at 10:23




@AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json
– Nikita Goncharuk
Jun 4 at 10:23










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.



var contentJson = await SendRequest(request);
dynamic response = JsonConvert.DeserializeObject(contentJson);
List<Organization> organizations = response.organizations.ToObject<List<Organization>>();


The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.






share|improve this answer






























    up vote
    1
    down vote













    Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:



    class ContentData

    public List<Organization> organizations;


    ...

    var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
    DoSomething(contentData.organizations);





    share|improve this answer





















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      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: "196"
      ;
      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: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195795%2fconvert-json-response-to-object-array%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.



      var contentJson = await SendRequest(request);
      dynamic response = JsonConvert.DeserializeObject(contentJson);
      List<Organization> organizations = response.organizations.ToObject<List<Organization>>();


      The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.






      share|improve this answer



























        up vote
        2
        down vote



        accepted










        Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.



        var contentJson = await SendRequest(request);
        dynamic response = JsonConvert.DeserializeObject(contentJson);
        List<Organization> organizations = response.organizations.ToObject<List<Organization>>();


        The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.






        share|improve this answer

























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.



          var contentJson = await SendRequest(request);
          dynamic response = JsonConvert.DeserializeObject(contentJson);
          List<Organization> organizations = response.organizations.ToObject<List<Organization>>();


          The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.






          share|improve this answer















          Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.



          var contentJson = await SendRequest(request);
          dynamic response = JsonConvert.DeserializeObject(contentJson);
          List<Organization> organizations = response.organizations.ToObject<List<Organization>>();


          The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 5 at 9:22


























          answered Jun 5 at 0:43









          Nkosi

          1,868619




          1,868619






















              up vote
              1
              down vote













              Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:



              class ContentData

              public List<Organization> organizations;


              ...

              var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
              DoSomething(contentData.organizations);





              share|improve this answer

























                up vote
                1
                down vote













                Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:



                class ContentData

                public List<Organization> organizations;


                ...

                var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
                DoSomething(contentData.organizations);





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:



                  class ContentData

                  public List<Organization> organizations;


                  ...

                  var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
                  DoSomething(contentData.organizations);





                  share|improve this answer













                  Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:



                  class ContentData

                  public List<Organization> organizations;


                  ...

                  var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
                  DoSomething(contentData.organizations);






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 5 at 0:55









                  Errorsatz

                  3285




                  3285






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195795%2fconvert-json-response-to-object-array%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Greedy Best First Search implementation in Rust

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      C++11 CLH Lock Implementation