在包含 JsonIdentityInfo 的 JavaScript 中反序列化 Jackson 对象 [英] deserialize Jackson object in JavaScript containing JsonIdentityInfo

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

问题描述

你好(对不起我的英语)

我正在使用使用 SPRING MVC 生成 json 的 Web 服务的 angularjs 前端网站.spring mvc 使用 JsonIdentityInfo 选项进行序列化,因此每个对象仅在 json 中写入一次,并且每次使用引用时,例如她有 2 个计算机"使用相同的对象组件",因此 spring 将 id 放入第一个组件 ("@componentID": 2) 和第二个组件的 id ( 2 ) :

<预><代码>[{"@computerID": 1,成分": {"@componentID": 2,处理器":2,"ram": "8g",硬盘":WD"}},{"@computerID": 3,组件":2}]

我想要什么:

<预><代码>[{"@computerID": 1,"owner" : "B 先生",成分": {"@componentID": 2,处理器":2,"ram": "8g",硬盘":WD"}},{"@computerID": 3,"owner" : "A 先生",成分": {"@componentID": 2,处理器":2,"ram": "8g",硬盘":WD"}}]

我多次搜索执行此操作的代码,但我没有找到任何想法.

我无法编辑网络服务以删除此行为.我可以在客户端使用 javascript 或 jquery(或其他库)编辑 json 以将引用替换为真正引用的对象吗?(数据实际上更复杂更深,我在对象中有3级子对象).

非常感谢.

解决方案

将所有数组成员拆分为新数组:具有完整 component 属性(不仅仅是数字)的和没有的.遍历剩余的原始成员,这些成员应该只有数字component 属性,然后从good"数组中查找相应的@componentID,并进行一些复制和移动.

//初始化一些变量var final = [], temp = [], bad = [],c = {},计算机 = [{"@computerID": 1,成分": {"@componentID": 2,处理器":2,"ram": "8g",硬盘":WD"}},{"@computerID": 3,组件":2}];//将原始数组拆分为 3:final、bad 和 &温度while(computers.length > 0) {c = 电脑.pop();如果(c.hasOwnProperty(组件")){if (typeof c.component === "number") {temp.push(c);} 别的 {final.push(c);}} 别的 {坏推(c);}}//遍历 temp &在 final 中查找 @componentIDwhile (temp.length > 0) {c = temp.pop();//@componentID 应该是 1-of-a-kind 吗?var found = getObjects(final, "@componentID", c.component);如果(找到.长度){c.component = found[0];final.push(c);} 别的 {坏推(c);}}//来源:http://stackoverflow.com/a/4992429/1072176函数 getObjects(obj, key, val) {var 对象 = [];for (var i in obj) {if (!obj.hasOwnProperty(i)) 继续;if (typeof obj[i] == 'object') {objects = objects.concat(getObjects(obj[i], key, val));} else if (i == key && obj[key] == val) {对象.推(对象);}}返回对象;}//应该只产生一两个填充数组:final 和/或 bad警报(JSON.stringify(最终));

你会注意到我实际上创建了三个数组,但最终只填充了两个:final 有你好的新对象,另一个(bad)是一个对于没有组件属性的对象,或者无法找到其组件编号对应的 @componentID 的对象,包罗万象.

hello (sorry for my english)

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
  }
]

what i want :

[
  {
    "@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"
    }
  }
]

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

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).

thanks a lot.

解决方案

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));

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 中反序列化 Jackson 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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