I have an array of location points (latitude, longitude, and created_at) that need to be sent up in bulk. However, when I use JsonConvert.SerializeObject()
it returns a string which can't be parsed on the server endpoint.
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);
The result looks like the following:
{"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}]"}
The array of coordinates comes across as one big string, so it looks like :"[{\\"created_at\\":
when it should be :[{"created_at":
.
So, the server is expecting something like this:
{"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) {}
}
Is there a way to make the key value pairs a <string, []>
? I haven't found an example that DOESN'T use the <string, string>
pair.
Is there another work-around for HttpClient for arrays of json data?
Construct the model and then serialize the entire thing before posting
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.