Java - JSON - 返回Object中的对象数组

时间:2017-12-12 17:39:42

标签: java json

嘿同胞溢出的用户, 我在Java中完全围绕JSON调用时遇到了一些问题。

我正在使用位于此处的JSON LIB:

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

JSON目前在服务器端结构如下。

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

以下就是我现在所拥有的。

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) {


        }

    }

}

对我来说很简单,Java不是我以前真正使用过的语言,但我对它的功能有了很好的理解。

我遇到的问题是,当我打印变量loudScreaming(是的,我正在失去理智)时,我获得了嵌套数据的所有JSON。

我实际上想要获得的只是数组补丁中的对象,因此我可以使用该数组与本地副本进行比较,看看是否缺少任何这些文件名。

所以最后我只是尝试将补丁返回到没有任何嵌套数据的数组。

@AakashVerma在评论中回答。我修改了代码,您可以看到它如下所示。

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) {

        }

    }

}

2 个答案:

答案 0 :(得分:0)

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) {

        }

    }

}

见上文。使用了用户的评论来完成。

答案 1 :(得分:0)

[...] - 这种形式表示JSON数组。编辑您的文件,如下所示

   "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. 

如果它不起作用,请尝试以下

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);
}

使用keys()

迭代JSONObject的属性