在java中解析json rest响应

时间:2013-08-10 05:50:47

标签: java json neo4j

我试图在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);       
}

有人可以帮助我在“data”元素中获取值:

我有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}"
}]

此致 Jayendra

2 个答案:

答案 0 :(得分:1)

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

JSONObject data = (JSONObject) jsonObject.get("data");
final String motherName = (String) data.get("MOTHERS_NAME");

答案 1 :(得分:0)

您使用什么库来解析JSON?我建议您使用Jackson

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

   @SuppressWarnings("rawtypes")
 public static Map toMap(Object object) throws JsonProcessingException{                   ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(object, Map.class);
}