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
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
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.
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:
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
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
58 Points
176 Posts
JsonConvert.SerializeObject Multiple Objetcs
Nov 20, 2019 08:26 PM|neerajkumarmodi|LINK
Hi,
My Code which works fine with single object but need to modify for posting multiple object
Need help to modify above code to pass multiple object
Thanks in adavnce
All-Star
53051 Points
23634 Posts
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 20, 2019 08:45 PM|mgebhard|LINK
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...
Then an array of RequestModelis simply
or
Maybe if you explain the problem you are trying to solve we can provide better recommendations.
Member
58 Points
176 Posts
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 21, 2019 01:42 AM|neerajkumarmodi|LINK
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.
All-Star
58214 Points
15668 Posts
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 21, 2019 01:48 AM|bruce (sqlwork.com)|LINK
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
Member
58 Points
176 Posts
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 21, 2019 02:32 AM|neerajkumarmodi|LINK
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
All-Star
40565 Points
6233 Posts
Microsoft
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 21, 2019 05:23 AM|Fei Han - MSFT|LINK
Hi neerajkumarmodi,
If you'd like to generate data like you shared, you can try this code snippet.
Test Result
With Regards,
Fei Han
Member
58 Points
176 Posts
Re: JsonConvert.SerializeObject Multiple Objetcs
Nov 21, 2019 09:26 AM|neerajkumarmodi|LINK
Thanks a lot Fie for your help!