【问题标题】:Post With C# WebClient Multiple Json Object使用 C# WebClient 多个 Json 对象发布
【发布时间】:2017-12-04 17:07:03
【问题描述】:

我想用这个 Json Schema 发送带有 C# WebClient 的 Post 请求:

 [
  {
    "id": "00000000-0000-0000-0000-000000000000",
    "points": [
      {
        "timestamp": "2017-12-04T16:07:44.562Z",
        "value": 0
      }
    ]
  }
]

我试过这个:

public class RequestData
{
    public string id {get; set; }
    public points points { get; set; }
}

public class points
{
     public DateTime timestamp { get; set; }
     public float value { get; set; }
}

我的程序:

Random number = new Random();
var req = new RequestData();
req.id = "0e13d9c-571c-44f4-b796-7c40c0e20a1d";
req.points = new points { timestamp = DateTime.UtcNow, value = 
number.Next(100, 99999) };

JsonSerializerSettings settings = new JsonSerializerSettings();
var data = JsonConvert.SerializeObject(req);

WebClient client = new WebClient(); 

client.Headers.Add(HttpRequestHeader.Authorization, 
AquaAtuhorization.accessToken);

client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); 
client.UploadString ("http://localhost:8080/api/v1/data/writeNumericValues",  
data  ); 

而且我总是得到 Http 415(不支持的媒体类型)。

如何将我的 C# 对象格式化为 restApi 接受的格式。

【问题讨论】:

    标签: c# json webclient


    【解决方案1】:

    查看 JSON,方括号[ ] 表示某事物是一个数组。在这种情况下,RequestDatapoints 都必须是一个数组,请参见下面的示例:

    public class RequestData
    {
        public string id { get; set; }
        public List<points> points { get; set; }  // I use list, could be an array
    }
    
    public class points
    {
        public DateTime timestamp { get; set; }
        public float value { get; set; }
    }
    

    然后像这样构造你的req 对象:

    var req = new List<RequestData> // Again I use list, could be an array
    {
        new RequestData
        {
            id = "0e740d9c-571c-44f4-b796-7c40c0e20a1d",
            points = new List<points> // Defined as a list, even though it has one entry
            {
                new points
                {
                    timestamp = DateTime.UtcNow,
                    value = number.Next(100, 99999)
                }
            }
        }
    };
    

    然后正常序列化,结果如下:

    [
      {
        "id":"0e740d9c-571c-44f4-b796-7c40c0e20a1d",
        "points":[
          {
            "timestamp":"2017-12-04T17:12:25.8957648Z",
            "value":59522.0
          }
        ]
      }
    ]
    

    【讨论】:

      【解决方案2】:

      您的 Json 类需要是这样的,请参阅 http://json2csharp.com/ 或使用来自 VS 的 paste as JSON https://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/

      public class Point
      {
        public DateTime timestamp { get; set; }
        public int value { get; set; }
      }
      
      public class RootObject
      {
        public string id { get; set; }
        public List<Point> points { get; set; }
      }`
      

      【讨论】:

      • 谢谢 Saj,但我仍然遇到同样的 Http 415 错误
      • 使用 fiddler 并检查它是否设置了内容类型标头,同时检查您的 API 是否需要字符串
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多