Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从元素获取JSON路径_Javascript_Json_D3.js_Svg_Backbone.js - Fatal编程技术网

Javascript 如何从元素获取JSON路径

Javascript 如何从元素获取JSON路径,javascript,json,d3.js,svg,backbone.js,Javascript,Json,D3.js,Svg,Backbone.js,我实现了一个递归javascript函数,通过读取JSON模式在svg中绘制以下结构,如下所示 有没有办法确定所选节点的JSON路径?比如,当用户点击一个节点时(比如说country:string),有没有办法返回JSON路径(比如person.address.country) 更新:我正在使用主干模型,因此我认为解决方案是跟踪并添加JSON路径作为节点的属性,以供以后使用。我正在寻找其他直接的方法 { "title": "person", "type": "object", "pr

我实现了一个递归javascript函数,通过读取JSON模式在svg中绘制以下结构,如下所示

有没有办法确定所选节点的JSON路径?比如,当用户点击一个节点时(比如说
country:string
),有没有办法返回JSON路径(比如
person.address.country

更新:我正在使用主干模型,因此我认为解决方案是跟踪并添加JSON路径作为节点的属性,以供以后使用。我正在寻找其他直接的方法

{
  "title": "person",
  "type": "object",
  "properties": {
    "first name": { "type": "string" },
    "last name": { "type": "string" },
    "age":{ "type":"number"},
    "birthday": { "type": "string", "format": "date-time" },
    "address": {
      "type": "object",
      "properties": {
        "street address": {
      "type": "object",
      "properties": {
        "house number": { "type": "number" },
        "lane": { "type": "string" }
      }    
    },
        "city": { "type": "string" },
        "state": { "type": "string" },
        "country": { "type" : "string" }
      }    
    },
    "phone number": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string"
          },
          "code": {
            "type": "number"
          }
        },
        "required": [
          "location",
          "code"
        ]
      }
    },
    "children": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "nickname":{"type":"string"}
  }
}

递归代码(主干函数)如下所示:

//initiate
  var title = data.title || "Root";
  var count = this.traverseJSONSchema(data, title, 0, 0, this.get('tempParent'));

//function
traverseJSONSchema: function (root, rootName, level, rank, resultPane) {
                    var height = this.nodeHeight,
                            width = this.containerWidth,
                            margin = width / 6,
                            x = 0,
                            overhead = rank * margin,
                            y = level * height;
                    var tempParent = resultPane.append("g").attr("class", "nested-group");
                    if (root.type === "object") {
                        if (rootName !== "") {
                            var nodeText = rootName + ":" + root.type;
                            var node = new DataMapper.Models.Node({parent: tempParent, text: nodeText, x: x, y: y, type: this.get('type'), category: "object", height: height, width: width});
                            node.drawContainerNode(overhead);
                            rank++;
                            level++;
                        }
                        var nestedParent = tempParent.append("g").attr("class", "nested-group");
                        var keys = root.properties; //select PROPERTIES
                        for (var i = 0; i < Object.keys(keys).length; i++) {   //traverse through each PROPERTY of the object
                            var keyName = Object.keys(keys)[i];
                            var key = keys[keyName];
                            level = this.traverseJSONSchema(key, keyName, level, rank, tempParent);
                        }

                    } else if (root.type === "array") {
                        var keys = root.items; //select ITEMS
                        if (rootName !== "") {
                            var nodeText = rootName + ":" + root.type + "[" + keys.type + "]";
                            var node = new DataMapper.Models.Node({parent: tempParent, text: nodeText, x: x, y: y, type: this.get('type'), category: "array", height: height, width: width});
                            node.drawContainerNode(overhead);
                            rank++;
                            level++;
                        }

                        level = this.traverseJSONSchema(keys, "", level, rank, tempParent); //recurse through the items of array
                    } else if (["string", "integer", "number", "boolean"].indexOf(root.type) > -1) {    //when the type is a primitive
                        tempParent.classed("nested-group", false);
                        if (rootName !== "") {
                            var nodeText = rootName + ":" + root.type;
                            var node = new DataMapper.Models.Node({parent: tempParent, text: nodeText, x: x, y: y, type: this.get('type'), category: root.type, height: height, width: width});
                            node.drawContainerNode(overhead);
                            rank++;
                            level++;
                        }
                    }
                    return level;
                }
//启动
var title=data.title | |“根”;
var count=this.traverseJSONSchema(数据、标题、0、0、this.get('tempParent');
//作用
traverseJSONSchema:函数(根、根名称、级别、等级、结果平面){
var height=this.nodeHeight,
宽度=此.containerWidth,
边距=宽度/6,
x=0,
管理费用=排名*利润,
y=标高*高度;
var tempParent=resultPane.append(“g”).attr(“类”,“嵌套组”);
if(root.type==“对象”){
if(rootName!==“”){
var nodeText=rootName+“:”+root.type;
var node=newdatamapper.Models.node({parent:tempParent,text:nodeText,x:x,y:y,type:this.get('type'),category:'object',height:height,width:width});
node.drawContainerNode(开销);
排名++;
级别++;
}
var nestedParent=tempParent.append(“g”).attr(“类”、“嵌套组”);
var keys=root.properties;//选择属性
对于(var i=0;i-1){//当类型为基元时
tempParent.classed(“嵌套组”,false);
if(rootName!==“”){
var nodeText=rootName+“:”+root.type;
var node=new DataMapper.Models.node({parent:tempParent,text:nodeText,x:x,y:y,type:this.get('type'),category:root.type,height:height,width:width});
node.drawContainerNode(开销);
排名++;
级别++;
}
}
回报水平;
}

您可以对路径进行特殊处理

函数getPath(对象,搜索){
功能iter(o,p){
返回Object.keys(o).some(函数(k){
if(k==key&&o[k]&&o[k]。type==value){
路径=p.concat(k).join('.');
返回true;
}
if(o[k]!==null&&typeof o[k]==='object'){
返回iter(o[k],
k=='properties'&&!o.title?
p:
p、 concat(k==“属性”&&o.title?o.title:k)
);
}
});
}
var parts=search.split(“:”),
键=零件[0],
值=零件[1],
路径
国际热核实验堆(对象,[]);
返回路径;
}
var数据={title:“person”,type:“object”,properties:{“first name”:{type:“string”},“last name”:{type:“string”},年龄:{type:“number”},生日:{type:“string”,格式:“date time”},地址:{type:“object”,属性:{“street address”:{type:“object”,属性:{“house number”:{type:“number”},lane:{type:“string”}}},城市:{type:“string”},州:{type:“string”},国家:{type:“string”},电话号码:{type:“array”,项目:{type:“object”,属性:{location:{type:{type:“string”},代码:{type:“number”},必需:[“location”,“code”]},子项:{type:“array”,项目:{type:“string”},昵称:{type:“string:”" } } };
log(getPath(数据,'country:string')

.as console wrapper{max height:100%!important;top:0;}
您可以对路径进行特殊处理

函数getPath(对象,搜索){
功能iter(o,p){
返回Object.keys(o).some(函数(k){
if(k==key&&o[k]&&o[k]。type==value){
路径=p.concat(k).join('.');
返回true;
}
if(o[k]!==null&&typeof o[k]==='object'){
返回iter(o[k],
k=='properties'&&!o.title?
p:
p、 concat(k==‘属性’&