简体   繁体   English

Java HttpsURLConnection 响应 JSON 数组

[英]Java HttpsURLConnection response JSON array

I have been receiving JSON objects as a response from HttpsURLConnection.我一直在接收 JSON 对象作为来自 HttpsURLConnection 的响应。 I have been using this to parse the response.我一直在使用它来解析响应。

ObjectMapper map = new ObjectMapper();
JsonNode node = map.readTree(conn.getInputStream());

This has been working fine for me but now I am receiving arrays.这对我来说一直很好,但现在我正在接收数组。 How can I parse them?我该如何解析它们?

This is an example of the response that I receive:这是我收到的回复的示例:

"value": [1 ] 0: { "ID": "2135125324" "name": "John" "2ndName": null "lastName": "James" }

Please try this if you are using AsyncTask write the below code it would help you如果您正在使用 AsyncTask,请尝试此操作,请编写以下代码,它会对您有所帮助

private void yourfunction()
{

    class YourClass extends AsyncTask<String, Void, String>
    {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                JSONObject jsonObj = new JSONObject(s);
                user = jsonObj.getJSONArray("value");
                JSONObject c = user.getJSONObject(0);
                String profile = c.getString("ID");
                String name = c.getString("name");



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


        }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL("your url string");
                HttpURLConnection con= (HttpURLConnection) url.openConnection();
                bufferedReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
                String result;
                result = bufferedReader.readLine();
                return result;
            }
            catch (Exception e) {
                return null;
            }
        }
    }
    YourClass lu=new YourClass();
    lu.execute();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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