Javascript 将 Json 响应作为字符串和整数发送...
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6858293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Json response as String and Integers...
提问by John Cooper
{
"UpdateRequest": {
"SAASAS": {
"SPO2": "99.00000",
"VitalGroupID": "1219",
"Temperature": "36.6666666666667",
},
"Modified": 1,
"ID": 25465
}
}
How can i send VitalGroupID and Temperature as Integer instead of String.... This is the request that get's formed after i hit submit button.
我如何将 VitalGroupID 和温度作为整数而不是字符串发送......这是我点击提交按钮后形成的请求。
采纳答案by Charles Ma
Strictly speaking, json is untyped, so you can't send it as an integer, it has to be a string. javascript objects are a little less strict, so what you have their will evaluate to a javascript object, but no strict json parser will be able to understand it.
严格来说,json 是无类型的,所以你不能把它作为整数发送,它必须是一个字符串。javascript 对象不那么严格,所以你有他们将评估为一个 javascript 对象,但没有严格的 json 解析器能够理解它。
The best you can do is to convert fields that you know are numbers on the client side using parseInt.
您能做的最好的事情是使用 parseInt 在客户端转换您知道是数字的字段。
e.g. sonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"] = parseInt(jsonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"], 10);
例如 sonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"] = parseInt(jsonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"], 10);
回答by T.J. Crowder
You'll need to show the code that's creating the request when you click the button. Basically, if the object you're serializing contains numbers rather than strings, the resulting JSON will have numbers instead of strings. So the problem is that the object you're serializing has strings instead.
单击按钮时,您需要显示创建请求的代码。基本上,如果您序列化的对象包含数字而不是字符串,则生成的 JSON 将包含数字而不是字符串。所以问题是你正在序列化的对象有字符串。
But for instance, if you're getting these values from HTML input fields or similar, e.g.:
但是,例如,如果您从 HTML 输入字段或类似字段中获取这些值,例如:
UpdateRequest.SAASAS.VitalGroupID = someInputElement.value;
...value
is alwaysa string. You'll need to parse it:
......value
是永远的字符串。你需要解析它:
UpdateRequest.SAASAS.VitalGroupID = parseInt(someInputElement.value, 10);
Note that it's best to use parseInt
and to give it the radix (the number base, usually 10), else you run into issues with numbers written as "08" and similar.
请注意,最好使用parseInt
并为其指定基数(以数字为基数,通常为 10),否则您会遇到写为“08”等类似数字的问题。
回答by Chris Buck
JSON supports numbers as values. So:
JSON 支持数字作为值。所以:
var r = {
"UpdateRequest": {
"SAASAS": {
"SPO2": "99.00000",
"VitalGroupID": "1219",
"Temperature": "36.6666666666667",
},
"Modified": 1,
"ID": 25465
}
}
r.UpdateRequest.SAASAS.VitalGroupID = +r.UpdateRequest.SAASAS.VitalGroupID
回答by Subdigger
new Number(jsonObject.UpdateRequest.SAASAS.VitalGroupID);
回答by Ashwin Krishnamurthy
If you want to handle it in the Client side (As you have mentioned JSON,JAvascript). You can do the following after u parse the JSON object. parseInt(UpdateRequest.SAASAS.VitalGroupID,10)
or UpdateRequest.SAASAS.VitalGroupID*1
Adopt the same for the "temparature" variable too.
如果你想在客户端处理它(正如你提到的 JSON,JAvascript)。解析 JSON 对象后,您可以执行以下操作。parseInt(UpdateRequest.SAASAS.VitalGroupID,10)
或UpdateRequest.SAASAS.VitalGroupID*1
对“温度”变量也采用相同的方法。