[英]JsonConvert.SerializeObject Turning Double Into String
I'm passing in an object to JsonConvert.SerializeObject, in hopes of turning said object into a json string for use with an API....before serialization this is what the object looks like: 我正在将一个对象传递给JsonConvert.SerializeObject,希望将所述对象转换为JSON字符串以用于API。...在序列化之前,该对象如下所示:
var hotelBedsSearchRequest = new HotelBedsSearchRequest()
{
stay = new Stay
{
checkIn = "2016-06-08",
checkOut = "2016-06-10",
shiftDays = "2"
},
occupancies = new Occupancy[]
{
new Occupancy
{
rooms = 1,
adults = 2,
children = 1,
paxes = new Pax[]
{
new Pax
{
type = "AD",
age = 30
},
new Pax
{
type = "AD",
age = 30
},
new Pax
{
type = "CH",
age = 8
}
}
}
},
geolocation = new Geolocation
{
longitude = 2.646633999999949,
latitude = 39.57119,
radius = 20,
unit = "km"
}
};
All fields serialize fine except for latitude and longitude which when I serialize to a JSON string and view as json in visual studio looks like this: 除了纬度和经度,所有字段都可以序列化,当我将其序列化为JSON字符串并在Visual Studio中以json格式查看时,它们看起来像这样:
Why is the lat and long coming out as a string? 为什么lat和long会以字符串形式出现? It is specified as a double type in the class schema...i've tried both most common serializing techniques (Newtonsoft and built in) both do the same thing...when I run the API i get a 400 error bad request and I'm thinking it's this misrepresentation of the lat and long data type...not sure though and any help would be amazing! 在类模式中将其指定为双精度类型...我尝试了两种最常见的序列化技术(Newtonsoft和内置)都做同样的事情...当我运行API时,我收到一个400错误的错误请求,并且我在想这是经纬度和长数据类型的这种错误表示...虽然不确定,但任何帮助都将是惊人的!
Here is Geolocation class: 这是地理位置类:
public class Geolocation
{
public double longitude { get; set; }
public double latitude { get; set; }
public int radius { get; set; }
public string unit { get; set; }
}
And here is the serialized JSON output as text: 这是序列化的JSON输出为文本的形式:
{"stay":{"checkIn":"2016-06-08","checkOut":"2016-06-10","shiftDays":"2"},"occupancies":[{"rooms":1,"adults":2,"children":1,"paxes":[{"type":"AD","age":30},{"type":"AD","age":30},{"type":"CH","age":8}]}],"geolocation":{"longitude":2.6466341018676758,"latitude":39.571189880371094,"radius":20,"unit":"km"},"adults":0}
And here is the serialization and API call: 这是序列化和API调用:
var json = new JavaScriptSerializer().Serialize(request);
var response = client.UploadString(endpoint, "POST", json);
var ans = DeserializeResult(response);
Fixed, it had nothing to do with passing in the wrong structure, the request class was simply built wrong...sorry. 已修复,与传递错误的结构无关,请求类仅被构建为错误的...抱歉。 I didn't realize that even if you don't pass in anything for a field in the schema it will still show when you new up the class 我没有意识到,即使您不为架构中的字段传递任何内容,它也仍然会在您上课时显示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.