How to parse this json from file

Asked by At

How I can parse this json file using SimpleJson lib, the format is like this:

Thank you

my file looks like this: json file with Array of Json inside;

 {"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":" "}]}  

my code at this moment:

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;

I did manually like this:

//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 Answers

0
Sedrick On Best Solutions

It looks like JSON.simple is an old Google library. I think you need to switch to GSON. Here is sample code that demos how to read the given JSON. The Key here is that the original JSON object holds an array of JSON objects as the value of key 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());
        }
    }
}

Output:

hostname1
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7