Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to create an api in which final output would be look like that:-

{
	"authdata": {
		"version": "1.0",
		"Type": "APIResponse",
		"PDateTime": "9/14/2022 6:18:34 AM",
		"status": "SUCCESS"
	},
	"data": [
		{
			"orderID": "",
			"Orderdetails ": [
				{
					"orderNumber": "291323150",
					"ID": "0001",
					"orderDate": "9/2/2022 12:00:00 AM",
				}
			],
			"Partdetails ": [
				{
					"key": "pincode",
					"value": "34598702"
				},
				{
					"key": "deliveryStatus",
					"value": "OptedOut"
				},
				{
					"key": "deliveryDate",
					"value": "2022-09-02T00:00:00"
				},
				{
					"key": "Status",
					"value": "successfully dispatch"
				}
			]
		}
	]
}


to get such json response i create this modal:-

public class myResponse
 {
     public Authdata authdata { get; set; }
     public List<orderdata> data { get; set; }
 }


 public class Authdata
 {
     public string version { get; set; }
     public string Type { get; set; }
     public string PDateTime { get; set; }
     public string status { get; set; }
 }

 public class orderdata
 {
     public string orderID { get; set; }

     [JsonProperty("Orderdetails ")]
     public List<Orderdetail> Orderdetails { get; set; }

     [JsonProperty("Partdetails ")]
     public List<Partdetail> Partdetails { get; set; }
 }

 public class Orderdetail
 {
     public string orderNumber { get; set; }
     public string ID { get; set; }
     public string orderDate { get; set; }
 }

 public class Partdetail
 {
     public string key { get; set; }
     public object value { get; set; }
 }


now i have a function in which i passed all the data list and bind assemble it as per my output response

private myResponse outputResponse(List<od> odData, List<pd> pdData,string oid)
 {
     return new myResponse
     {
         authdata = new Authdata
         {
             version = "1.0",
             Type = "APIResponse",
             PDateTime = "9/14/2022 6:18:34 AM",
             status ="SUCCESS"
         },

         data = new List<orderdata>
         {
             orderID = oid,
             Orderdetails = odData
             Partdetails = pdData
         }
     };
 }



It's give me an error that orderdata does not contain a defination of orderID,Orderdetails,Partdetails.

can any one sugguest me the solution to implement this.

What I have tried:

i changed my modal code from this:
public class myResponse
   {
       public Authdata authdata { get; set; }
       public List<orderdata> data { get; set; }
   }


to this
public class myResponse
   {
       public Authdata authdata { get; set; }
       public orderdata data { get; set; }
   }


it's working but not getting expected result.
Posted
Updated 16-Sep-22 1:26am

1 solution

You are trying to create data as a List<T>, not as an orderdata item:
C#
data = new List<orderdata>
{
    orderID = oid,
    Orderdetails = odData
    Partdetails = pdData
}

And Lists do not contain members named orderID, Orderdetails or Partdetails.
 
Share this answer
 
Comments
TCS54321 16-Sep-22 7:59am    
thanx for the reply. i try following code and it's working fine for orderdetail list:-
-->
data = new List<orderdata> { new orderdata{ orderID = oid, Orderdetails = odData} }
Richard MacCutchan 16-Sep-22 8:04am    
So what is your problem?
TCS54321 16-Sep-22 8:12am    
my issue resolved. thanx for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900