【问题标题】:Converting an object into Json in C# and sending it over POST results in a corrupt object?在 C# 中将对象转换为 Json 并通过 POST 发送会导致对象损坏?
【发布时间】:2014-09-17 14:07:51
【问题描述】:

在 C# 上,我正在打印发送到控制台的 JSON 化字符串,它的内容为

 { "message" : "done", "numSlides" : 1, "slides" : [{ "num" : 1, "key" : "530d8aa855df0c2d269a5a5853a47a469c
52c9d83a2d71d9/1slide/Slide1_v8.PNG" }], "bucket" : "xx.xxxxxxxxxx", "error"
: null, "wedge" : false, "tenant" : null, "name" : null }

然后我这样做是为了将它转换为字节数组并发送它

WebRequest request = WebRequest.Create(Program.api +"/"+ route);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

//Get the request stream
Stream dataStream = request.GetRequestStream();
byte[] byteArray = Encoding.UTF8.GetBytes(myString);
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

在 node.js 方面,我在 console.logging res.body 时得到这个:

{ '{ "message" : "done", "numSlides" : 1, "slides" : ': { '{ "num" : 1, "key" : "530d8aa855df0c2d269a5a5853a47a469c52c9d83a2d71d9/1slide/Slide1_v8.PNG" }], "bucket" : "xx.xxxxxxxxxx", "error" : null, "wedge" : false, "tenant" : null, "name" : null ': '' } }

这看起来不像是有效的 JSON。发生了什么?如何发送和接收正确的数据?

【问题讨论】:

    标签: c# javascript json node.js bytearray


    【解决方案1】:

    在测试我正在编写的节点服务器时,我遇到了类似的问题。问题最终出在请求的内容类型上。我相信这也可能是您的问题。

    我在想你想要的内容类型是“application/json”。

    更多信息请见This Post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2017-06-16
      相关资源
      最近更新 更多