反序列化对象杰克逊含JsonIdentityInfo的JavaScript [英] deserialize Jackson object in JavaScript containing JsonIdentityInfo

查看:734
本文介绍了反序列化对象杰克逊含JsonIdentityInfo的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好(对不起,我的英语)

hello (sorry for my english)

我的工作angularjs前端的网站与消费Spring MVC的生产JSON Web服务上。对Spring MVC使用seralization所以每个对象都所著的JSON和对方一次只能有一个时间JsonIdentityInfo选项参考使用,例如她还有2电脑使用同一个对象分量,所以春天放一个id第一组分(@componentID:2)和用于第二组分探微的ID(2):

I'm working on angularjs front end website consuming web service producing json with SPRING MVC. The spring mvc use JsonIdentityInfo option for seralization so each object are writed only one time in the json and each other time a reference is used, example her there is 2 "computer" using the same object "component", so spring put an id to the first component ("@componentID": 2) and for the second component juste the id ( 2 ) :

[
  {
    "@computerID": 1,
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  },
  {
    "@computerID": 3,
    "component": 2
  }
]

我想要什么:

[
  {
    "@computerID": 1,
    "owner" : "Mister B",
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  },
  {
    "@computerID": 3,
    "owner" : "Mister A",
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  }
]

我做了code谁这样做,但我没有找到anythink许多搜索。

I make many search for a code who do this but i didn't find anythink.

我不能删除此behavor编辑Web服务。我可以编辑JavaScript或jQuery的(或其他LIBRAIRIE)客户端上的JSON来取代真正的引用的对象引用? (数据实际上更复杂,更深入,我有3个级别的对象子对象的)。

I can't edit the web service for removing this behavor. Can i edit the json on client side with javascript or jquery (or another librairie) to replace references with the real referenced object ? ( the data are in fact more complex and deeper, i have 3 level of sub object in object).

非常感谢。

推荐答案

拆分所有阵列成员到新阵列:与全组件属性(不只是一个数字)和那些没有。遍历剩余的原始成员应该具备哪些只是数值组件属性,然后查找对应的 @componentID 的好的阵列,并做一些复制和移动。

Split all the array members into new arrays: those with a full component attribute (not just a number) and those without. Loop through the remaining original members which should have just numerical component attributes, then look up the corresponding @componentID from the "good" array, and do some copying and moving.

// initialize some vars
var final = [], temp = [], bad = [],
    c = {},
    computers = [
      {
        "@computerID": 1,
        "component": {
          "@componentID": 2,
          "processor": 2,
          "ram": "8g",
          "harddrive": "wd"
        }
      },
      {
        "@computerID": 3,
        "component": 2
      }
    ];

// split original array into 3: final, bad, & temp
while(computers.length > 0) {
    c = computers.pop();
    if (c.hasOwnProperty("component")) {
        if (typeof c.component === "number") {
            temp.push(c);
        } else {
            final.push(c);
        }
    } else {
        bad.push(c);
    }
}

// loop through temp & look up @componentID within final
while (temp.length > 0) {
    c = temp.pop();
    // should @componentID be 1-of-a-kind?
    var found = getObjects(final, "@componentID", c.component);
    if (found.length) {
        c.component = found[0];
        final.push(c);
    } else {
        bad.push(c);
    }
}


// SOURCE: http://stackoverflow.com/a/4992429/1072176
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

// should result in just one or two populated arrays: final and/or bad
alert(JSON.stringify(final));

您会注意到我实际上是由三个数组,但只有两个结了人口:最后有你的好新的对象,而另一个()是一个包罗万象的对象,而不组件属性,或者其部件数量相应@componentID无法找到。

You'll note I actually made THREE arrays, but only two end up populated: final has your good new objects, and the other one (bad) is a catch-all for objects without a component attribute, or for whose component number a corresponding @componentID cannot be found.

这篇关于反序列化对象杰克逊含JsonIdentityInfo的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆