1
我有这个JSON文件my JSON file我怎样才能获得JSON值的迭代器内用Java
,我想在Java中的System.out中显示该片段的内容:
else if(jsonObject2.get("type").equals("link")){
System.out.println(jsonObject2.get("tr_name"));
System.out.println(jsonObject2.get("tr_description"));
System.out.println(jsonObject2.get("tr_rules"));
System.out.println(jsonObject2.get("source"));
System.out.println(jsonObject2.get("target"));
}
到目前为止我可以成功地获得tr_name
这是LINKNAME
,但由于tr_description
,tr_rules
,source
和更深入我无法访问它们。从source
和target
我需要得到他们的id
。
我怎么能得到它们?
我的完整的Java代码如下:
package jsontoxml;
import java.io.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.*;
import java.util.*;
public class JacksonStreamExample {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("text.json"));
JSONObject jsonObject = (JSONObject) obj;
//Check inside the JSON array with all graph objects
JSONArray cells = (JSONArray) jsonObject.get("cells");
//Check inside the JSON object with all graph objects
Iterator<JSONObject> iterator = cells.iterator();
while(iterator.hasNext()){
JSONObject jsonObject2 = (JSONObject) iterator.next();
if(jsonObject2.get("type").equals("link")){
System.out.println(jsonObject2.get("tr_name"));
System.out.println(jsonObject2.get("tr_description"));
System.out.println(jsonObject2.get("tr_rules"));
System.out.println(jsonObject2.get("source"));
System.out.println(jsonObject2.get("target"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的系统输出到目前为止是:
LINKNAME
null
null
{"id":"cae4c219-c2cd-4a4b-b50c-0f269963ca24"}
{"id":"d23133e0-e516-4f72-8127-292545d3d479"}
DEFINITELY YESSSSSSSSS !!!!有效!!!谢谢!!! –