java - 是否有必要从 json 对象中检索所有值?

标签 java android json

我读了这篇文章https://www.androidhive.info/2012/01/android-json-parsing-tutorial/在这篇文章中

   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("contacts");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString("id");
                    String name = c.getString("name");
                    String email = c.getString("email");
                    String address = c.getString("address");
                    String gender = c.getString("gender");

                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject("phone");
                    String mobile = phone.getString("mobile");
                    String home = phone.getString("home");
                    String office = phone.getString("office");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("id", id);
                    contact.put("name", name);
                    contact.put("email", email);
                    contact.put("mobile", mobile);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[]{"name", "email",
                "mobile"}, new int[]{R.id.name,
                R.id.email, R.id.mobile});

        lv.setAdapter(adapter);
    }

他/她在将其扩展到 ListView 之前已检索了所有值。

我是 Json 新手。我尝试仅检索名称,但它没有运行。那么,在使用任何值之前是否有必要检索所有值。

谢谢

最佳答案

简单的答案是:这取决于情况。

当您定义 JSON 数据中的内容时,您一定要以“简约”的方式设计它:您只想包含具有值(value)<的信息/strong> 为您(或您的用户)。意思是:在这样的世界中,您只传输您想要显示的数据。然后,您的后端代码很可能想要访问 JSON 字符串中的所有数据。因为您将所有这些 JSON 对象设计为仅保存相关信息。

但很多时候,你的代码只是消耗一些东西。您不拥有/定义 JSON 结构,您只知道“其中应该有字段 X、Y、Z,我的代码将使用这些字段”。然后,显然,您只提取 X、Y、Z。并且您将其他数据保留在该 JSON 中。接触和处理与您的用例无关的信息是没有意义的。

换句话说:在现实世界中,你做某事并不是因为你可以。你做事,因为这样做会给你和你的产品的用户带来值(value)。

关于java - 是否有必要从 json 对象中检索所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53168207/

相关文章:

android - 如何在 Google Play 中为每个国家分配不同的应用名称

android - 错误的Google Play服务版本

c# - 使用System.Text.Json反序列化匿名类型

android - 如何获取 AutoCompleteTextView 适配器的正确 ID

Java 小程序的替代方案和桌面应用程序中的可用性

java - 找出方程中的单个未知数

java - 将RTL支持添加到GitHub库吗?

javascript - 检查多个复选框 jQuery

javascript - 访问通过 xhr 请求获取的 HTML 源中所有脚本标签中的变量

java.lang.NoSuchMethodError : javax. servlet.ServletContext.getContextPath()Ljava/lang/String 在cargo中部署到Tomcat时