如何在Javascript中将JSON字符串值转换为小写?
我有一些JSON数据,其中包含字符串和int值的混合。如何将所有字符串值转换为小写 例如:如何在Javascript中将JSON字符串值转换为小写?,javascript,json,Javascript,Json,我有一些JSON数据,其中包含字符串和int值的混合。如何将所有字符串值转换为小写 例如: { id: 0, name: "SAMPLe", forms: { formId: 0, id: 0, text: "Sample Text" }} 期望输出: { id: 0, name: "sample", forms: { formId: 0, id: 0, text: "sample text" }} 您需要在对象中递归: 您需要遍历对象 函数lowerStringsobj{ 对于obj中的
{ id: 0, name: "SAMPLe", forms: { formId: 0, id: 0, text: "Sample Text" }}
期望输出:
{ id: 0, name: "sample", forms: { formId: 0, id: 0, text: "sample text" }}
您需要在对象中递归:
您需要遍历对象 函数lowerStringsobj{ 对于obj中的let attr{ 如果对象的类型[attr]=“string”{ obj[attr]=obj[attr].toLowerCase; }否则,如果对象的类型[attr]=“对象”{ lowerStringsobj[attr]; } } } var obj={ id:0, 名称:样本, 表单:{formId:0,id:0,text:Sample text} }; lowerStringsobj; console.logobj 您可以使用JSON.stringify、JSON.parse和typeof 风险值数据={ id:0, 名称:样本, 表格:{ formId:0, id:0, 文本:示例文本 } }; var res=JSON.parseJSON.stringifydata,functa,b{ 返回类型b==字符串?b.toLowerCase:b };
logres在我的例子中,我只想将属性转换为小写,密码、数字等值保持不变 我的ajax回调结果集是:
result = [{FULL_NAME:xxx}, {}, {}....... {}]
我希望它是:
result = [{full_name:xxx}, {}, {}....... {}]
这是我的工作代码:
我使用Mazilla浏览器原生api获取,而不是旧的ajax或jquery$get等
// ********** must use self = this **************
// this reference vue-app. must pass it to self, then pass into callback function (success call back)
var self = this;
fetch(getUser_url).then(function (response) {
return response.json();
}).then(function (result) {
//------------------------ properties to lowercase ----------------------
// result is upper case, must convert all properties to lowercase,
// however, the value like password or number MUST remain no change.
// result = [{}, {}, {}....... {}]
var result_lower_properties= [];
var arrayLength = result.length;
for (var i = 0; i < arrayLength; i++) {
var obj = result[i];
var obj_lower_properties = {};
for (var prop in obj) {
//console.log(prop.toLowerCase())
//console.log(obj[prop])
obj_lower_properties[prop.toLowerCase()] = obj[prop]
}// for
result_lower_properties.push(obj_lower_properties)
}// for
//---------- ENd -------------- properties to lowercase ----------------------
// must use self.user, do not use this.user,
// because here, this's scope is just the function (result).
// we need this reference to vue-app,
self.user = result_lower_properties; // [{}, {}, {}]
}); // fetch(){}
这一个有错误,不工作!你必须修复这个错误。
// ********** must use self = this **************
// this reference vue-app. must pass it to self, then pass into callback function (success call back)
var self = this;
fetch(getUser_url).then(function (response) {
return response.json();
}).then(function (result) {
//------------------------ properties to lowercase ----------------------
// result is upper case, must convert all properties to lowercase,
// however, the value like password or number MUST remain no change.
// result = [{}, {}, {}....... {}]
var result_lower_properties= [];
var arrayLength = result.length;
for (var i = 0; i < arrayLength; i++) {
var obj = result[i];
var obj_lower_properties = {};
for (var prop in obj) {
//console.log(prop.toLowerCase())
//console.log(obj[prop])
obj_lower_properties[prop.toLowerCase()] = obj[prop]
}// for
result_lower_properties.push(obj_lower_properties)
}// for
//---------- ENd -------------- properties to lowercase ----------------------
// must use self.user, do not use this.user,
// because here, this's scope is just the function (result).
// we need this reference to vue-app,
self.user = result_lower_properties; // [{}, {}, {}]
}); // fetch(){}