Java脚本:通过$ ref属性加载子json消息

时间:2015-04-23 11:22:45

标签: javascript jquery json

我有这个Json架构:

{
  "name":"Myname", 
   "Job":"Myjob", 
   "item":{
      "$ref": "#/definitions/address"
    },
    "definitions":
    {
        "address":
        {
            "street":"My street",
            "postalCode":"11111"
        }
     }
}

我想知道JS是否有办法直接访问地址属性内容并通过“item”属性中存在的引用#/ definitions / address读取它。 所以我将在结果中得到Json对象:

        "address":
        {
            "street":"My street",
            "postalCode":"11111"
        }

1 个答案:

答案 0 :(得分:0)

如果你试图从你的地址索引引用的定义索引中访问地址对象,我想你可以用这种方式

<强> fiddle demo

var object = {
  "name":"Myname", 
   "Job":"Myjob", 
   "address":{
      "$ref": "#/definitions/address"
    },
    "definitions":
    {
        "address":
        {
            "street":"My street",
            "postalCode":"11111"
        }
     }
};

var add_ref = object.address.$ref.split('/');

console.log(object[add_ref[1]][add_ref[2]]);