我想从来自web服务的json数据填充微调器。我有一个名为“categoriesList”的主数组,其中包括名为“subcategories”的子数组,但这些数组具有相同的名称并包含数据对象。我想根据第一个微调器(即categoriesList)的选择填充子类别的第二个微调器。问题是,当我从第一个微调器(categoriesList)中选择一个项目时,第二个微调器将显示所有子数组,但我只需要在第二个微调器(子类别)中显示相应的子数组。
以下是json数据格式:
{
"success": true,
"code": 200,
"message": "Data Found.",
"content": {
"companyTypes": [
{
"id": 1,
"title": "Distributor"
},
{
"id": 2,
"title": "Mechanics"
},
{
"id": 3,
"title": "Retailer"
},
{
"id": 4,
"title": "Others"
}
],
"citiesList": [
{
"id": 1,
"name": "Sharja"
}
],
"brandsList": [
{
"id": 1,
"title": "Test Brand"
},
{
"id": 2,
"title": "Brand 2"
},
{
"id": 3,
"title": "Brand 3"
},
{
"id": 4,
"title": "Brand 4"
},
{
"id": 5,
"title": "Brand 5"
}
],
"categoriesList": [
{
"id": 1,
"title": "Test Category",
"subCategories": [
{
"id": 1,
"title": "sub category 1"
},
{
"id": 5,
"title": "Sub Category 5"
}
]
},
{
"id": 2,
"title": "Category 2",
"subCategories": [
{
"id": 2,
"title": "Sub Category 2"
},
{
"id": 3,
"title": "Sub Category 3"
}
]
},
{
"id": 3,
"title": "Category 3",
"subCategories": [
{
"id": 4,
"title": "Sub Category 4"
}
]
}
]
}
}
这是我的android java代码:
类别列表代码
public void populateSpinnerCategories() {
StringRequest request = new StringRequest(Request.Method.GET, urlConfigurationsCategories,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
final ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
jsonObjectResponse = new JSONObject(response);
jsonObjectContent = jsonObjectResponse.getJSONObject("content");
jsonArrayCompanyTypes = jsonObjectContent.getJSONArray("categoriesList");
for (int i = 0; i < jsonArrayCompanyTypes.length(); i++) {
jsonObjectID = jsonArrayCompanyTypes.getJSONObject(i);
distributorId = jsonObjectID.getInt("id");
title = jsonObjectID.getString("title");
map = new HashMap<String, String>();
map.put("title", title);
map.put("id", distributorId + "");
myArrList.add(map);
//Log.i("Rsp", "onResponse: " + distributorId + "------Title: " + title);
}
SimpleAdapter simpleAdapter;
simpleAdapter = new SimpleAdapter(getActivity(), myArrList, R.layout.activity_show,
new String[]{"title"},
new int[]{R.id.tv_column_name});
spinnercategoriesList.setAdapter(simpleAdapter);
spinnercategoriesList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedCompanyType = myArrList.get(position).get("title").toString();
categoryId = (int) (spinnercategoriesList.getSelectedItemId() + 1);
//populateCompanies(DIST_ID);
//TODO populate the subcategories spinner
populateSpinnerSubCategories(categoryId);
loadCategories();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getActivity()).add(request);
}
子类别代码
public void populateSpinnerSubCategories(final int categoryId) {
String url = "http://radial-energy.com/radial/api/configurations";
StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
final ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
String subCategory = null;
int subCatId = 0;
jsonObjectResponse = new JSONObject(response);
jsonObjectContent = jsonObjectResponse.getJSONObject("content");
JSONArray jsonArrayCategories = jsonObjectContent.getJSONArray("categoriesList");
for (int i = 0; i < jsonArrayCategories.length(); i++) {
jsonObjectID = jsonArrayCategories.getJSONObject(i);
JSONArray jsonArraySubCategories = new
JSONArray(jsonArrayCategories.getJSONObject(i)
.getString("subCategories"));
System.out.println("Sub Categories Array: " + jsonArraySubCategories);
for (int j = 0; j < jsonArraySubCategories.length(); j++) {
JSONObject jsonObjectSubCategories = jsonArraySubCategories.getJSONObject(j);
subCatId = jsonObjectSubCategories.getInt("id");
subCategory = jsonObjectSubCategories.getString("title");
Log.d("CATEGORY: ", subCategory);
map = new HashMap<String, String>();
map.put("title", subCategory);
map.put("id", subCatId + "");
myArrList.add(map);
}
}
SimpleAdapter simpleAdapter;
simpleAdapter = new SimpleAdapter(getActivity(), myArrList,
R.layout.activity_show, new String[]{"title"},
new int[]{R.id.tv_column_name});
spinnerSubcategoriesList.setAdapter(simpleAdapter);
spinnerSubcategoriesList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedSubCategory = myArrList.get(position).get("title").toString();
//DIST_ID = (int) (spinnerSubcategoriesList.getSelectedItemId() + 1);
//populateCompanies(DIST_ID);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(getActivity()).add(request);
}
描述数据模型
class DataItem {
String id;
String title;
List<DataItem> subCategories;
}
class DataContent {
List<DataItem> companyTypes;
List<DataItem> citiesList;
List<DataItem> brandsList;
List<DataItem> categoriesList;
}
class DataResponse {
Boolean success;
Integer code;
String message;
DataContent content;
}
只需使用Gson解析您的响应
@Override
public void onResponse(String response) {
Gson gson = new GsonBuilder().create();
DataResponse dataResponse = gson.fromJson(response, DataResponse.class);
...
问题内容: 我想解析一个json文件,但它经过这样的事情: 但是大约有三千个这样的对象。我一直在使用Gson解析我的json对象,但是我怎么解析这种文件呢?以及如何检索名称“ CDG”或“ ORY”? 问题答案: 您可以尝试如下操作: 使用gson,您可以按以下方式检索键名: 并使用java- json 可以执行以下操作: 从网址获取json:
问题内容: 我有一个Json Array作为没有名称的字符串,我想解析它如何在android中进行? 我的JsonArray是: 问题答案: 试试这个..
问题内容: 例如: 因为json数组被解码为go数组,并且go数组需要显式定义类型,所以我不知道如何处理它。 问题答案: 首先,json无效,对象必须具有键,因此它应该类似于或just 。 而当您处理多种随机类型时,只需使用即可。
问题内容: 如何使用Gson解析此JSON?我有一个具有多个对象类型的数组,但我不知道需要创建哪种对象来保存此结构。我无法更改json消息(我无法控制服务器)。 唯一起作用的类是 JSON消息 (请注意具有多个对象类型的数组。) 问题答案: 《 Gson用户指南》明确涵盖了以下内容: https://sites.google.com/site/gson/gson-user-guide#TOC-Se
我想从这个链接获取一个url: 它给出结果: { " data ":{ " URL ":" https://FBC dn-profile-a . akamaihd . net/hprofile-AK-xa P1/t 1.0-1/s200x 200/10342822 _ 10202261537234765 _ 3194866551853134720 _ n . jpg "," is_silhouet
问题内容: 如何解析JSON ARRAY以获取没有[]和“”的数据 这是json 我的代码: 输出:[“ 23,Damansara-蒲种高速公路(Bandar Puchong Jaya)”,“ 47100 Puchong Batu Dua Belas,雪兰莪,”马来西亚“] 我只想要数据。如: 23,Damansara-Puchong Hwy(Bandar Puchong Jaya),47100