【问题标题】:How to get Data from childs of a JSON如何从 JSON 的子节点获取数据
【发布时间】:2020-02-05 21:38:47
【问题描述】:

我有这行代码,我想从“列表”中删除数字。哪一种是实现它的好方法?我是开发新手,这是我被困了好几天的地方。

    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
         ]
      }}
} 

【问题讨论】:

  • 您没有正确使用 HttpURLConnection,您的 API url 方法是什么?获取还是发布?

标签: java android json android-studio


【解决方案1】:

试试这个伪代码:

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

【讨论】:

    【解决方案2】:

    如果您使用 Retrofit 来消费服务,您可以添加 Gson 以自动将 Json 序列化为 Dto 对象。

    这是一个例子: Get nested JSON object with GSON using retrofit

    【讨论】:

      【解决方案3】:

      如果我假设您在没有护照或令牌身份验证的情况下在您的 API 上使用 GET 方法,您需要这样做。

      您需要定义API方法 获取输入流中的 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        相关资源
        最近更新 更多