将C#对象转换为Json对象

我试图将C#对象序列化为Json对象。 然后将其提交给Salesforce API,并创建一个应用程序。 现在我将C#对象序列化为Json字符串,但我需要它作为一个对象。

这是我的C#对象以及伴随序列化。

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; 

我需要在JSON对象内输出JSON字符串。 我需要的一个例子如下:

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

这个硬编码的json字符串位于对象内部。 就目前而言,C#对象中的值被输出到JSON字符串中,但我需要将其输出到对象中,以便Salesforce API接受提交。

如何将JSON字符串追加或插入对象?

要首先创建正确的JSON,您需要准备适当的模型。 它可以是这样的:

 [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; } } 

为了能够使用Data属性,您需要选择其他一些JSON序列化程序。 例如DataContractJsonSerializer或Json.NET (我将在此示例中使用它)。

 Customer customer = new Customer { ProductDescription = tbDescription.Text, Fname = tbFName.Text, Lname = tbLName.Text }; string creditApplicationJson = JsonConvert.SerializeObject( new { jsonCreditApplication = customer }); 

所以jsonCreditApplication变量将是:

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

其他方式。

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

  /* 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 }); 

看到

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

在撰写本文时我的packages.config …虽然我确定未来/最新版本仍将支持它:

     

您可以使用类似http://restsharp.org/的内容 ,这是REST的ac#库。 如果是这样,它有一个用于json对象的内置序列化器(.addJsonBody()),或者你可以自己序列化它并添加

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

或者,如果您想要更多地控制它,您可以使用

  System.Net.HttpWebRequest() 

我还发现了https://github.com/ademargomes/JsonRequest ,但它仍处于开发阶段。 请注意,如果您使用RestSharp之类的东西,它是一个预制请求,因此它们作为标准请求所创建的内容(例如,带有json或自定义标头的多部分/表单数据甚至自定义身份validation)的任何变化都可能无法与其库一起使用,在这种情况下,无论如何最好自己使用HttpWebRequest。 希望有所帮助!

安装Newtonsoft.Json NuGet然后使用require命名装饰来装饰Customer类,告诉Json序列化器如何序列化customer class字段:

 public class Customer { [JsonProperty("gors_descr")] public string ProductDescription; [JsonProperty("b_name_first")] public string Fname; [JsonProperty("b_name_last")] public string Lname; } 

接下来,像这样序列化对象:

 Customer application = new Customer { ProductDescription = "Appliances ", Fname = "Marisol ", Lname = "Testcase " }; var JsonOutput = JsonConvert.SerializeObject(new { jsonCreditApplication = application }); 

您将获得所需的结果, JsonOutput的值将为: "{\"jsonCreditApplication\":{\"gors_descr\":\"Appliances \",\"b_name_first\":\"Marisol \",\"b_name_last\":\"Testcase \"}}"

有很多方法可以做到这一点,但我相信这是最简单的解决方案。