lowercase JSON keys to camelCase

怎甘沉沦 提交于 2019-12-08 06:28:22

问题


I am trying to convert the keys of this JSON to camelCase:

{
  "status": true,
  "data": {
    "id": "28c0905c-5abc-4d8b-88a3-d41421e76ccc",
    "username": "mmmmmm",
    "password": "111111",
    "email": "mmmmmmm@gmail.com",
    "birthdate": null,
    "hmac": null,
    "lol": {
      "id": "223da927-4547-47a4-9675-1a2934dbde9d",
      "userid": "28c0905c-5abc-4d8b-88a3-d41421e76ccc",
      "summonerid": 2102444,
      "name": "mmmmmmmmm",
      "profileiconid": 700,
      "profileiconurl": "http://ddragon.leagueoflegends.com/cdn/7.3.2/img/profileicon/700.png",
      "region": "TB",
      "summonerlevel": 10,
      "revisiondate": "32423423423",
      "isverified": true,
      "messages": {
        "30_level": true
      },
      "createdat": "2017-02-23T18:28:11.199Z",
      "updatedat": "2017-02-23T18:28:20.287Z"
    },
    "dota2": null,
    "nationality": null
  },
  "error": {}
}

With this code:

function toCamel(o) {
  var newO, origKey, newKey, value
  if (o instanceof Array) {
    newO = []
    for (origKey in o) {
      value = o[origKey]
      if (typeof value === "object") {
        value = toCamel(value)
      }
      newO.push(value)
    }
  } else {
    newO = {}
    for (origKey in o) {
      if (o.hasOwnProperty(origKey)) {
        newKey = (origKey.charAt(0).toLowerCase() + origKey.slice(1) || origKey).toString()
        value = o[origKey]
        if (value !== null && value.constructor === Object) {
          value = toCamel(value)
        }
        newO[newKey] = value
      }
    }
  }
  return newO
}

But I got this error:

maximum call stack size exceeded

Also tried libraries like: https://github.com/sindresorhus/camelcase-keys but also didnt worked.

来源:https://stackoverflow.com/questions/42425537/lowercase-json-keys-to-camelcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!