javascript JSON.stringify,改变key的大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5480570/
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
JSON.stringify, change the case of the key
提问by ScottE
I'm consuming a web service that returns json, and storing the json in a local variable. The json represents a simple business object such as:
我正在使用一个返回 json 的 Web 服务,并将 json 存储在一个本地变量中。json 代表一个简单的业务对象,例如:
var entry = {
"FirstName": "John",
"LastName": "Doe",
....
};
The casing is like that because it matches up with the property names from the .net class, as per our naming convention.
大小写是这样的,因为根据我们的命名约定,它与 .net 类的属性名称相匹配。
When a change a few of these properties and pass back the json, the web service now expects camel case (again, as per our naming convention for method parameters) instead of the pascal case initially returned.
当更改其中一些属性并传回 json 时,Web 服务现在需要驼峰式大小写(同样,根据我们的方法参数命名约定),而不是最初返回的 pascal 大小写。
var entry = {
"firstName": "John",
"lastName": "Doe",
....
};
This of course doesn't work.
这当然行不通。
I'm using JSON.stringify
to send the json back to the web service as a string, and I was looking to see if there was an easy way to change the key to camel case. However, it looks like I can only use the replacer param to work with the value.
我正在使用JSON.stringify
将 json 作为字符串发送回 Web 服务,我想看看是否有一种简单的方法可以将密钥更改为驼峰式大小写。但是,看起来我只能使用替换参数来处理该值。
I could change the serialization of the class, but lets pretend that's not an option. Any ideas?
我可以更改类的序列化,但让我们假设这不是一个选项。有任何想法吗?
Thanks.
谢谢。
回答by Mike Samuel
You can use a JSON replacer to switch keys before writing.
您可以使用 JSON 替换器在写入之前切换键。
JSON.stringify(myVal, function (key, value) {
if (value && typeof value === 'object') {
var replacement = {};
for (var k in value) {
if (Object.hasOwnProperty.call(value, k)) {
replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
}
}
return replacement;
}
return value;
});
For the opposite, you can use a JSON reviver.
相反,您可以使用 JSON reviver。
JSON.parse(text, function (key, value) {
if (value && typeof value === 'object')
for (var k in value) {
if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
delete value[k];
}
}
return value;
});
The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.
第二个可选参数是一个函数,该函数在解析过程中创建的每个值或将要写入的每个值都被调用。这些实现只是简单地迭代键并小写任何具有大写字母的第一个字母。
There is documentation for replacers and revivers at http://json.org/js.html:
http://json.org/js.html 上有替换器和恢复器的文档:
The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
可选的 reviver 参数是一个函数,它将在最终结果的每个级别为每个键和值调用。每个值都将替换为 reviver 函数的结果。这可用于将通用对象转换为伪类的实例,或将日期字符串转换为日期对象。
stringifier 方法可以采用可选的替换函数。它将在结构中的每个值的 toJSON 方法(如果有)之后调用。它将每个键和值作为参数传递,这将绑定到持有键的对象。返回的值将被字符串化。