js中json对象key值首字母大写化

function toUpperCase(jsonObj) {
    if(typeof(jsonObj)=='object'){
         for (var key in jsonObj){
        jsonObj[key.substring(0,1).toUpperCase()+key.substring(1)] = jsonObj[key];  
        delete(jsonObj[key]);  
    }  
    return jsonObj;  

    }
    return data;
}
var res;  
var _data = {"myKey":"myValue"};

res = toUpperCase(_data);
console.log(res);//{MyKey: "myValue"}
console.log(JSON.stringify(res));//{"MyKey":"myValue"}

猜你喜欢

转载自my.oschina.net/hrw/blog/1812000
今日推荐