gpt4 book ai didi

c# - 使用 HTTPClient PostAsync 发送数组

转载 作者:太空狗 更新时间:2023-10-30 01:13:33 24 4
gpt4 key购买 nike

我有一组位置点(纬度、经度和 created_at)需要批量发送。但是,当我使用 JsonConvert.SerializeObject()它返回一个无法在服务器端点上解析的字符串。

var location_content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("access_token", $"{Settings.AuthToken}"),
new KeyValuePair<string, string>("coordinates", JsonConvert.SerializeObject(locations))
});

var response = await client.PostAsync(users_url + bulk_locations_url, location_content);

结果如下所示:

{"access_token":"XX","coordinates":"[{\"created_at\":\"2018-03-27T21:36:15.308265\",\"latitude\":XX,\"longitude\":XX},{\"created_at\":\"2018-03-27T22:16:15.894579\",\"latitude\":XX,\"longitude\":XX}]"}

坐标数组作为一个大字符串出现,因此看起来像 :"[{\"created_at\":什么时候应该是:[{"created_at": .

所以,服务器期待这样的事情:

{"access_token":"XX","coordinates":[{\"created_at\":\"2018-03-27T21:36:15.308265\",\"latitude\":XX,\"longitude\":XX},{\"created_at\":\"2018-03-27T22:16:15.894579\",\"latitude\":XX,\"longitude\":XX}]}

Location.cs

public class Location
{
public DateTime created_at { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }

[PrimaryKey, AutoIncrement, JsonIgnore]
public int id { get; set; }

[JsonIgnore]
public bool uploaded { get; set; }

public Location()
{

}

public Location(double lat, double lng)
{
latitude = lat;
longitude = lng;

uploaded = false;
created_at = DateTime.UtcNow;

Settings.Latitude = latitude;
Settings.Longitude = longitude;
}

public Location(Position position) : this(position.Latitude, position.Longitude) {}
}

有没有办法让键值对成为<string, []> ?我还没有找到一个不使用 <string, string> 的例子一对。

对于 json 数据数组,HttpClient 是否有另一种解决方法?

最佳答案

构建模型然后在发布前序列化整个东西

var model = new{
access_token = Settings.AuthToken,
coordinates = locations
};
var json = JsonConvert.SerializeObject(model);
var location_content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync(users_url + bulk_locations_url, location_content);

关于c# - 使用 HTTPClient PostAsync 发送数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49567926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com