How to get a JSON response as an array of objects in asp.net? [duplicate]












0
















This question already has an answer here:




  • Convert json to a C# array?

    3 answers



  • Parse JSON in C#

    7 answers



  • Web API method return JSON data

    1 answer



  • WebAPI Return JSON array without root node

    2 answers




I am making a Report Card System and I would like to know how get a JSON response that consists of seeded data as an array of objects instead of a singular object. So far I used a IDictionary method but it is only giving me one object eg.SubjectMark = { Math:76 }, How do I get all my seeded data instead of one instance , for example:



Report = [{
Subject:Maths
Mark: 76
},
{
Subject:IT
Mark: 90
},]


or at least



SubjectMark = [{ Math:76 },{ IT:90 }]


I am a beginner please let me know if i should add more context, Thank you.










share|improve this question













marked as duplicate by Panagiotis Kanavos, ewolden, Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You can get solution convert json to array

    – Darji Sonali
    Nov 21 '18 at 8:57











  • @DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

    – Panagiotis Kanavos
    Nov 21 '18 at 9:06











  • Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

    – Panagiotis Kanavos
    Nov 21 '18 at 9:08













  • @PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

    – Alexei Levenkov
    Nov 22 '18 at 6:36











  • @AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

    – Panagiotis Kanavos
    Nov 22 '18 at 7:44
















0
















This question already has an answer here:




  • Convert json to a C# array?

    3 answers



  • Parse JSON in C#

    7 answers



  • Web API method return JSON data

    1 answer



  • WebAPI Return JSON array without root node

    2 answers




I am making a Report Card System and I would like to know how get a JSON response that consists of seeded data as an array of objects instead of a singular object. So far I used a IDictionary method but it is only giving me one object eg.SubjectMark = { Math:76 }, How do I get all my seeded data instead of one instance , for example:



Report = [{
Subject:Maths
Mark: 76
},
{
Subject:IT
Mark: 90
},]


or at least



SubjectMark = [{ Math:76 },{ IT:90 }]


I am a beginner please let me know if i should add more context, Thank you.










share|improve this question













marked as duplicate by Panagiotis Kanavos, ewolden, Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You can get solution convert json to array

    – Darji Sonali
    Nov 21 '18 at 8:57











  • @DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

    – Panagiotis Kanavos
    Nov 21 '18 at 9:06











  • Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

    – Panagiotis Kanavos
    Nov 21 '18 at 9:08













  • @PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

    – Alexei Levenkov
    Nov 22 '18 at 6:36











  • @AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

    – Panagiotis Kanavos
    Nov 22 '18 at 7:44














0












0








0









This question already has an answer here:




  • Convert json to a C# array?

    3 answers



  • Parse JSON in C#

    7 answers



  • Web API method return JSON data

    1 answer



  • WebAPI Return JSON array without root node

    2 answers




I am making a Report Card System and I would like to know how get a JSON response that consists of seeded data as an array of objects instead of a singular object. So far I used a IDictionary method but it is only giving me one object eg.SubjectMark = { Math:76 }, How do I get all my seeded data instead of one instance , for example:



Report = [{
Subject:Maths
Mark: 76
},
{
Subject:IT
Mark: 90
},]


or at least



SubjectMark = [{ Math:76 },{ IT:90 }]


I am a beginner please let me know if i should add more context, Thank you.










share|improve this question















This question already has an answer here:




  • Convert json to a C# array?

    3 answers



  • Parse JSON in C#

    7 answers



  • Web API method return JSON data

    1 answer



  • WebAPI Return JSON array without root node

    2 answers




I am making a Report Card System and I would like to know how get a JSON response that consists of seeded data as an array of objects instead of a singular object. So far I used a IDictionary method but it is only giving me one object eg.SubjectMark = { Math:76 }, How do I get all my seeded data instead of one instance , for example:



Report = [{
Subject:Maths
Mark: 76
},
{
Subject:IT
Mark: 90
},]


or at least



SubjectMark = [{ Math:76 },{ IT:90 }]


I am a beginner please let me know if i should add more context, Thank you.





This question already has an answer here:




  • Convert json to a C# array?

    3 answers



  • Parse JSON in C#

    7 answers



  • Web API method return JSON data

    1 answer



  • WebAPI Return JSON array without root node

    2 answers








c# asp.net asp.net-web-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 8:51







user10174623











marked as duplicate by Panagiotis Kanavos, ewolden, Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Panagiotis Kanavos, ewolden, Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You can get solution convert json to array

    – Darji Sonali
    Nov 21 '18 at 8:57











  • @DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

    – Panagiotis Kanavos
    Nov 21 '18 at 9:06











  • Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

    – Panagiotis Kanavos
    Nov 21 '18 at 9:08













  • @PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

    – Alexei Levenkov
    Nov 22 '18 at 6:36











  • @AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

    – Panagiotis Kanavos
    Nov 22 '18 at 7:44



















  • You can get solution convert json to array

    – Darji Sonali
    Nov 21 '18 at 8:57











  • @DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

    – Panagiotis Kanavos
    Nov 21 '18 at 9:06











  • Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

    – Panagiotis Kanavos
    Nov 21 '18 at 9:08













  • @PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

    – Alexei Levenkov
    Nov 22 '18 at 6:36











  • @AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

    – Panagiotis Kanavos
    Nov 22 '18 at 7:44

















You can get solution convert json to array

– Darji Sonali
Nov 21 '18 at 8:57





You can get solution convert json to array

– Darji Sonali
Nov 21 '18 at 8:57













@DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

– Panagiotis Kanavos
Nov 21 '18 at 9:06





@DarjiSonali that's not a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already

– Panagiotis Kanavos
Nov 21 '18 at 9:06













Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

– Panagiotis Kanavos
Nov 21 '18 at 9:08







Why the question? JSON is Web API's default serialization format. If you return an array of objects, it will be serialized into a JSON array. If your action accepts an array of objects as a parameter, the corresponding element from the payload will be deserialized automatically. What does your code look like and why use a dictionary in the first place?

– Panagiotis Kanavos
Nov 21 '18 at 9:08















@PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

– Alexei Levenkov
Nov 22 '18 at 6:36





@PanagiotisKanavos It is somewhat confusing that you've said that suggested answers are wrong like "Yes, Json.Net is what you need" and yet you recommend to use JSON.Net... (Likely OP actually don't need any manual serialization at all but it is not what they are asking).

– Alexei Levenkov
Nov 22 '18 at 6:36













@AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

– Panagiotis Kanavos
Nov 22 '18 at 7:44





@AlexeiLevenkov why? I didn't say Json.Net is what you need. I said JavaScriptSerializer is obsolete and that Web API already uses JSON.NET. Which makes this question unclear - all the OP has to do is return the desired object, no extra serialization is required

– Panagiotis Kanavos
Nov 22 '18 at 7:44












2 Answers
2






active

oldest

votes


















0














You need to desearilize Json Object as collection.



public class ReportCard
{
public string Subject
{
get;
set;
}
public int Mark
{
get;
set;
}
}
List<ReportCard> ReportCard = JsonConvert.DeserializeObject<List<ReportCard>>
(json);





share|improve this answer































    0














    Use this



    var response = await client.PostAsync(**your_api_url**, formContent);

    response.EnsureSuccessStatusCode();

    json = await response.Content.ReadAsStringAsync();
    var data = JsonConvert.DeserializeObject<**your_object_class**>(json);


    YOUR_OBJECT_CLASS: use this http://json2csharp.com/






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You need to desearilize Json Object as collection.



      public class ReportCard
      {
      public string Subject
      {
      get;
      set;
      }
      public int Mark
      {
      get;
      set;
      }
      }
      List<ReportCard> ReportCard = JsonConvert.DeserializeObject<List<ReportCard>>
      (json);





      share|improve this answer




























        0














        You need to desearilize Json Object as collection.



        public class ReportCard
        {
        public string Subject
        {
        get;
        set;
        }
        public int Mark
        {
        get;
        set;
        }
        }
        List<ReportCard> ReportCard = JsonConvert.DeserializeObject<List<ReportCard>>
        (json);





        share|improve this answer


























          0












          0








          0







          You need to desearilize Json Object as collection.



          public class ReportCard
          {
          public string Subject
          {
          get;
          set;
          }
          public int Mark
          {
          get;
          set;
          }
          }
          List<ReportCard> ReportCard = JsonConvert.DeserializeObject<List<ReportCard>>
          (json);





          share|improve this answer













          You need to desearilize Json Object as collection.



          public class ReportCard
          {
          public string Subject
          {
          get;
          set;
          }
          public int Mark
          {
          get;
          set;
          }
          }
          List<ReportCard> ReportCard = JsonConvert.DeserializeObject<List<ReportCard>>
          (json);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:02









          Hamza HaiderHamza Haider

          666315




          666315

























              0














              Use this



              var response = await client.PostAsync(**your_api_url**, formContent);

              response.EnsureSuccessStatusCode();

              json = await response.Content.ReadAsStringAsync();
              var data = JsonConvert.DeserializeObject<**your_object_class**>(json);


              YOUR_OBJECT_CLASS: use this http://json2csharp.com/






              share|improve this answer




























                0














                Use this



                var response = await client.PostAsync(**your_api_url**, formContent);

                response.EnsureSuccessStatusCode();

                json = await response.Content.ReadAsStringAsync();
                var data = JsonConvert.DeserializeObject<**your_object_class**>(json);


                YOUR_OBJECT_CLASS: use this http://json2csharp.com/






                share|improve this answer


























                  0












                  0








                  0







                  Use this



                  var response = await client.PostAsync(**your_api_url**, formContent);

                  response.EnsureSuccessStatusCode();

                  json = await response.Content.ReadAsStringAsync();
                  var data = JsonConvert.DeserializeObject<**your_object_class**>(json);


                  YOUR_OBJECT_CLASS: use this http://json2csharp.com/






                  share|improve this answer













                  Use this



                  var response = await client.PostAsync(**your_api_url**, formContent);

                  response.EnsureSuccessStatusCode();

                  json = await response.Content.ReadAsStringAsync();
                  var data = JsonConvert.DeserializeObject<**your_object_class**>(json);


                  YOUR_OBJECT_CLASS: use this http://json2csharp.com/







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 9:07









                  begovskybegovsky

                  11




                  11















                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy