13
{
    "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.

John Cooper
  • 7,343
  • 31
  • 80
  • 100
  • Isn't JSON always sent as String? I think you must cast the type manually – das_weezul Jul 28 '11 at 11:53
  • 3
    @das: JSON is a textual notation, just like JavaScript source code is a textual notation. He's talking about `"VitalGroupID": "1219"` (which defines a property with the *string* value `"1219"`) as opposed to `"VitalGroupID": 1219` (which defines the property with the *numeric* value `1219`). – T.J. Crowder Jul 28 '11 at 12:31

5 Answers5

10

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.

But for instance, if you're getting these values from HTML input fields or similar, e.g.:

UpdateRequest.SAASAS.VitalGroupID = someInputElement.value;

...value is always a string. You'll need to parse it:

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
7

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.

The best you can do is to convert fields that you know are numbers on the client side using parseInt.

e.g. sonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"] = parseInt(jsonObj["UpdateRequest"]["SAASAS"]["VitalGroupID"], 10);

Charles Ma
  • 47,141
  • 22
  • 87
  • 101
  • 2
    Looks like ECMA-404 standard does make a difference between string and number in JSON: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf – Timur Yusupov Aug 17 '15 at 08:08
1

JSON supports numbers as values. So:

var r = {
    "UpdateRequest": {
        "SAASAS": {
            "SPO2": "99.00000",
            "VitalGroupID": "1219",
            "Temperature": "36.6666666666667",            
        },
        "Modified": 1,
        "ID": 25465
    }
}
r.UpdateRequest.SAASAS.VitalGroupID = +r.UpdateRequest.SAASAS.VitalGroupID
Chris Buck
  • 745
  • 5
  • 15
0
new Number(jsonObject.UpdateRequest.SAASAS.VitalGroupID);
Subdigger
  • 2,166
  • 3
  • 20
  • 42
0

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.

Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49