Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#对象转换为Json对象_C#_Json_Api_Serialization - Fatal编程技术网

将C#对象转换为Json对象

将C#对象转换为Json对象,c#,json,api,serialization,C#,Json,Api,Serialization,我试图将一个C#对象序列化为一个Json对象。然后将其提交给Salesforce API,并创建一个应用程序。现在我将C#对象序列化为Json字符串,但我需要它是一个对象 这是我的C#对象和伴随序列化 Customer application = new Customer { ProductDescription = "gors_descr " + tbDescription.Text, Fname = "b_name_first " + tbFName.Text,

我试图将一个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字符串追加或插入到对象中

您可以使用类似的东西,这是一个用于REST的c#库。如果是这样的话,它有一个内置的json对象序列化程序(.addJsonBody()),或者您可以自己序列化它并添加

    request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
或者,如果您想对其进行更多控制,可以使用

    System.Net.HttpWebRequest()
我也发现了,但它仍在开发中。
请注意,如果您使用RestSharp之类的东西,那么它是一个固定的请求,因此与他们创建的标准请求(例如,多部分/表单数据w/json或自定义头或甚至自定义身份验证)不同的任何变化都可能无法与他们的库一起使用,在这种情况下,可能最好还是使用HttpWebRequest创建自己的库。希望有帮助

要创建正确的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; }
}
为了能够使用
数据
属性,您需要选择其他一些JSON序列化程序。例如或(我将在本例中使用它)

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

    }
}

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

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

安装NuGet,然后使用requires命名修饰来修饰Customer类,以告诉Json序列化程序如何序列化Customer类字段:

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\'u descr\”:“Appliances\”,“b\u name\u first\”:“Marisol\”,“b\u name\u last\”:“Testcase\”}}“

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

使用系统;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
CurrentUTCDateTime yourObject=新建CurrentUTCDateTime();
JObject json=JObject.Parse(JsonConvert.SerializeObject(yourObject));

?对于初学者,请确保您的json字符串有效。您可以实际使用此网站将json字符串转换为C#类。首先,当您序列化
应用程序时,请查看此链接,您将获得如下json:
{“ProductDescription”:“gors_descr Appliances”,“Fname”:“b_name_First Marisol”,…}
。它看起来不像您想要的JSON。+1用于使用Newonsoft解决方案-请记住,要使用它,您需要使用NuGet软件包管理器安装其软件包,并且(当然)在“使用”部分包含Newtonsoft.JSON。谢谢你的反馈。我添加了packages.config。“使用”已经存在了!:)欢迎来到StackOverflow!请提供一些解释,为什么您认为您提出的解决方案可能有助于OP。我的解决方案将有助于希望从对象获取json作为JSONObject的人欢迎使用堆栈溢出。对于堆栈溢出,不鼓励使用仅代码的答案,因为它们没有解释堆栈溢出是如何解决问题的。请编辑您的答案,以解释此代码的作用以及它如何回答问题,以便对OP以及具有类似问题的其他用户有用。
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 });