如何使用小写密钥解析JSON到对象 [英] How to parse JSON to object with lower case key
本文介绍了如何使用小写密钥解析JSON到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些JSON数据,但所有键都是UPPER大小写。如何解析它们并将键转换为更低?我正在使用jQuery。
I have some JSON data but all the keys are in UPPER case. How to parse them and convert the keys to lower? I am using jQuery.
例如:
JSON数据:
{"ID":1234, "CONTENT":"HELLO"}
期望输出:
{id:1234, content:"HELLO"}
推荐答案
这个怎么样:
json.replace(/"([^"]+)":/g,function($0,$1){return ('"'+$1.toLowerCase()+'":');}));
正则表达式捕获密钥名称$ 1并将其转换小写。
The regex captures the key name $1 and converts it to lower case.
现场演示: http:// jsfiddle.net/bHz7x/1/
为了解决@FabrícioMatté的评论,另一个只匹配单词字符的演示: http://jsfiddle.net/bHz7x/4/
[edit] To address @FabrícioMatté's comment, another demo that only matches word characters: http://jsfiddle.net/bHz7x/4/
这篇关于如何使用小写密钥解析JSON到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文