如何从文件中解析此json

时间:2019-04-05 13:29:50

标签: java json simplejson

如何使用SimpleJson lib解析此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);

1 个答案:

答案 0 :(得分:0)

JSON.simple似乎是旧的Google库。我认为您需要切换到GSON。以下是示例代码,演示了如何读取给定的JSON。这里的键是原始的JSON对象包含JSON对象的数组作为键data的值。

import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author blj0011
 */
public class JsonSimpleReaderExample
{
    public static void main(String[] args)
    {
        JSONParser parser = new JSONParser();

        try {
            Object obj = parser.parse(new FileReader("file.json"));
            JSONObject jsonObject = (JSONObject) obj;

            JSONArray array = (JSONArray) jsonObject.get("data");
            Iterator<JSONObject> iterator = array.iterator();
            while (iterator.hasNext()) {
                JSONObject jsonObjectInJsonArray = (JSONObject) iterator.next();
                System.out.println(jsonObjectInJsonArray.get("host"));
            }
        }
        catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}

输出:

hostname1
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7