简体   繁体   中英

Java - JSON - Return array of Objects within Object

Hey fellow overflow users, I am having some issues wrapping my head fully around JSON calls in java.

I am using the JSON LIB located here:

http://www.java2s.com/Code/JarDownload/java-json/java-json.jar.zip

The JSON is currently structured as follows on the server end.

{  
   "patches":{  
      "patch1.zip":{  
         "name":"patch1.zip",
         "type":"file",
         "path":"patch1.zip",
         "size":15445899,
         "checksum":"ed4e2275ba67470d472c228a78df9897"
      },
      "patch2.zip":{  
         "name":"patch2.zip",
         "type":"file",
         "path":"patch2.zip",
         "size":1802040,
         "checksum":"59de97037e5398c5f0938ce49a3fa200"
      },
      "patch3.zip":{  
         "name":"patch3.zip",
         "type":"file",
         "path":"patch3.zip",
         "size":6382378,
         "checksum":"25efa1e9145a4777deaf589c5b28d9ad"
      },
      "user.cfg":{  
         "name":"user.cfg",
         "type":"file",
         "path":"user.cfg",
         "size":819,
         "checksum":"489a315ac832513f4581ed903ba2886e"
      }
   }
}

And below is what I currently have.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import org.json.JSONException;
import org.json.JSONObject;

public class GetManifest {

    public static void main(String[] args) throws IOException, JSONException {
        try {
            String url = "SEE ABOVE"; //My URL requires a username and password. Please see JSON Above.
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("User-Agent", "Mozilla/5.0");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line = "";
            while((line = in.readLine()) != null) {
                response.append(line).append(System.getProperty("line.separator"));
            }

            JSONObject responseJSON = new JSONObject(response.toString());
            JSONObject loudScreaming = responseJSON.getJSONObject("patches");

            System.out.println(loudScreaming);



        } catch (MalformedURLException e) {


        }

    }

}

Please be easy with me, Java is not a language that I have really used before but I do have an okay understanding of its functions.

The issue I am having is that when I print the variable loudScreaming (yes I am losing my mind) I get all of the JSON with the nested data.

What I am actually trying to get is just the objects within patches to an array so I can then use the array to compare with a local copy and see if any of those file names are missing.

So in the end I am just trying to return the patches to an array without any of the nested data for now.

Answered by @AakashVerma in the comments. I have modified the code and you can see it working as below.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetManifest {

    public static void main(String[] args) throws IOException, JSONException {
        try {
            String url = "https://www.aerosimulations.com/wp-content/uploads/example.json";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder response = new StringBuilder();

            String line = "";
            while((line = in.readLine()) != null) {
                response.append(line).append(System.getProperty("line.separator"));
            }

            JSONObject responseJSON = new JSONObject(response.toString());
            JSONObject obj1_JSON = responseJSON.getJSONObject("patches");

            System.out.println(obj1_JSON);

            JSONArray patches = obj1_JSON.names();

            System.out.println(patches);


        } catch (MalformedURLException e) {

        }

    }

}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetManifest {

    public static void main(String[] args) throws IOException, JSONException {
        try {
            String url = "https://www.aerosimulations.com/wp-content/uploads/example.json";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder response = new StringBuilder();

            String line = "";
            while((line = in.readLine()) != null) {
                response.append(line).append(System.getProperty("line.separator"));
            }

            JSONObject responseJSON = new JSONObject(response.toString());
            JSONObject obj1_JSON = responseJSON.getJSONObject("patches");

            System.out.println(obj1_JSON);

            JSONArray patches = obj1_JSON.names();

            System.out.println(patches);


        } catch (MalformedURLException e) {

        }

    }

}

See Above. used comments from user to complete.

[ ... ] - of this form is what indicates a JSON array. Edit your file like below

   "patches":[ 
      "patch1.zip":{  
         "name":"patch1.zip",
         "type":"file",
         "path":"patch1.zip",
         "size":15445899,
         "checksum":"ed4e2275ba67470d472c228a78df9897"
      },
      "patch2.zip":{  
         "name":"patch2.zip",
         "type":"file",
         "path":"patch2.zip",
         "size":1802040,
         "checksum":"59de97037e5398c5f0938ce49a3fa200"
      },
      "patch3.zip":{  
         "name":"patch3.zip",
         "type":"file",
         "path":"patch3.zip",
         "size":6382378,
         "checksum":"25efa1e9145a4777deaf589c5b28d9ad"
      },
      "user.cfg":{  
         "name":"user.cfg",
         "type":"file",
         "path":"user.cfg",
         "size":819,
         "checksum":"489a315ac832513f4581ed903ba2886e"
      }
   ]
Try running your lines after changing like this. 

If it doesn't work, try this below

FileReader file = new FileReader("patches.json"); //considering patches.json your file name
Object obj = parser.parse(file);
JSONObject jsonObject = (JSONObject) obj;
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
    Object key = keys.next();
    JSONObject value = jsonObject.getJSONObject((String) key);
    String component = value.getString("component");
    System.out.println(component);
}

Iterate through the properties of an JSONObject using keys()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM