简单的 JSON 文件,只有一个数组,用于我的 Android 应用程序
Simple JSON file with just one Array to get on my Android App
我有一个 JSON 文件,只有一个数组,如本例所示(如您所见,该数组没有名称):
["Cream","Cheese","Milk","Powder Milk","Blue Cheese","Gouda Cheese"]
如何才能将此数组放入我的 Android Studio 中的数组或数组列表中?
在此先感谢您!
这是我创建的用于获取此数组的函数,但我无法完成它我仍然是处理 json 个文件的菜鸟。
public void getProducts() {
JsonArrayRequest myresponseArray = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0 ; i < response.length();i++){
JSONArray responseArray = response.getJSONArray(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
现在我想将此 "responseArray" 添加到我在 MainActivity
上创建的产品数组列表中
ArrayList<String> products = new ArrayList<String>();
这个问题之前有人问过并且有据可查:
Get JSONArray without array name?
试试 geString(int index)
for (int i = 0; i < responeArray.length(); i++) {
products.add( responseArray.getString(i) );
}
我有一个 JSON 文件,只有一个数组,如本例所示(如您所见,该数组没有名称):
["Cream","Cheese","Milk","Powder Milk","Blue Cheese","Gouda Cheese"]
如何才能将此数组放入我的 Android Studio 中的数组或数组列表中?
在此先感谢您!
这是我创建的用于获取此数组的函数,但我无法完成它我仍然是处理 json 个文件的菜鸟。
public void getProducts() {
JsonArrayRequest myresponseArray = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0 ; i < response.length();i++){
JSONArray responseArray = response.getJSONArray(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
现在我想将此 "responseArray" 添加到我在 MainActivity
上创建的产品数组列表中ArrayList<String> products = new ArrayList<String>();
这个问题之前有人问过并且有据可查:
Get JSONArray without array name?
试试 geString(int index)
for (int i = 0; i < responeArray.length(); i++) {
products.add( responseArray.getString(i) );
}