【发布时间】:2016-04-24 01:22:55
【问题描述】:
我正在尝试将 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 字符串有效,您实际上可以使用此站点将 json 字符串转换为 C# 类 json2csharp.com 也可以查看指向 convert C# Object into Json 的链接
-
首先,当您序列化
application时,您将获得类似于:{"ProductDescription": "gors_descr Appliances", "Fname": "b_name_first Marisol", ...}的 JSON。它看起来不像你想要的 JSON。
标签: c# json api serialization