5

I am sending the Json format like this in PostMan

    {
  "number": 2106887,
  "date": "09/10/2018",
  "degree":"BE"
  "Students": [
    {
      "Branch": "ABK015",
      "Doc": "NCE",
      "Description": "Testing",
      "dni": "1016035232",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    },
    {
      "Branch": "ABK016",
      "Doc": "NCE",
      "Description": "Testing1",
      "dni": "1016035233",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    }
  ]
}

In controller level i am doing the validation of all fields. After validation how can i convert the above json string to below format

{

  "Students": [
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK015",
      "Doc": "NCE",
      "Description": "Testing",
      "dni": "1016035232",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    },
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK016",
      "Doc": "NCE",
      "Description": "Testing1",
      "dni": "1016035233",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    }
  ]
}

And aftre converting, i want to insert to database. How to convert in c#? Please help me.

And the below code is the class for students:

public class Students
{
    [Required]
    public string Branch{ get; set; }
    [Required]
    public string Doc{ get; set; }
    [Required]
    public string Description{ get; set; }
    [Required]
    public string dni{ get; set; }
    [Required]
    public string Name{ get; set; }
    [Required]
    public string Gender{ get; set; }
    [Required]
    public string Title{ get; set; }
    [Required]
    public string email{ get; set; }
    [Required]
    public string degree{ get; set; }
    [Required]
    public string date{ get; set; }
    [Required]
    public string number{ get; set; }
}

And i am deserializing here

var requestBody = requestContent.Content.ReadAsStringAsync().Result;
            JObject jxxx = JsonConvert.DeserializeObject<dynamic>(requestBody);

Please refer the updated code

6
  • 1
    By creating that structure and serializing. Commented Mar 15, 2019 at 13:08
  • 1
    Please show us some code, including your relevant models and controller methods. Commented Mar 15, 2019 at 13:08
  • @Amy var requestBody = requestContent.Content.ReadAsStringAsync().Result; JObject jxxx = JsonConvert.DeserializeObject<dynamic>(requestBody); here the requestContent is json format Commented Mar 15, 2019 at 13:18
  • The json you return will result from the model youre returning if you're following normal conventions. As @Amy said, we need to see your models and controller action Commented Mar 15, 2019 at 13:20
  • @GregH i have created the class for Students with all fields Commented Mar 15, 2019 at 13:26

2 Answers 2

3

You can use DeserializeAnonymousType method. First create a template object

var template = new {number = "", date = "", degree = "", Students = new Students[0]};

now deserialize to temporary json object:

var jsonObject = JsonConvert.DeserializeAnonymousType(input, template);

After this you can copy info from json header to Students with linq:

var students = jsonObject.Students.Select(s =>
{
    s.number = jsonObject.number;
    s.date = jsonObject.date;
    s.degree = jsonObject.degree;
    return s;
}).ToArray();

Also note, that your input JSON is not valid: it missing comma afetr degree value.

Demo is here

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, here how can i add the Student list?
@Chandu replacre ToArray with ToList and then you can call students.Add(...
@Alexs, i have one more doubt. How to convert all the tagnames(keys) to lowercase? like Branch to branch, Doc to doc, Description to description
0
"Students": [
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK015",
      "Doc": "NCE",
      "Description": "Testing",
      "dni": "1016035232",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    },
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK016",
      "Doc": "NCE",
      "Description": "Testing1",
      "dni": "1016035233",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    }
  ]

And then try this

var students = JsonConvert.DeserializeObject<Students[]>(requestBody);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.