如何在javascript中解析json字符串包含循环引用? [英] how to parse json string contains circular reference in javascript?

查看:111
本文介绍了如何在javascript中解析json字符串包含循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有以下json字符串。该字符串包含循环引用。我想解析这个字符串,以便引用将被其实际对象替换。我使用 Json.Parse 但它使用引用创建了json对象。有什么方法可以实现这个目标吗?

I have the following json string in javascript. This string contains a circular references. I want to parse this string in such a way that the reference will be replaced by its actual object. I use Json.Parse but it creates the json object with references. Is there any way by whihc i can achieve this ?

{
  "$id": "1",
  "$values": [
    {
      "$id": "2",
      "Event": {
        "$id": "3",
        "Invitaions": {
          "$id": "4",
          "$values": [
            {
              "$ref": "2"
            },
            {
              "$id": "5",
              "Event": {
                "$ref": "3"
              },
              "Id": 2,
              "Name": "test2",
              "Date": "24",
              "EventId": 1
            }
          ]
        },
        "Id": 1,
        "Name": "marriage",
        "Address": "abcd"
      },
      "Id": 1,
      "Name": "test1",
      "Date": "23",
      "EventId": 1
    },
    {
      "$ref": "5"
    },
    {
      "$id": "6",
      "Event": {
        "$id": "7",
        "Invitaions": {
          "$id": "8",
          "$values": [
            {
              "$ref": "6"
            }
          ]
        },
        "Id": 2,
        "Name": "birthday",
        "Address": "abcd"
      },
      "Id": 3,
      "Name": "test3",
      "Date": "25",
      "EventId": 2
    }
  ]
} 


推荐答案

这应该这样做:

function resolveReferences(json) {
    if (typeof json === 'string')
        json = JSON.parse(json);

    var byid = {}, // all objects by id
        refs = []; // references to objects that could not be resolved
    json = (function recurse(obj, prop, parent) {
        if (typeof obj !== 'object' || !obj) // a primitive value
            return obj;
        if ("$ref" in obj) { // a reference
            var ref = obj.$ref;
            if (ref in byid)
                return byid[ref];
            // else we have to make it lazy:
            refs.push([parent, prop, ref]);
            return;
        } else if ("$id" in obj) {
            var id = obj.$id;
            delete obj.$id;
            if ("$values" in obj) // an array
                obj = obj.$values.map(recurse);
            else // a plain object
                for (var prop in obj)
                    obj[prop] = recurse(obj[prop], prop, obj)
            byid[id] = obj;
        }
        return obj;
    })(json); // run it!

    for (var i=0; i<refs.length; i++) { // resolve previously unknown references
        var ref = refs[i];
        ref[0][ref[1]] = byid[refs[2]];
        // Notice that this throws if you put in a reference at top-level
    }
    return json;
}            

这篇关于如何在javascript中解析json字符串包含循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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