如何在javascript中将所有JSON键转换为小写?

时间:2016-03-29 09:37:12

标签: javascript jquery angularjs json

实际上,我的要求是将整个JSON(仅键)转换为小写。我试过它只转换那个JSON的第一个密钥而且它没有转换整个所有密钥。请查看小提琴链接或请提供任何其他方法来执行此操作。

...谢谢

var obj = {
  "Collections": {
    "conTainer": {
      "rowSet": [{
        "containerIsArchived": "Null",
        "containerOrderNo": "26",
        "versionNum": "0",
        "containerGlobalUniqueId": "Null",
        "containerIsTenantBased": "true",
        "containerCreatedBy": "user",
        "containerIsDeleted": "false",
        "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
        "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
        "containerApplicationId": "0000000-0000-0000-0000-000000000000",
        "containerIsActive": "Null",
        "containerHeaderText": "apitest19feb16",
        "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
        "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
      }]
    }
  }
};

convertKeysToCamelCase(obj);

function convertKeysToCamelCase(obj) {
  if (!obj || typeof obj !== "object") return null;

  if (obj instanceof Array) {
    return $.map(obj, function(value) {
      return convertKeysToCamelCase(value);
    });
  }

  var newObj = {};
  $.each(obj, function(key, value) {
    key = key.charAt(0).toLowerCase() + key.slice(1);
    newObj[key] = value;
  });
  console.log(newObj);
  return newObj;
};

这里是小提琴链接:fiddle

5 个答案:

答案 0 :(得分:6)

尝试以下代码

function ConvertKeysToLowerCase(obj) {
    var output = {};
    for (i in obj) {
        if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
           output[i.toLowerCase()] = ConvertKeysToLowerCase(obj[i]);
        }else if(Object.prototype.toString.apply(obj[i]) === '[object Array]'){
            output[i.toLowerCase()]=[];
             output[i.toLowerCase()].push(ConvertKeysToLowerCase(obj[i][0]));
        } else {
            output[i.toLowerCase()] = obj[i];
        }
    }
    return output;
};

JsFiddle

答案 1 :(得分:1)

你可以参考@Christophe的 Answer

如果您无法理解,这里的代码就是您: link

JS:

var obj = {
  "Collections": {
    "conTainer": {
      "rowSet": [{
        "containerIsArchived": "Null",
        "containerOrderNo": "26",
        "versionNum": "0",
        "containerGlobalUniqueId": "Null",
        "containerIsTenantBased": "true",
        "containerCreatedBy": "user",
        "containerIsDeleted": "false",
        "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
        "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
        "containerApplicationId": "0000000-0000-0000-0000-000000000000",
        "containerIsActive": "Null",
        "containerHeaderText": "apitest19feb16",
        "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
        "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
      }]
    }
  }
};
var json = JSON.stringify(obj);
var newJson = json.replace(/"([\w]+)":/g, function($0, $1) {
  return ('"' + $1.toLowerCase() + '":');
});
var newObj = JSON.parse(newJson);
console.debug(newObj);

答案 2 :(得分:0)

您需要递归到对象中,以便覆盖较低级别的键。要做到这一点,最好先创建一个对象的副本,然后再修改它,就像这样

// create copy of object
var newObj = JSON.parse(JSON.stringify(obj));
convertKeysToCamelCase(newObj);

function convertKeysToCamelCase(obj) {
    if (!obj || typeof obj !== "object") return null;

    if (obj instanceof Array) {
        return $.map(obj, function(value) {
            return convertKeysToCamelCase(value);
        });
    }

    // manipulates the object being passed in
    $.each(obj, function(key, value) {
        // delete existing key
        delete obj[key];
        key = key.charAt(0).toLowerCase() + key.slice(1);
        obj[key] = value;
        convertKeysToCamelCase(value);
    });

    return obj;
};

小提琴 - http://jsfiddle.net/sbbnmz0b/

答案 3 :(得分:0)

稍微修改以下答案以便于阅读

function convertKeysToCamelCase(obj) {
    var output = {}
    for (var i in obj) {
        var current = obj[i]
        if (isObject(current) || isArray(current)) {
            output[i.toLowerCase()] = convertKeysToCamelCase(current);
        } else {
            output[i.toLowerCase()] = current;
        }
    }
    return output;
};

function isObject(obj) {
    return typeof obj === 'object'
}

function isArray(arr) {
    return Array.isArray(arr)
}

console.log(JSON.stringify(convertKeysToCamelCase(input)));

答案 4 :(得分:0)

这是一个想法,我希望它有用。

var obj = {
    "containerIsArchived": "Null",
    "containerOrderNo": "26",
    "versionNum": "0",
    "containerGlobalUniqueId": "Null",
    "containerIsTenantBased": "true",
    "containerCreatedBy": "user",
    "containerIsDeleted": "false",
    "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
    "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
    "containerApplicationId": "0000000-0000-0000-0000-000000000000",
    "containerIsActive": "Null",
    "containerHeaderText": "apitest19feb16",
    "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
    "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
  };  

for(var i in obj){
    obj[i.toLowerCase()] = obj[i]; 
    delete obj[i];
}

console.log(obj);

最后一个语句以小写形式打印所有键的对象。我们的想法是创建一个 新密钥 - 任何给定密钥的小写版本 - 然后摆脱 旧密钥