2

我正在尝试将 java 中 neo4j 的 json 输出解析为:

Object obj = parser.parse(new FileReader("D:\\neo4j.json"));

JSONArray json = (JSONArray)  obj;

System.out.println(json.size());

for (int i = 0; i < json.size(); i++) {
    JSONObject jsonObject = (JSONObject) json.get(i);
    String data = (String);   
    jsonObject.get("outgoing_relationships");
    String name = (String) jsonObject.get("name");
    System.out.println(data);
    System.out.println(name);       
}

有人可以帮我在“数据”元素中获取值吗:

我有来自 neo4j 的 json 输出,如下所示:

[{
"outgoing_relationships": "http://host1.in:7474/db/data/node/133/relationships/out",
"data": {
    "MOTHERS_NAME": "PARVEEN BAGEM",
    "MOBILE_NO": "9211573758",
    "GENDER": "M",
    "name": "MOHD",
    "TEL_NO": "0120-",
    "PINCODE": "110001"
},
"traverse": "http://host1.in:7474/db/data/node/133/traverse/{returnType}",
"all_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/all/{-list|&|types}",
"property": "http://host1.in:7474/db/data/node/133/properties/{key}",
"self": "http://host1.in:7474/db/data/node/133",
"properties": "http://lhost1.in:7474/db/data/node/133/properties",
"outgoing_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/out/{-list|&|types}",
"incoming_relationships": "http://host1.in:7474/db/data/node/133/relationships/in",
"extensions": {

},
"create_relationship": "http://host1.in:7474/db/data/node/133/relationships",
"paged_traverse": "http://host1.in:7474/db/data/node/133/paged/traverse/{returnType}{?pageSize,leaseTime}",
"all_relationships": "http://host1.in:7474/db/data/node/133/relationships/all",
"incoming_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/in/{-list|&|types}"
}]

问候, 贾延德拉

4

2 回答 2

1

您可以尝试以下方式。在 for 循环中获取数据节点为 JSONObject。从该数据节点中,您可以提取每个属性。我刚刚从数据中提取了母亲的名字。

JSONObject data = (JSONObject) jsonObject.get("data");
final String motherName = (String) data.get("MOTHERS_NAME");
于 2013-08-10T06:05:59.710 回答
0

你用什么库来解析 JSON ?我建议你使用杰克逊

例如:要获取您从 Map 中的文件中读取的数据,您可以编写这样的方法。

   @SuppressWarnings("rawtypes")
 public static Map toMap(Object object) throws JsonProcessingException{                   ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(object, Map.class);
}
于 2013-08-10T06:08:04.060 回答