我正在使用的服务器更改了纯 JSON 的 REST 格式:
{
"removedVertices": [
{
"id": "1",
"info": {
"host": "myhost",
"port": "1111"
},
"name": "Roy",
"type": "Worker"
}
],
"id": "2",
"time": 1481183401573
}
jackson 格式:
{
"removedVertices": [
"java.util.ArrayList",
[
{
"id": "1",
"info": [
"java.util.HashMap",
{
"host": "myhost",
"port": "1111"
}
]
"name": "Roy",
"type": "Worker",
}
]
"id": "2",
"time": 1482392323858
}
如何像以前在 Angular/Javascript 中那样解析它?
最佳答案
假设只有数组受到影响,我会使用 underscore.js并编写一个递归函数来删除 Jackson 类型。
function jackson2json(input) {
return _.mapObject(input, function(val, key) {
if (_.isArray(val) && val.length > 1) {
// discard the Jackson type and keep the 2nd element of the array
return val[1];
}
else if (_.isObject(val)) {
// apply the transformation recursively
return jackson2json(val);
}
else {
// keep the value unchanged (i.e. primitive types)
return val;
}
});
}
关于javascript - Angular - 将 Jackson 输出转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280204/