Java 是否需要从json对象检索所有值?
我读了这篇文章,在这篇文章中Java 是否需要从json对象检索所有值?,java,android,json,Java,Android,Json,我读了这篇文章,在这篇文章中 @Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Please wait..."); pDialog.se
@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);
}
@覆盖
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“contacts”);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(“id”,id);
联系人。填写(“姓名”,姓名);
联系方式。放置(“电子邮件”,电子邮件);
联系人:put(“mobile”,mobile);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
MainActivity.this,联系人列表,
R.layout.list_项,新字符串[]{“name”,“email”,
“mobile”},新的int[]{R.id.name,
R.id.email,R.id.mobile});
低压设置适配器(适配器);
}
他/她在将其膨胀到listview之前已检索到所有值
我是JSon新手。我尝试只检索名称,但它没有运行。因此,是否有必要在使用任何值之前检索所有值
谢谢简单的答案是:视情况而定 当您定义JSON数据中的内容时,请确保以“最低限度”的方式进行设计:您只希望包含对您(或您的用户)有价值的信息。意思:在这样的世界中,您只传输您想要显示的数据。然后,您的后端代码很可能希望访问JSON字符串中的所有数据。因为您将所有这些JSON对象设计为只保存相关信息 但很多时候,你的代码只是消耗了一些东西。您不拥有/定义JSON结构,您只知道“其中应该有X、Y、Z字段,我的代码将使用这些字段”。然后,显然,您只提取X、Y、Z,而将其他数据单独留在JSON中。触摸和处理与您的用例无关的信息是没有意义的
换句话说:在现实世界中,你不会因为自己能行而做事。你做事,因为这样做会分别给你的产品用户带来价值 您应该能够获得所需的信息,因此需要一个JSON对象或JSON数组的特定索引。不需要遍历整个有效负载。您是否收到错误消息?如果是的话,用这个更新你的问题。如果没有,请尝试使用调试器进行跟踪,以查看哪里开始出错。此外,它看起来好像您正在尝试循环遍历整个联系人数组。Listview用于列出多个对象,如果您只想检索一个项目,然后检索它并在控件中显示,请在帖子中添加更多说明