如何在JSON.stringify中保留数字字段

时间:2021-09-23 06:52:29

I've got code that takes form fields and converts them into a JSON string suitable for passing to a REST API (the marathon REST API specifically). The problem is that JSON.stringify double quotes all the form field values, even those that are numeric, which causes the ajax call to fail. Here's the string my code is producing:

我有一些代码,它接受表单字段并将它们转换为适合传递给REST API的JSON字符串(特别是马拉松REST API)。问题是JSON.stringify双引号所有表单字段值,即使是那些数字,这会导致ajax调用失败。这是我的代码生成的字符串:

{"id":"basic-1","cpus":"0","mem":"32","instances":"1","cmd":"while [ true ] ; do echo 'Hello Marathon' ; sleep 5 ; done"}

The mem, cpus, and instances values should not be double quoted because they're numeric.

mem,cpus和instances值不应该双引号,因为它们是数字的。

data: JSON.stringify($('form').MytoJson());

MytoJson is a function that serializes the form fields. That's working fine. The problem is that JSON.stringify is putting the quotes around the keys and all the values, including the numeric values, which is causing the ajax call to fail.

MytoJson是一个序列化表单字段的函数。这工作正常。问题是JSON.stringify在键和所有值(包括数值)之间放置引号,导致ajax调用失败。

The JSON.stringify function takes two arguments. The first argument is a replacer function, which can be a function or an array. Consider this replacer function:

JSON.stringify函数有两个参数。第一个参数是replacer函数,可以是函数或数组。考虑这个替换器功能:

function replacer(key, value) {
    if (typeof value === "string") {
        return undefined;
    }
    return value;
}
JSON.stringify($('form').MytoJson(), replacer);

I want to modify this replacer function to not quote numeric values. I'm wondering if I have use the HTML5 form attributes to differentiate between string and numeric form fields? Consider this form field:

我想修改此replacer函数不引用数值。我想知道我是否使用HTML5表单属性来区分字符串和数字表单字段?考虑这个表单字段:

<input id="cpus" type="number" name="cpus" value="0.1" />

How can I use the fact that this field has the attribute type="number" in the replacer function? I want to modify the replacer function to not quote values when the form field is of type number.

如何在replacer函数中使用此字段具有属性type =“number”的事实?我想修改replacer函数,以便在表单字段的类型为number时不引用值。

3 个解决方案

#1


1  

In order to turn a stringifyed string back into data, you would use JSON.parse(stringifyedString);

为了将字符串化的字符串转换回数据,您将使用JSON.parse(stringifyedString);

Simply removing the quotes from a string won't convert "5" into a Number, for example. It'll just make a string with the character 5.

例如,简单地从字符串中删除引号不会将“5”转换为数字。它只会用字符5创建一个字符串。

#2


1  

I'm not 100%, but I imagine the issue is being caused by the the code that reads the form fields' values - namely it is reading the values as strings (which they are).

我不是100%,但我想这个问题是由读取表单字段值的代码引起的 - 即它将值读取为字符串(它们是)。

What you need to do is cast any numeric values to an actual JS Number. Try the following replacer function:

您需要做的是将任何数值转换为实际的JS编号。尝试以下替换器功能:

function replacer( key, value ) {

    var ret = ( value === value * 1 )
            ? value * 1
            : value;

    return ret;

}

This should check if each value is numeric, and if so convert it to a Number

这应检查每个值是否为数字,如果是,则将其转换为数字

#3


0  

Here's the solution. I've modified the replacer function to use the HTML5 form field type to discriminate between form fields that should be quoted or not quoted.

这是解决方案。我已经修改了replacer函数以使用HTML5表单字段类型来区分应该引用或不引用的表单字段。

function replacer(key, value) {
    //console.log(typeof($(eval('\'#' + key + '\'')).attr('type')));
    if ($(eval('\'#' + key + '\'')).attr('type') == null) {
        return value;
    }
    return parseFloat(value);
} 

Then in my ajax code:

然后在我的ajax代码中:

 data: JSON.stringify($('form').MytoJson(), replacer);

It seems that the 'type' attribute for standard 'text' input fields is undefined, but it is defined when the type is 'number'. I'm using that.

似乎标准“文本”输入字段的“类型”属性未定义,但在类型为“数字”时定义。我正在使用它。

If the type attribute is undefined I just return the value which gets included and double quoted in the JSON string. But if the type attribute is defined, then I cast the value to a float. It gets included in the JSON string un-quoted. This is a working solution for now but I'm sure I'll have to re-visit this in the near future.

如果type属性未定义,我只返回包含的值并在JSON字符串中双引号。但是如果定义了type属性,那么我将值转换为float。它包含在未引用的JSON字符串中。现在这是一个有效的解决方案,但我相信我不得不在不久的将来重新访问它。

#1


1  

In order to turn a stringifyed string back into data, you would use JSON.parse(stringifyedString);

为了将字符串化的字符串转换回数据,您将使用JSON.parse(stringifyedString);

Simply removing the quotes from a string won't convert "5" into a Number, for example. It'll just make a string with the character 5.

例如,简单地从字符串中删除引号不会将“5”转换为数字。它只会用字符5创建一个字符串。

#2


1  

I'm not 100%, but I imagine the issue is being caused by the the code that reads the form fields' values - namely it is reading the values as strings (which they are).

我不是100%,但我想这个问题是由读取表单字段值的代码引起的 - 即它将值读取为字符串(它们是)。

What you need to do is cast any numeric values to an actual JS Number. Try the following replacer function:

您需要做的是将任何数值转换为实际的JS编号。尝试以下替换器功能:

function replacer( key, value ) {

    var ret = ( value === value * 1 )
            ? value * 1
            : value;

    return ret;

}

This should check if each value is numeric, and if so convert it to a Number

这应检查每个值是否为数字,如果是,则将其转换为数字

#3


0  

Here's the solution. I've modified the replacer function to use the HTML5 form field type to discriminate between form fields that should be quoted or not quoted.

这是解决方案。我已经修改了replacer函数以使用HTML5表单字段类型来区分应该引用或不引用的表单字段。

function replacer(key, value) {
    //console.log(typeof($(eval('\'#' + key + '\'')).attr('type')));
    if ($(eval('\'#' + key + '\'')).attr('type') == null) {
        return value;
    }
    return parseFloat(value);
} 

Then in my ajax code:

然后在我的ajax代码中:

 data: JSON.stringify($('form').MytoJson(), replacer);

It seems that the 'type' attribute for standard 'text' input fields is undefined, but it is defined when the type is 'number'. I'm using that.

似乎标准“文本”输入字段的“类型”属性未定义,但在类型为“数字”时定义。我正在使用它。

If the type attribute is undefined I just return the value which gets included and double quoted in the JSON string. But if the type attribute is defined, then I cast the value to a float. It gets included in the JSON string un-quoted. This is a working solution for now but I'm sure I'll have to re-visit this in the near future.

如果type属性未定义,我只返回包含的值并在JSON字符串中双引号。但是如果定义了type属性,那么我将值转换为float。它包含在未引用的JSON字符串中。现在这是一个有效的解决方案,但我相信我不得不在不久的将来重新访问它。