0

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.

  • You can get solution [convert json to array](https://stackoverflow.com/questions/9586585/convert-json-to-a-c-sharp-array) – Darji Sonali Nov 21 '18 at 08:57
  • @DarjiSonali that's *not* a good link. JavaScriptSerializer is obsolete. Web API uses Json.NET already – Panagiotis Kanavos Nov 21 '18 at 09: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 09: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 06: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 07:44
  • @AlexeiLevenkov all the OP has to do is `return new[]{new SubjectMark{Subject="Maths",Mark=16},new SubjectMark{Subject="IT",Mark=90}];`. Or `return new Marks{Report=new[]{...}}`. – Panagiotis Kanavos Nov 22 '18 at 07:48
  • @AlexeiLevenkov that's assumes that `get a JSON response that consists of seeded data ` means `return a JSON reponse that ..` and not `parse a JSON response into objects that match the data`. Without the code, it's hard to guess whether the OP means one thing or the other. – Panagiotis Kanavos Nov 22 '18 at 07:50

2 Answers2

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);
Hamza Haider
  • 730
  • 6
  • 20
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/

begovsky
  • 1
  • 1