如何使用简单Json库解析这个json文件,格式是这样的:
谢谢
我的文件看起来像这样:里面有Json数组的json文件;
{"data":[{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname2","port":2049,"open":"false", "info":" "},
{"host":"hostname3","port":2049,"open":"false", "info":" "},
{"host":"hostname4","port":443,"open":"false", "info":" "},
{"host":"hostname5","port":443,"open":"false","info":" "},
{"host":"hostname6","port":61208,"open":"false","info":" "},
{"host":"hostname7","port":139,"open":"false","info":" "}]}
此时此刻我的代码:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("D:/file.json"));
JSONArray jsonObject = (JSONArray) obj;
JSONObject arr = (JSONObject) jsonObject.get(0);
JSONArray arguments = (JSONArray) arr.get("arguments");
System.out.println("arguments>>>>>>>>> "+arguments);
for(int i = 0 ; i< arguments.size() ;i++){
JSONObject object = (JSONObject) arguments.get(i);
System.out.println(object);
return object;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
我手动做了这个:
//data
JSONObject obj1 = new JSONObject();
obj1.put("host", "mkyong.com");
obj1.put("port", "555");
obj1.put("open", "false");
obj1.put("info", "");
JSONObject obj2 = new JSONObject();
obj2.put("host", "mkyong.com");
obj2.put("port", "555");
obj2.put("open", "false");
obj2.put("info", "");
JSONArray list = new JSONArray();
list.add(obj2);
JSONObject datajson = new JSONObject();
datajson.put("data", list);
波斯汪
相关分类