如何从文件解析此 json

如何使用简单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);


尚方宝剑之说
浏览 82回答 1
1回答

波斯汪

它看起来像一个旧的谷歌图书馆。我认为您需要切换到 .下面是演示如何读取给定 .这里的键是原始对象将对象数组作为键的值。JSON.simpleGSONJSONJSONJSONdataimport java.io.FileReader;import java.util.Iterator;import org.json.simple.JSONArray;import org.json.simple.JSONObject;import org.json.simple.parser.JSONParser;/**&nbsp;*&nbsp;* @author blj0011&nbsp;*/public class JsonSimpleReaderExample{&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; JSONParser parser = new JSONParser();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object obj = parser.parse(new FileReader("file.json"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = (JSONObject) obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray array = (JSONArray) jsonObject.get("data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<JSONObject> iterator = array.iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObjectInJsonArray = (JSONObject) iterator.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonObjectInJsonArray.get("host"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:hostname1hostname1hostname2hostname3hostname4hostname5hostname6hostname7
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java