json - 如何映射json实体?
问题描述
有一种方法可以在 react-native 中映射 json 对象吗?例如,假设我收到以下对象:
{
"dados": {
"ultimaAtualizacao": {
"nome": "Lala"
}
"nascimento": "01/01/2001"
}
}
我想像这样映射它:
{
"data": {
"lastUpdate": {
"name": "Lala"
}
"dob": "01/01/2001"
}
}
考虑到fetch
返回第一个对象,可以执行以下代码:
myService.get(url).then(response => this.mapObject(response, []));
//Considering state is initialized and I have the mapedEntity to return correct properties
mapObject(jsonObject, parentProperty) {
for (let property in jsonObject) {
if (Array.isArray(jsonObject[property])) {
this.mapArray(jsonObject[property], [...parentProperty,property]); //Would be something like mapObject function
} else if (typeof jsonObject[property] === 'object') {
this.mapObject(jsonObject[property],[...parentProperty,property]);
} else {
let prop = this.state;
for(let k of parentProperty) {
prop = prop[mapedEntity[k]];
}
prop[entidadeMapeada[property]] = jsonObject[property];
}
}
}
有某种更简单的方法来实现这一点?
解决方案
推荐阅读
- java - 为具有相同属性的多个不同POJO创建自定义验证器java spring
- java - Java Maven + Eclipse + junit:找不到包
- c++ - C++ 中多个模板之一的模板专业化
- python - 损失函数的爆炸式增长,LSTM 自动编码器
- angularjs - 浏览器“后退”按钮无法正常工作
- python - 如何将单个日期转换为 Pandas 的一个日期列
- c++ - C++:如何通过这种方式建立结构之间的关系?
- pagination - 请求事件列表时如何对 Pagerduty REST API 结果进行分页?
- java - 类文件有错误的版本 xxx,应该是 xxx
- ios - Apple App Review 定位服务同意问题