2013-05-08 96 views
1

将包含2个属性的节点插入到Neo4j数据库后,如何从REST API查询响应中提取属性(“名称”和“电话”)?我查询数据库脚本是:从Neo4j REST API查询解析json响应的问题

<script> 
function query_database() 
{ 
var restServerURL = "http://localhost:7474/db/data"; //local copy on windows machine 
$.ajax({ 
    type:"POST", 
    url: restServerURL + "/cypher", 
    accepts: "application/json", 
    dataType:"json", 
    data:{ 
     "query" : "start n = node(*) return n", 
     "params" : {} 
    }, 
    success: function(data, xhr, textStatus){ 
        //alert("query success!"); 
      //process query results here 

     alert(JSON.stringify(data, null, 4)); 
    }, 
    error:function(jqXHR, textStatus, errorThrown){ 
        alert(errorThrown); 
    } 
}); 
}//end of query database 

"alert(JSON.stringify(data, null, 4));"显示以下内容:

{ 
"columns": [ 
    "n" 
], 
"data": [ 
    [ 
     { 
      "paged_traverse": "http://localhost:7474/db/data/node/3761/paged/traverse/{returnType}{?pageSize,leaseTime}", 
      "outgoing_relationships": "http://localhost:7474/db/data/node/3761/relationships/out", 
      "data": { 
       "phone": "123.456.7890", 
       "name": "jeff " 
      }, 
      "traverse": "http://localhost:7474/db/data/node/3761/traverse/{returnType}", 
      "all_typed_relationships": "http://localhost:7474/db/data/node/3761/relationships/all/{-list|&|types}", 
      "all_relationships": "http://localhost:7474/db/data/node/3761/relationships/all", 
      "property": "http://localhost:7474/db/data/node/3761/properties/{key}", 
      "self": "http://localhost:7474/db/data/node/3761", 
      "properties": "http://localhost:7474/db/data/node/3761/properties", 
      "outgoing_typed_relationships": "http://localhost:7474/db/data/node/3761/relationships/out/{-list|&|types}", 
      "incoming_relationships": "http://localhost:7474/db/data/node/3761/relationships/in", 
      "incoming_typed_relationships": "http://localhost:7474/db/data/node/3761/relationships/in/{-list|&|types}", 
      "extensions": {}, 
      "create_relationship": "http://localhost:7474/db/data/node/3761/relationships" 
     } 
    ] 
] 

}

许多感谢,

杰夫

回答

3

在你的榜样,你会从这样的响应数据对象获取的姓名和电话:

var name = data.data[0][0].data.name; 
var phone = data.data[0][0].data.phone; 
alert("Name is " + name + "\nPhone is " + phone); 

JSFiddle here

+0

Brian - 感谢您的快速和优秀的回应。很棒! – JeffA 2013-05-08 03:49:44

+0

很高兴我能帮到你。 – 2013-05-08 04:15:58

+0

@JeffA如果它解决了你的问题,请考虑upvoting /接受答案(建议因为你在这里似乎是新的)。 – Gopi 2013-05-08 07:12:47