//将JSON字符串‘{"name": "jack","age":"19","address": "beijing"}‘的所有key值首字母改为大写:‘{"Name":"jack","Age":"19","Address":"beijing"}‘
function toUpperCasel(json){
//将json字符串解析为Object对象
let obj = JSON.parse(json)
let newObj = {}
let tempKey, currentValue, upperCase, arr
//遍历obj对象的属性——key值
for(let i in obj){
currentValue = obj[i]
//字符串切割为数组
arr = i.split(‘‘)
//数组的第一个元素转换为“大写字母”
upperCase = arr[0].toUpperCase()
//替换第一个元素
arr.splice(0, 1, upperCase)
//将数组拼接为字符串——新key值(首字母大写)
tempKey = arr.join(‘‘)
//将新key值赋给newObj
newObj[tempKey] = currentValue
}
//将newObj转换为json字符串,并返回
let newJson = JSON.stringify(newObj)
return newJson
}
let json = ‘{"name": "jack","age":"19","address": "beijing"}‘
console.log(toUpperCasel(json))