【问题标题】:How to add \r\n\ in json Using c#如何使用 c# 在 json 中添加 \r\n\
【发布时间】:2016-06-03 06:42:54
【问题描述】:

我确实有一个硬编码 json,我必须使用 post 在 http 请求中发送它, 这是json

{
  "RequestHeader": {
    "UserName": " ",
    "Password": " "
  },
  "RequestBody": {
    "ChannelType": 1,
    "BillAccountNumber": "1075-001",
    "BillAccountType": null,
    "PaymentAmount": 15.05,
    "FeeAmount": 3.50,
    "ABA": "111993776",
    "BankAccount": "1234567899",
    "EmailAddress": "jonah@doe.org",
    "AccountHolder": "JonahDoe",
    "WaiveFee": false,
    "Recurring": false,
    "CustomData": null
  }
}

当我通过这个时,我得到了正确的回应

但是当我使用另一个时,它并没有给出我需要的响应,唯一的区别是我硬编码的第一个有 \r\n 而下面的第二个没有。

{
  "RequestHeader": {
    "UserName": " ",
    "Password": " "
    },
  "RequestBody": {
  "ChannelType": 1,
  "BillAccountNumber": "1075-001",
  "BillAccountType": null,
  "PaymentAmount": 15.05,
  "FeeAmount": 3.5,
  "ABA": "111993776",
  "BankAccount": "1234567899",
  "EmailAddress": "jonah@doe.org",
  "AccountHolder": "Jonah Doe",
  "WaiveFee": false,
  "Recurring": false,
  "CustomData": null
 }
}

谁能告诉我是什么问题。

代码是:

class Program
{
    static void Main(string[] args)
    {
        var RequestBody = new RequestBody
        {
            ChannelType = 1,
            BillAccountNumber = "1075-001",
            BillAccountType = null,
            PaymentAmount = 15.05,
            FeeAmount = 3.50,
            ABA = "111993776",
            BankAccount = "1234567899",
            EmailAddress = "jonah@doe.org",
            AccountHolder = "Jonah Doe",
            WaiveFee = false,
            Recurring = false,
            CustomData = null
        };
        var RequestHeader = new RequestHeader
        {
            UserName = "myUname",
            Password = "MyPass"
        };
        var Request = new Request
        {
            RequestBody = RequestBody,
            RequestHeader = RequestHeader,

        };
        var ApiCredentials = new ApiCredentials
        {
            Request = Request
        };
        var httpWReq = (HttpWebRequest)WebRequest.Create("https://gw1.cwplab.com/api/Gateway/AuthorizeCheck");
        httpWReq.ContentType = "application/json";
        //httpWReq.Credentials = new NetworkCredential(" ", " ");
        string data = "{\r\n\"RequestHeader\":{\r\n\"UserName\":\" \",\r\n\"Password\":\" \"\r\n},\r\n\"RequestBody\":{\r\n    \"ChannelType\":1,\r\n    \"BillAccountNumber\":\"1075-001\",\r\n    \"BillAccountType\":null,\r\n    \"PaymentAmount\":15.05,\r\n    \"FeeAmount\":3.50,\r\n    \"ABA\":\"111993776\",\r\n    \"BankAccount\":\"1234567899\",\r\n    \"EmailAddress\":\"jonah@doe.org\",\r\n    \"AccountHolder\":\"Jonah Doe\",\r\n    \"WaiveFee\":false,\r\n    \"Recurring\":false,\r\n    \"CustomData\":null\r\n}\r\n}\r\n";//Request.ToJSON();
        string data1 = Newtonsoft.Json.JsonConvert.SerializeObject(Request);
        httpWReq.ContentLength = data.Length;
        httpWReq.Method = "POST";
        using (StreamWriter stream = new StreamWriter(httpWReq.GetRequestStream()))
        {
            stream.Write(data);
            //stream.Flush();
            //stream.Close();
        };
        dynamic httpResponse = (HttpWebResponse)httpWReq.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 两个json都是一样的。
  • @AmitKumarGhosh 不,它们不是,不同的值

标签: c# json http post


【解决方案1】:

JSON 有所不同。我怀疑你的服务器不接受第二个 json(你说你得到了错误的响应)。 \r\n 不是强制性的,仅供读者阅读。

    "FeeAmount": 3.5,
    "FeeAmount": 3.50,

    "AccountHolder": "Jonah Doe",
    "AccountHolder": "JonahDoe",

【讨论】:

  • 第二个只是string。这有什么不同吗
  • 取决于服务器对数据的处理方式。例如,如果服务器找不到名称“JonahDoe”,它可能会失败。
  • 感谢 RvdK 和 Amit!我已经找到了,但无论如何感谢您的时间和工作。
猜你喜欢
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 2019-12-30
  • 1970-01-01
相关资源
最近更新 更多