Convert C # object to Json object

I am trying to serialize a C # object to a Json object. This will then be presented in the Salesforce API and create the application. Right now I have a C # object serialized to a Json string, but I need it to be an object.

Here is my C # object and serialization support.

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 a JSON string to output inside a JSON object. Below is an example of what I need:

 "{ \"jsonCreditApplication\" : " + "\"gors_descr\" : \"Appliances\", " + "\"b_name_first\" : \"Marisol\", " + "\"b_name_last\" : \"Testcase\", " + "}"; 

This hardcoded json string is inside the object. Be that as it may, the values ​​in the C # object are output to the JSON string, but I need it to be output to the object for the Salesforce API to accept the view.

How to add or insert a JSON string in an object?

+7
json c # api serialization
source share
3 answers

To create the correct JSON, you first need to prepare the appropriate model. It could be something like this:

 [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 the Data attributes, you will need to select a different 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 the jsonCreditApplication variable will be:

 { "jsonCreditApplication": { "gors_descr": "Appliances", "b_name_first": "Marisol", "b_name_last": "Testcase" } } 
+13
source share

Another way.

 using System; using Newtonsoft.Json; namespace MyNamepace { public class MyCustomObject { public MyCustomObject() { } [JsonProperty(PropertyName = "my_int_one")] public int MyIntOne { get; set; } [JsonProperty(PropertyName = "my_bool_one")] public bool MyBoolOne { get; set; } } } 

and

  /* using Newtonsoft.Json; */ MyCustomObject myobj = MyCustomObject(); myobj.MyIntOne = 123; myobj.MyBoolOne = false; string jsonString = JsonConvert.SerializeObject( myobj, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); 

cm

http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm

My packages.config at time of writing ... although I am sure that future / latest versions will still support it:

 <?xml version="1.0" encoding="utf-8"?> <packages> <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" /> </packages> 
+2
source share

You can use something like http://restsharp.org/ , which is the C # library for REST. If so, it has a built-in serializer for json objects (.addJsonBody ()), or you can serialize it yourself and add using

  request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody); 

Alternatively, if you want more control over it, you can use

  System.Net.HttpWebRequest() 

I also found https://github.com/ademargomes/JsonRequest , but it is still under development. Be warned that if you use something like RestSharp, this is a canned request, so any changes from what they created as standard requests (for example, data on multipart / form w / json or custom headers or even user authentication), may not work with their library, in which case it is probably best to make your own using HttpWebRequest anyway. Hope this helps!

0
source share

All Articles