使用不同的内部数组解析Android中的json
我有一个json url,它输出这个(代码片段) 我遇到的问题是,如何根据其格式对其进行解析,以及如何获取诸如“code”、“slug”和“title”等数据。这是我到目前为止所做的,但它似乎是错误的,因为我可能必须有2个循环,而不是我认为的1个 这就是我目前所拥有的使用不同的内部数组解析Android中的json,android,json,parsing,Android,Json,Parsing,我有一个json url,它输出这个(代码片段) 我遇到的问题是,如何根据其格式对其进行解析,以及如何获取诸如“code”、“slug”和“title”等数据。这是我到目前为止所做的,但它似乎是错误的,因为我可能必须有2个循环,而不是我认为的1个 这就是我目前所拥有的 @Override protected Void doInBackground(Void... params) { // Create the array arraylist =
@Override
protected Void doInBackground(Void... params) {
// Create the array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrive JSON Objects from the given website URL in JSONfunctions.class
jsonobject = JSONfunctions
.getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");
try {
// Locate the array name
jsonarray = jsonobject.getJSONArray("item");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("code", jsonobject.getString("code"));
map.put("slug", jsonobject.getString("slug"));
map.put("title", jsonobject.getString("title"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@覆盖
受保护的Void doInBackground(Void…参数){
//创建数组
arraylist=新的arraylist();
//在JSONfunctions.class中从给定的网站URL检索JSON对象
jsonobject=JSONfunctions
.getJSONfromURL(“http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");
试一试{
//找到数组名
jsonarray=jsonobject.getJSONArray(“项”);
for(int i=0;i
您在这方面遗漏了一点
在这里,您得到了第一个json对象:
jsonobject = JSONfunctions
.getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");
这个json对象是包含状态和结果的整个大json对象,但是您直接想要访问结果对象中的json数组!使用此选项:
// Locate the array name
jsonarray = jsonobject.getJSONArray("items");
正确的做法是,您必须首先使用
JSONObject resultObject = jsonobject.getJSONObject("result");
然后使用resultObject获取数组
try {
// Locate the array name
jsonarray = resultObject .getJSONArray("item");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("code", jsonobject.getString("code"));
map.put("slug", jsonobject.getString("slug"));
map.put("title", jsonobject.getString("title"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
试试看{
//找到数组名
jsonarray=resultObject.getJSONArray(“项”);
for(int i=0;i
我希望你能理解我的答案,但如果你对我的答案有其他问题,请随时提问!:)
jsonarray=jsonobject.getJSONArray(“项”)代码>根据JSON判断,数组名称应该是“items”。。。
try {
// Locate the array name
jsonarray = resultObject .getJSONArray("item");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("code", jsonobject.getString("code"));
map.put("slug", jsonobject.getString("slug"));
map.put("title", jsonobject.getString("title"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}