android json 驼峰,格式化JSON数据被驼峰格式(formatting json data to be camelCas

我从那个看起来像这样的服务器的JSON响应:

{

"Response": {

"FirstName": "John",

"LastName": "Smith",

"NickNames": {

"NameOne": "Johnny",

"NameTwo": "JohnS",

"NameThree": "Smithy"

},

"Success": true,

"Errors": []

}

}

有没有一种方法,我可以运行通过函数这个反应,使每个键值对的关键将驼峰格式?

因此,输出将类似于:

{

"response": {

"firstName": "John",

"lastName": "Smith",

"nickNames": {

"nameOne": "Johnny",

"nameTwo": "JohnS",

"nameThree": "Smithy"

},

"success": true,

"errors": []

}

}

如果有人能在正确的方向指向我,那简直太好了。

谢谢。

Answer 1:

你会为JSON.parse将值赋给新的属性被套管下一个齐磊功能。

function toCamelCase(key, value) {

if (value && typeof value === 'object'){

for (var k in value) {

if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {

value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];

delete value[k];

}

}

}

return value;

}

var parsed = JSON.parse(myjson, toCamelCase);

它是如何工作的更多信息在这个苏答案 。

Answer 2:

用户“@i恨懒惰”提出这一方法 - 使用“齐磊”功能是 - 正确的。 然而,他的功能并没有为我工作。

也许是因为我解析JSON数组。 另外我使用ReSharper的 ,它抱怨一个代码味道:)(“并非所有的代码路径返回值”)。 所以,我结束了使用功能,从另一个SO问题,这确实为我工作:

function camelCaseReviver(key, value) {

if (value && typeof value === 'object') {

for (var k in value) {

if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {

value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];

delete value[k];

}

}

}

return value;

}

Answer 3:

这里是一个功能性递归(ES6)的方法。

function convertKeysToCamelCase(o) {

if (o === null || o === undefined) {

return o;

} else if (Array.isArray(o)) {

return o.map(convertKeysToCamelCase);

}

return typeof o !== 'object' ? o : Object.keys(o).reduce((prev, current) => {

const newKey = `${current[0].toLowerCase()}${current.slice(1)}`;

if (typeof o[current] === 'object') {

prev[newKey] = convertKeysToCamelCase(o[current]);

} else {

prev[newKey] = o[current];

}

return prev;

}, {});

}

// successfully tested input

const o = {

SomeNum: 1,

SomeStr: 'a',

SomeNull: null,

SomeUndefined: undefined,

SomeBoolean: true,

SomeNaN: NaN,

NestedObject: {

SomeSentence: 'A is for apple',

AnotherNested: {

B: 'is for blahblah'

}

},

NumArray: [1, 2, 3, 4],

StringArray: ['a', 'b', 'c'],

BooleanArray: [true, false],

ArrayOfArrays: [[1,2,], ['a','b']],

ObjectArray: [{Foo:'bar'}, {Hello:'world', Nested:{In:'deep'}}],

MixedArray: [1,'a', true, null, undefined, NaN, [{Foo:'bar'}, 'wat']]

}

const output = convertKeysToCamelCase(o);

console.log(output.mixedArray[6][0].foo); // 'bar'

Answer 4:

@adamjyee您的解决方案的工作,除了整数嵌套数组。 一个小的修复可能是:

function convertKeysToCamelCase (o) {

if (o === null) {

return null

} else if (o === undefined) {

return undefined

} else if (typeof o === 'number') {

return o

} else if (Array.isArray(o)) {

return o.map(convertKeysToCamelCase)

}

return Object.keys(o).reduce((prev, current) => {

const newKey = `${current[0].toLowerCase()}${current.slice(1)}`

if (typeof o[current] === 'object') {

prev[newKey] = convertKeysToCamelCase(o[current])

} else {

prev[newKey] = o[current]

}

return prev

}, {})

[右置评,但缺乏评论特权:(]

Answer 5:

你需要写遍历树,并返回其中的对象键已经更新了新的树递归函数。 递归函数会叫自己来解决它遇到的任何子对象。

文章来源: formatting json data to be camelCased

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值