locked
JsonConvert.SerializeObject Multiple Objetcs RRS feed

  • Question

  • User-1971168174 posted

    Hi,

    My API accept json in the following format which works fine when I test using Postman, Please help me with sample to post multiple object
    [
    
      {
        "ACTION":"NEW",
        "REQUEST": "35"
    
      },
    
      {
        "STATUS": "DONE",
        "PERSON":"JOHN"
      },
    
      {
        "ACTION":"NEW",
        "REQUEST": "35" 
      },
    
      {
         "STAGE":"OVERLAPP",
         "REQUEST": "35"
        
      },
    
      {
        "LOCATION":"STATION",
        "REQUEST": "35"
         }
    
     ]
    
    
    

    My Code which works fine with single object but need to modify for posting multiple object

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Request.WebForm2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Button1" runat="server" Text="post" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    
    using Newtonsoft.Json;
    using System;
    using System.Text;
    using System.Net.Http;
    using System.Web.Script.Serialization;
    
    namespace  Request
    {
        public class A
        {
            public string ACTION { get; set; }
            public string REQUEST { get; set; }
        }
    
        public class B
        {
            public string STATUS { get; set; }
            public string PERSON { get; set; }
        }
    
        public class C
        {
            public string ACTION { get; set; }
            public string REQUEST { get; set; }
        }
    
        public class D
        {
            public string STAGE { get; set; }
            public string REQUEST { get; set; }
        }
    
        public class E
        {
            public string LOCATION { get; set; }
            public string REQUEST { get; set; }
        }
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                InsertData_All();
            }
    
            public void InsertData_All()
            {
                A my_A = new A
                {
                    ACTION="NEW",
                    REQUEST="35"
                };
    
                B my_B = new B
                {
                    STATUS="DONE",
                    PERSON="JOHN"
                };
                C my_C = new C
                {
                    ACTION="NEW",
                    REQUEST="35"
                };
                D my_D = new D
                {
                    STAGE="OVERLAP",
                    REQUEST="35"
                };
                E my_E = new E
                {
                    LOCATION="STATION 35",
                    REQUEST=""
                };
    
    
    
                 HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Add("schema", "alto");
    
                var result = client.PostAsync("http://TESTAPI/ALL/NEW/API", new StringContent(
                    JsonConvert.SerializeObject(my_A), // HOW TO PASS MULTIPLE OBJECT HERE my_A,my_B,my_C,my_D,my_E ??????
                    Encoding.UTF8,
                    "application/json")
                    ).Result;
    
                var data = result.Content.ReadAsStringAsync().Result;
                
                
            }
        }
    }

    Need help to modify above code to pass multiple object

    Thanks in adavnce

    Wednesday, November 20, 2019 8:26 PM

Answers

  • User61956409 posted

    Hi neerajkumarmodi,

    neerajkumarmodi

    Please help me with sample to post multiple object

    If you'd like to generate data like you shared, you can try this code snippet.

    var d1 =new Dictionary<string, string>() { };
    d1.Add("ACTION", "NEW");
    d1.Add("REQUEST", "35");
    
    var d2 = new Dictionary<string, string>() { };
    d2.Add("STATUS", "DONE");
    d2.Add("PERSON", "JOHN");
    
    var d3 = new Dictionary<string, string>() { };
    d3.Add("ACTION", "NEW");
    d3.Add("REQUEST", "35");
    
    var d4 = new Dictionary<string, string>() { };
    d4.Add("STAGE", "OVERLAPP");
    d4.Add("REQUEST", "35");
    
    var d5 = new Dictionary<string, string>() { };
    d5.Add("LOCATION", "STATION");
    d5.Add("REQUEST", "35");
    
    List<Dictionary<string, string>> reqdata = new List<Dictionary<string, string>>() { d1, d2, d3, d4, d5 };
    
    var jdata = JsonConvert.SerializeObject(reqdata);

    Test Result

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2019 5:23 AM

All replies

  • User475983607 posted

    The design make little sense.  Class A and C are the same type.  A, C, D, and E all have the same request value.  Seems like the class can be reduced to...

        public class RequestModel
        {
            public string REQUEST { get; set; }
            public string ACTION { get; set; }
            public string STATUS { get; set; }
            public string STAGE { get; set; }
            public string LOCATION { get; set; }
        }

    Then an array of RequestModelis simply

    RequestModel[] Requests

    or 

    List<RequestModel> Requests

    Maybe if you explain the problem you are trying to solve we can provide better recommendations.

    Wednesday, November 20, 2019 8:45 PM
  • User-1971168174 posted

    Thanks for Reply!

    Above is sample , in actual scenario there will be 10+ fields for each class.I need to pass multiple objects or if some other way.

    Thursday, November 21, 2019 1:42 AM
  • User-474980206 posted

    json objects are type less. To deserialize json into a collection of different types is not supported out of the box by any json deserializers. You will need to write your own custom deserializer. If you are using newton soft see:

    https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

    Thursday, November 21, 2019 1:48 AM
  • User-1971168174 posted

    Thanks bruce,

    Is there a way to post data to api? which accepts  json  exactly in the below format ( in actual case there will be 10+  fields for all sections below ) ,Please help me with the sample code to post below json

    [
    
      {
        "ACTION":"NEW",
        "REQUEST": "35"
    
      },
    
      {
        "STATUS": "DONE",
        "PERSON":"JOHN"
      },
    
      {
        "ACTION":"NEW",
        "REQUEST": "35" 
      },
    
      {
         "STAGE":"OVERLAPP",
         "REQUEST": "35"
        
      },
    
      {
        "LOCATION":"STATION",
        "REQUEST": "35"
         }
    
     ]

    Thursday, November 21, 2019 2:32 AM
  • User61956409 posted

    Hi neerajkumarmodi,

    neerajkumarmodi

    Please help me with sample to post multiple object

    If you'd like to generate data like you shared, you can try this code snippet.

    var d1 =new Dictionary<string, string>() { };
    d1.Add("ACTION", "NEW");
    d1.Add("REQUEST", "35");
    
    var d2 = new Dictionary<string, string>() { };
    d2.Add("STATUS", "DONE");
    d2.Add("PERSON", "JOHN");
    
    var d3 = new Dictionary<string, string>() { };
    d3.Add("ACTION", "NEW");
    d3.Add("REQUEST", "35");
    
    var d4 = new Dictionary<string, string>() { };
    d4.Add("STAGE", "OVERLAPP");
    d4.Add("REQUEST", "35");
    
    var d5 = new Dictionary<string, string>() { };
    d5.Add("LOCATION", "STATION");
    d5.Add("REQUEST", "35");
    
    List<Dictionary<string, string>> reqdata = new List<Dictionary<string, string>>() { d1, d2, d3, d4, d5 };
    
    var jdata = JsonConvert.SerializeObject(reqdata);

    Test Result

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2019 5:23 AM
  • User-1971168174 posted

    Thanks a lot Fie for your help!

    Thursday, November 21, 2019 9:26 AM