Read json , for each command to run into the server!

Discussion in 'Spigot Plugin Development' started by AleksBG1, Jul 19, 2018.

  1. Hello guys, i have that code

    Code (Text):
        public static void main(String[] args) throws Exception {
           
            // Json Stream Reader
            String jsonS = "";
           
            // Connect to web api
            URL url = new URL("http://b50172e8.ngrok.io/api/plugin/521100d075c1284b944841394e157744");
           
            // Make Connection
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Accept","*/*");
            conn.connect();
           
            // Stream reader
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;

            while((inputLine = in.readLine()) != null) {
                jsonS+=inputLine;
            }
           
            // Read json response
            Gson gson = new Gson();
           
            // Json Object
            JsonObject jsonObject= gson.fromJson(jsonS, JsonObject.class);
            JsonElement id = jsonObject.get("data");

            // Close connection
            in.close();
           
           
           
        }
    The data that url return is this: http://prntscr.com/k8jpru

    I want to make for each cmd to run into the server (I want only loop that display only command) Thanks!
     
  2. If this can help you :
    Code (Java):

           // my json
            String json = "[{\"value\":3,\"name\":\"me\"},{\"value\":42,\"name\":\"you\"}]";

            // read from json to a list of map
            List<LinkedTreeMap<String, Object>> list = new Gson().fromJson(json, List.class);

            // for each map
            list.forEach(map -> {
                // get the name
                String name = (String) map.getOrDefault("name", "?");
                // get the value
                int value = ((Double) map.getOrDefault("value", 0)).intValue();

                // print the line
                System.out.println("Name: " + name + ", Value: " + value);
            });
    Out that :
    Code (Text):
    Name: me, Value: 3
    Name: you, Value: 42
    (Because I don't know what you try to do)