如何访问具有多个数组对象的JSON数据:android
我一直在从具有多个数据集的JSON文件中获取数据。如何访问具有多个数组对象的JSON数据:android
{"status":"ok","count":3,"count_total":661,"pages":133,"posts":
[{"id":20038,"type":"post","slug":"xperia-launcher-download","url":"http:\/\/missingtricks.net\/xperia-launcher-download\/","status":"publish","title":"Download Xperia Launcher app for Android (Latest Version)",
{"id":94,"type":"post","slug":"top-free-calling-apps-of-2014-year","url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2014-year\/","status":"publish","title":"Best Free Calling Apps for Android November 2014",
{"id":98,"type":"post","slug":"top-free-calling-apps-of-2016-year" "url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2016-year\/","status":"publish","title":"Best Free Calling Apps for Android December 2016"}]}
我需要从上面的JSON文件访问标题,URL和状态。
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<DataFish> data = new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
DataFish fishData = new DataFish();
fishData.status = json_data.getString("status");
fishData.title = json_data.getString("url");
fishData.sizeName = json_data.getString("title");
data.add(fishData);
}
} catch (JSONException e) {
Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
Log.d("Json","Exception = "+e.toString());
}
}
我得到一个JSONException与上面的代码。
如何从JSON文件访问标题,状态和URL?
你要取你的JSONArray
这是一个JSONObject
里面,所以创建一个JSONObject
和索引抓取您的阵列“上岗”
1)result
是JSONObject
所以创建JSONObject
2。 )通过指数获取它与索引值取你的JSONArray
为“上岗”
3)现在只需遍历数组对象
JSONObject jObj = new JSONObject(result);
JSONArray jArray = jObj.getJSONArray("posts");
// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
DataFish fishData = new DataFish();
fishData.status = json_data.getString("status");
fishData.title = json_data.getString("url");
fishData.sizeName = json_data.getString("title");
data.add(fishData);
}
注:我不知道天气是用较短的版本,虽然你的JSON对象的样品的反应应该与}
不,
结束。
[{ “ID”:20038, “类型”: “后”, “弹头”: “XPERIA-发射器下载”, “URL”:“http://missingtricks.net/xperia-launcher -download/“ ”状态“: ”发布“, ”称号“: ”下载 的Xperia桌面应用的Android(最新版)“,
// ^^^ there should be a } not a , to end json
// so make sure to do the correction so it will look like => ...st Version)"},
{ ”ID“:94”输入“:”后“‘弹头’:‘免费顶级呼 - 应用 - 的2014年’,‘URL’:” http://missingtricks.net/top-free-calling-apps-of- 2014年/“,”状态“:”发布“,”标题“:”最佳 免费拨打Android版2014年11月“,]
改进:
可以使用optString
避免空或非字符串值,如果没有映射关键
这有两个变化
获取一个可选的字符串与一个关键字相关联。如果没有这样的密钥,它将返回 defaultValue。
public String optString(String key, String defaultValue) {
fishData.status = json_data.optString("status","N/A");
// will return "N/A" if no key found
如果没有找到键,然后简单地使用
fishData.status = json_data.optString("status");
// will return "" if no key found where "" is an empty string
使用optString而不是getString它将避免零点异常 – Nithinlal
@Nithinlal是啊这是我给大家的建议,我只是看对于一个旧的帖子指出这一点:)谢谢 –
这是工作! –
响应不是正确的JSON格式 – Nithinlal
@Nithinlal我正要指向相同的 –