android - 解析 JSON:Android 中的嵌套对象
问题描述
我对 Android 很陌生。我正在尝试解析 JSON url。但我收到以下错误“Json 解析错误:非官方摘要没有价值”。这是我得到的回应。
来自 url 的响应:{"success":true,"data":{"summary":{"total":15616130,"confirmedCasesIndian":15616082,"confirmedCasesForeign":48,"discharged":13276039,"deaths":182553 ,"confirmedButLocationUnidentified":0},"unofficial-summary":[{"source":"covid19india.org","total":7945975,"recovered":7198877,"deaths":119538,"active":626192} ],"regional":[{"loc":"Andaman and Nicobar Islands","confirmedCasesIndian":5466,"confirmedCasesForeign":0,"discharged":5243,"deaths":64,"totalConfirmed":5466}, {"loc":"安得拉邦","confirmedCasesIndian":976987,"confirmedCasesForeign":0,"出院“:915626”,“死亡”:7472,“totalConfirmed”:976987},{“loc”:“阿鲁纳恰尔邦”,“confirmedCasesIndian”:17113,“confirmedCasesForeign”:0,“出院”:16835,“死亡”: 56,"totalConfirmed":17113},{"loc":"Assam","confirmedCasesIndian":227473,"confirmedCasesForeign":0,"discharged":217296,"deaths":1145,"totalConfirmed":227473},{ "loc":"Bihar","confirmedCasesIndian":342059,"confirmedCasesForeign":0,"discharged":283863,"deaths":1841,"totalConfirmed":342059},{"loc":"Chandigarh","confirmedCasesIndian ":35148,"confirmedCasesForeign":0,"discharged":30768,"deaths":421,"totalConfirmed":35148},{"loc":"Chhattisgarh","confirmedCasesIndian":574299,"confirmedCasesForeign":0,"discharged":442337,"deaths":6274,"totalConfirmed":574299},{"loc ":"Dadra and Nagar Haveli and Daman and Diu","confirmedCasesIndian":5422,"confirmedCasesForeign":0,"discharged":4047,"deaths":4,"totalConfirmed":5422},{"loc":"德里","confirmedCasesIndian":905540,"confirmedCasesForeign":1,"discharged":807328,"deaths":12638,"totalConfirmed":905541},{"loc":"果阿","confirmedCasesIndian":69311," confirmCasesForeign":1,"discharged":60145,"deaths":926,"totalConfirmed":69312},{"loc":"Gujarat","confirmedCasesIndian":428177,"confirmedCasesForeign":1,"discharged":346063,"deaths":5615,"totalConfirmed":428178},{"loc":"Haryana ","confirmedCasesIndian":371610,"confirmedCasesForeign":14,"discharged":318369,"deaths":3483,"totalConfirmed":371624},{"loc":"喜马偕尔邦","confirmedCasesIndian":79410," confirmCasesForeign":0,"discharged":68164,"deaths":1219,"totalConfirmed":79410},{"loc":"查谟和克什米尔","confirmedCasesIndian":150238,"confirmedCasesForeign":0,"discharged" :134697,"死亡":2071,"totalConfirmed":150238},{"loc":"Jharkhand","confirmedCasesIndian":172315,"confirmedCasesForeign":0,"discharged":137590,"deaths":1547,"totalConfirmed":172315},{"loc":"Karnataka","confirmedCasesIndian":1198644," confirmCasesForeign":0,"discharged":1025821,"deaths":13646,"totalConfirmed":1198644},{"loc":"Kerala","confirmedCasesIndian":1272637,"confirmedCasesForeign":8,"discharged":1148671 ,"死亡":4978,"totalConfirmed":1272645},{"loc":"Ladakh","confirmedCasesIndian":12556,"confirmedCasesForeign":0,"discharged":10610,"deaths":134,"totalConfirmed" :12556},{"loc":"Lakshadweep","confirmCasesIndian":1335,"confirmedCasesForeign":0,"discharged":817,"deaths":1,"totalConfirmed":1335},{"loc":"Madhya Pradesh","confirmedCasesIndian":433704,"confirmedCasesForeign": 0,"discharged":350720,"deaths":4713,"totalConfirmed":433704},{"loc":"Maharashtra","confirmedCasesIndian":3960356,"confirmedCasesForeign":3,"discharged":3213464,"deaths ":61343,"totalConfirmed":3960359},{"loc":"Manipur","confirmedCasesIndian":29869,"confirmedCasesForeign":0,"discharged":29106,"deaths":378,"totalConfirmed":29869} ,{"loc":"Meghalaya","confirmedCasesIndian":15116,"confirmCasesForeign":0,"discharged":14105,"deaths":154,"totalConfirmed":15116},{"loc":"Mizoram","confirmedCasesIndian":5085,"confirmedCasesForeign":0,"discharged":4569 ,"deaths":12,"totalConfirmed":5085},{"loc":"Nagaland","confirmedCasesIndian":12650,"confirmedCasesForeign":0,"discharged":12299,"deaths":94,"totalConfirmed" :12650},{"loc":"Odisha","confirmedCasesIndian":377464,"confirmedCasesForeign":0,"discharged":349377,"deaths":1953,"totalConfirmed":377464},{"loc":"本地治里","confirmedCasesIndian":48974,"confirmedCasesForeign":0,"discharged":43184,"死亡“:717”,“totalConfirmed”:48974},{“loc”:“Punjab”,“confirmedCasesIndian”:309316,“confirmedCasesForeign”:0,“出院”:264562,“死亡”:8045,“t
我究竟做错了什么?
package com.sudarshan.jsonparse2;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://api.rootnet.in/covid19-in/stats/latest";
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
JSONObject c = jsonObj.getJSONArray("unofficial-summary").getJSONObject(0);
String source = c.getString("source");
int total = c.getInt("total");
int recovered = c.getInt("recovered");
int deaths = c.getInt("deaths");
int active = c.getInt("active");
// tmp hash map for single contact
HashMap<String, String> summary = new HashMap<>();
// adding each child node to HashMap key => value
summary.put("source", "source: "+source);
summary.put("total", "total:" +String.valueOf(total));
summary.put("recovered", "recovered" +String.valueOf(recovered));
summary.put("deaths", "deaths" + String.valueOf(deaths));
summary.put("active", "active" + String.valueOf(active));
// adding contact to contact list
contactList.add(summary);
} 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);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{ "source","total","recovered","deaths","active"},
new int[]{R.id.source, R.id.total,R.id.recovered,R.id.deaths,R.id.active});
lv.setAdapter(adapter);
}
}
}
解决方案
json就像
{
"success": true,
"data": {
"summary": {
//...
},
"unofficial-summary": [
{
//...
}]
}
}
应该先获取数据:
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://api.rootnet.in/covid19-in/stats/latest";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
// Add: 2021/4/26 add get data first
JSONObject jsonObj = new JSONObject(jsonStr);
String data = jsonObj.getString("data");
Log.w(TAG, "doInBackground: " + data);
// Getting JSON Array node
JSONObject c = new JSONObject(data).getJSONArray("unofficial-summary").getJSONObject(0);
String source = c.getString("source");
int total = c.getInt("total");
int recovered = c.getInt("recovered");
int deaths = c.getInt("deaths");
int active = c.getInt("active");
// tmp hash map for single contact
HashMap<String, String> summary = new HashMap<>();
// adding each child node to HashMap key => value
summary.put("source", "source: " + source);
summary.put("total", "total:" + String.valueOf(total));
summary.put("recovered", "recovered" + String.valueOf(recovered));
summary.put("deaths", "deaths" + String.valueOf(deaths));
summary.put("active", "active" + String.valueOf(active));
// adding contact to contact list
contactList.add(summary);
} catch (final JSONException e) {
//...
}
} else {
//...
}
return null;
}
推荐阅读
- ios - 具有多个目标的项目的单元测试
- python - 如何在 Pytorch 中使用 CUDA 流?
- actionscript-3 - 错误 #1063:参数计数不匹配 - AS3
- c# - 将 Xamarin.Android 项目从 package.config 迁移到 csproj 包参考后无法编译
- javascript - 即使用户单击“返回”,也将 PHP 变量传递给 javascript
- windows - 无法捕获 lParam 的 WM_INPUT 消息以收集原始鼠标输入
- android - 如何使用邮政编码过滤 Google 地方结果
- php - Wordpress/Woocommerce 如何自定义 wc_display_item_meta
- c# - 如何在直线频道中创建语言机器人
- sparql - 如何选择由多个对齐的参考本体构建的巨型本体?