我读了这篇文章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/