"Table_Data":[
{ // not coming above
"status": "New", //coming as "status:New", above
"requestor_email":"xyz@xyz.com",
"start_date":"8/6/2019 7:10:28 AM"
}// not coming above
]
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Web.Script.Serialization;
namespace KeeylockoRequest
{
public partial class test : System.Web.UI.Page
{
public class Keeylocko
{
public string Table_Name { get; set; }
public string User_Id { get; set; }
public string Password { get; set; }
public IList<string> Table_Data { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
Keeylocko my_data_to_pass = new Keeylocko
{
Table_Name = "new_request",
User_Id = "xyz@gmail.com",
Password = "passnew",
Table_Data = new List<string>
{
"status:New",
"requestor_email:xyz@xyz.com",
"start_date:8/6/2019 7:10:28 AM"
}
};
HttpClient client = new HttpClient();
var json = new JavaScriptSerializer().Serialize(my_data_to_pass);
var result = client.PostAsync("http://app/new/api", new StringContent(
JsonConvert.SerializeObject(my_data_to_pass),
Encoding.UTF8,
"application/json")
).Result;
var data = result.Content.ReadAsStringAsync().Result;
}
}
}
I meant in your previous thread that you should have something such as:
Keeylocko my_data_to_pass = new Keeylocko
{
Table_Name = "new_request",
User_Id = "xyz@gmail.com",
Password = "passnew",
Table_Data = new List<Data>
{
new Data(){status="New",requestor_email="xyz@xyz.com",start_date=new DateTime(2019,8,6,7,10,28) }
}
};
with :
public class Keeylocko
{
public string Table_Name { get; set; }
public string User_Id { get; set; }
public string Password { get; set; }
public IList<Data> Table_Data { get; set; }
}
public class Data
{
public string status {get;set;}
public string requestor_email { get; set; }
public DateTime start_date { get; set; }
}
A list of "key:value" string is not the same than a list of object having property names and values. Not sure about the serialized date format. You are 100% sure it needs to be this particular format ? This is your own API or a 3rd party ?
Member
58 Points
176 Posts
how to Covert fields to json format
Sep 30, 2019 04:00 PM|neerajkumarmodi|LINK
Hi ,
Need help to have following output for posting it to API ,but not able to get that using code sample below:
It will be great if someone can correct it.
Output getting as by code
{
"Table_Name":"new_request",
"User_Id":"xyz@gmail.com",
"Password":"passnew",
"Table_Data":[
"status:New",
"requestor_email:xyz@xyz.com",
"start_date:8/6/2019 7:10:28 AM"
]
}
Desired Output
{
"Table_Name":"new_request",
"User_Id":"xyz@gmail.com",
"Password":"passnew",
"Table_Data":[
{ // not coming above
"status": "New", //coming as "status:New", above
"requestor_email":"xyz@xyz.com",
"start_date":"8/6/2019 7:10:28 AM"
}// not coming above
]
}
All-Star
48490 Points
18070 Posts
Re: how to Covert fields to json format
Sep 30, 2019 04:17 PM|PatriceSc|LINK
Hi,
I meant in your previous thread that you should have something such as:
with :
A list of "key:value" string is not the same than a list of object having property names and values. Not sure about the serialized date format. You are 100% sure it needs to be this particular format ? This is your own API or a 3rd party ?
Edit: for example https://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
According to what I see the JavaScript serializer uses a format such as \/Date(1330848000000-0800)\/
Member
58 Points
176 Posts
Re: how to Covert fields to json format
Sep 30, 2019 04:43 PM|neerajkumarmodi|LINK
Thank you so much PatriceSc!
Just one more help , how to i pass schema in the code
https://imgur.com/a/2WuVf9y
All-Star
58124 Points
15635 Posts
Re: how to Covert fields to json format
Sep 30, 2019 10:27 PM|bruce (sqlwork.com)|LINK
its a header:
client.DefaultRequestHeaders.Add("schema", value);