简体   繁体   English

在Java中解析json rest响应

[英]parse json rest response in java

I am trying to parse json output from neo4j in java as: 我试图将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);       
}

Can somebody help me to get values inside "data" element: 有人可以帮助我在“数据”元素中获取值:

I have json output from neo4j as follows: 我有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}"
}]

Regards, Jayendra 此致Jayendra

You can try following way. 您可以尝试以下方式。 Inside the for loop get the data node as JSONObject. 在for循环内,将数据节点作为JSONObject获取。 From that data node you can extract every property. 您可以从该数据节点提取每个属性。 I just extracted mother name from data. 我只是从数据中提取了母亲的名字。

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

What library are you using to parse JSON ? 您正在使用哪个库来解析JSON? I'd recommend that you use Jackson 我建议您使用杰克逊

For eg: To get the data you read from the file in a Map, you can write a method like this. 例如:要获取从Map中的文件中读取的数据,可以编写这样的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM