javascript - Angular - 将 Jackson 输出转换为 JSON

标签 javascript angularjs json rest jackson

我正在使用的服务器更改了纯 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/

相关文章:

javascript - 更改时的 JQuery 复选框

angularjs - Angular UI-Router解析可选参数

javascript - 在 AngularJS 中使用 Wcf 数据服务

mysql - 未获取以变量作为 json 键的 Mysql json 值

javascript - 是否有任何规范说明什么仍然被视为有效的 JSONP?

javascript - 如何更改滚动部分?

javascript - HTML/Javascript : How to add a live video stream from another website

java - 如何解决 JSONParser() 错误?

javascript - 为什么 Date 类返回 NaN?

angularjs - 关于 ng-repeat 我们可以在 ng-repeat 语句中绑定(bind)吗