Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从JSON的子对象获取数据_Java_Android_Json_Android Studio - Fatal编程技术网

Java 如何从JSON的子对象获取数据

Java 如何从JSON的子对象获取数据,java,android,json,android-studio,Java,Android,Json,Android Studio,我有这行代码,我想从“列表”中去掉数字。哪一个是实现这一点的好方法?我刚开始开发,这是一个我被困了好几天的地方 protected Void doInBackground(Void... voids) { try { URL url = new URL("https://{myUrl}"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openC

我有这行代码,我想从“列表”中去掉数字。哪一个是实现这一点的好方法?我刚开始开发,这是一个我被困了好几天的地方

    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("https://{myUrl}");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (line != null){
                line = bufferedReader.readLine();
                data = data + line; }

            JSONArray JA = new JSONArray(data);
            for (int i = 0; i <JA.length(); i++ ){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed = "list:" + JO.get("list");
                dataParsed = dataParsed + singleParsed;
            } } catch (MalformedURLException e) {
            e.printStackTrace(); } catch (IOException e) {
            e.printStackTrace(); } catch (JSONException e) {
            e.printStackTrace(); }
        return null; }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.data.setText(this.dataParsed);
    } ```

JSON file

 {
   "common":{
      "mixed":"yes",
      "nums":{
         "list":[
            1,
            2,
            3,
            4
         ],
         "other":[
            5
         ]
      }}
} 
受保护的Void doInBackground(Void…voids){
试一试{
URL=新URL(“https://{myUrl}”);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
while(行!=null){
line=bufferedReader.readLine();
数据=数据+行;}
JSONArray JA=新JSONArray(数据);
对于(int i=0;i请尝试以下伪代码:

for(int i=0 ; i<JA.length() ; i++)
    {
        try
        {
            JSONObject jobj1 = (JSONObject) JA.get(i);
            JSONObject jobj2 = jobj1.getJSONObject("common");
            JSONObject jobj3 = (JSONObject) jobj2.get("nums");
            JSONArray jarray_list = jobj3.getJSONArray("list");

            //now you have a json array in that there are item of 'list'

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }

for(inti=0;i如果您使用改型来使用服务,您可以添加Gson来自动将Json序列化到Dto对象

以下是一个例子:

如果我假设您在API上使用GET方法而没有passport或令牌身份验证,那么您需要这样做

您需要定义API方法 获取inputstream中的JSON并对其进行解析

            URL url = new URL("https://{myUrl}"); //use HttpsURLConnection if you are sure it is https
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.connect();

            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder result = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            Log.e("ResponseCode",""+conn.getResponseCode()); // 200 is success
            Log.e("TAG", "result" + result); // your json

            if (conn.getResponseCode() == 200) {
            try {

                JSONObject resultOBJ= new JSONObject(result.toString());
                String common = (String)resultOBJ.get("common");
                JSONObject commonOBJ= new JSONObject(common);
                String nums = (String)commonOBJ.get("nums");

                 //do the rest ...

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

如果您没有正确使用HttpURLConnection,您的API url方法是什么?GET还是POST?