locked
how to Covert fields to json format RRS feed

  • Question

  • User-1971168174 posted

    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
    ]
    }

    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;
            }
        }
    }
    Monday, September 30, 2019 4:00 PM

Answers

  • User753101303 posted

    Hi,

    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 ?

    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)\/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 4:17 PM
  • User-474980206 posted

    its a header:

    client.DefaultRequestHeaders.Add("schema", value);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 10:27 PM

All replies

  • User753101303 posted

    Hi,

    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 ?

    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)\/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 4:17 PM
  • User-1971168174 posted

    Thank you so much PatriceSc!

    Just one more help , how to i pass schema in the code 

    https://imgur.com/a/2WuVf9y

    schema

    Monday, September 30, 2019 4:43 PM
  • User-474980206 posted

    its a header:

    client.DefaultRequestHeaders.Add("schema", value);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 10:27 PM