I am trying to serialize a C# object into a Json object. That will then be submitted to the Salesforce API, and create an application. Right now I have the C# object serialized into a Json string, but I need it to be an object.
Here is my C# object along with accompany serialization.
Customer application = new Customer {
ProductDescription = "gors_descr " + tbDescription.Text,
Fname = "b_name_first " + tbFName.Text,
Lname = "b_name_last " + tbLName.Text
};
var json = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = json.Serialize(application);
string endPoint = token.instance_url + "/services/apexrest/submitApplication/";
string response = conn.HttpPost(endPoint, json, token);
Literal rLiteral = this.FindControl("resultLiteral") as Literal;
I need the JSON string to output inside of a JSON Object. An example of what I need is below:
"{ \"jsonCreditApplication\" : " +
"\"gors_descr\" : \"Appliances\", " +
"\"b_name_first\" : \"Marisol\", " +
"\"b_name_last\" : \"Testcase\", " +
"}";
This hard coded json string is inside of an object. As it stands, the values in the C# object are being outputted into a JSON string, but I'm needing it output into an object so that the Salesforce API will accept the submission.
How can I append or insert the JSON string into an object?
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
CurrentUTCDateTime yourObject = new CurrentUTCDateTime();
JObject json = JObject.Parse(JsonConvert.SerializeObject(yourObject));
To create correct JSON first you need to prepare appropriate model. It can be something like that:
[DataContract]
public class Customer
{
[DataMember(Name = "gors_descr")]
public string ProductDescription { get; set; }
[DataMember(Name = "b_name_first")]
public string Fname { get; set; }
[DataMember(Name = "b_name_last")]
public string Lname { get; set; }
}
To be able to use Data
attributes you will need to choose some other JSON serializer. For example DataContractJsonSerializer or Json.NET(I will use it in this example).
Customer customer = new Customer
{
ProductDescription = tbDescription.Text,
Fname = tbFName.Text,
Lname = tbLName.Text
};
string creditApplicationJson = JsonConvert.SerializeObject(
new
{
jsonCreditApplication = customer
});
So jsonCreditApplication
variable will be:
{
"jsonCreditApplication": {
"gors_descr": "Appliances",
"b_name_first": "Marisol",
"b_name_last": "Testcase"
}
}
Install Newtonsoft.Json NuGet then decorate Customer class with the requires naming decorations to tell Json serializer how to serialize the customer class fields:
public class Customer
{
[JsonProperty("gors_descr")]
public string ProductDescription;
[JsonProperty("b_name_first")]
public string Fname;
[JsonProperty("b_name_last")]
public string Lname;
}
Next, serialize the object like this:
Customer application = new Customer
{
ProductDescription = "Appliances ",
Fname = "Marisol ",
Lname = "Testcase "
};
var JsonOutput = JsonConvert.SerializeObject(new { jsonCreditApplication = application });
You will get the desired result and the value of JsonOutput
will be :
"{\"jsonCreditApplication\":{\"gors_descr\":\"Appliances \",\"b_name_first\":\"Marisol \",\"b_name_last\":\"Testcase \"}}"
There are many ways to do this but I believe that this one is the simplest solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With