我有一个嵌套的json数据和多级expandablelistview。 JSON数据如下所示:
[
{
"DivisionID": "2c0e9dc1-a6a7",
"DivisionName": "Tyuio",
"SubDivision": [
{
"SubDivisionID": "70c3ac53-eec6",
"SubDivisionName": "FM2222",
"Vehicle": [
{
"Nopol": "00571564",
"LastUpdate": "Oct 10 2010 10:10AM",
"LastSpeed": 0,
"LastLon": 106.82176
"Location": "KNOWHERE"
},
{
"Nopol": "352848020936627",
"LastUpdate": "Oct 10 2010 10:10AM",
"LastSpeed": 0,
"LastLon": 10124.228
"Location": "KNOWHERE2"
}
]
}
]
}
]
这就是我如何解析json数据并且正在工作(编辑):
protected ArrayList<Product> doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(URL_DIVI , ServiceHandler.GET);
try {
Divi = new JSONArray(jsonStr)
pProductArrayList=new ArrayList<Product>();
if (Divi != null) {
// looping through All data
for (int i = 0; i < Divi.length(); i++) {
JSONObject c = Divi.getJSONObject(i);
// Storing each json item values in variable
String divid = c.getString(DIV_ID);
String divname = c.getString(DIV_NAME);
pProductArrayList.add(DIV_NAME.getJSONObject(i).toString());
if (c.getString(DIV_NAME).length() != 0)
{
Log.d("Check", "filled");
}else{
Log.d("Check", "unfilled");
}
pSubItemArrayList=new ArrayList<SubCategory>();
SDiv = new JSONArray();
if (SDiv != null) {
JSONArray SubDiv = c.getJSONArray("SubDivision");
for (int j=0; j<SubDiv.length(); j++)
{
JSONObject sub = SubDiv.getJSONObject(j);
String subdivid = sub.getString(SUBDIV_ID);
String subdivname = sub.getString(SUBDIV_NAME);
pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray));
if (sub.getString(SUBDIV_NAME).length() != 0)
{
Log.d("Check2", "subdvi_name filled");
}else{
Log.d("Check2", "subdvi_name unfilled");
}
ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
Vehic = new JSONArray();
if (Vehic != null) {
JSONArray Vehc = sub.getJSONArray("Vehicle");
for (int k=0; k<Vehc.length(); k++)
{
JSONObject veh = Vehc.getJSONObject(k);
String nopol = veh.getString(TAG_NOPOL);
String location = veh.getString(TAG_LOCATION);
String longl = veh.getString(TAG_LONG);
mItemListArray.add(new ItemList(TAG_NOPOL,TAG_LOCATION,TAG_LONG));
if (veh.getString(TAG_NOPOL).length() != 0)
{
Log.d("Check3", "nopol filled");
}else{
Log.d("Check3", "nopol unfilled");
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(DIV_ID, divid);
map.put(DIV_NAME, divname);
map.put(SUBDIV_ID, subdivid);
map.put(SUBDIV_NAME, subdivname);
map.put(TAG_NOPOL, nopol);
map.put(TAG_LOCATION, location);
map.put(TAG_LONG, longl);
// adding HashList to ArrayList
diviList.add(map);
我尝试将JSON存储到此ArrayList
中private ArrayList<Product>pProductArrayList;
private ArrayList<SubCategory>pSubItemArrayList;
private static final String DIV_NAME= "DivisionName";
private static final String SUBDIV_NAME = "SubDivisionName";
private static final String TAG_NOPOL = "Nopol";
private static final String TAG_LOCATION = "Location";
private static final String TAG_LONG = "LastLon";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
mItemListArray.add(new ItemList(TAG_NOPOL, TAG_LOCATION,TAG_LONG));
pSubItemArrayList=new ArrayList<SubCategory>();
pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray));
pProductArrayList=new ArrayList<Product>();
pProductArrayList.add(new Product(DIV_NAME, pSubItemArrayList));
期望的输出:
| Tyuio | <-- parent list
| FM222 | -- child list
| 00571564 | <-- grandchild list #1
| KNOWHERE |
| 106.82176 |
| 35284802 | <-- grandchild list #2
| KNOWHERE2 |
| 10124.228 |
我有一个问题是存储并将数据显示到ArrayList中。我没有得到DivisionName(父列表),SubdivisionName(子列表)和所有车辆阵列(孙子列表)。此JSON数据是动态的。我无法找到解决这个问题的方法。我见过很多在线教程,但那些对我没有帮助。如果我在问题上不太清楚道歉,请问我并澄清......提前谢谢
答案 0 :(得分:0)
class Product {
String divid;
String divname;
ArrayList<SubCategory> subCategories;
}
class SubCategory {
String SubDivisionID;
String SubDivisionName;
ArrayList<Vehicle> Vehicles;
}
class Vehicle {
String Nopol;
String LastUpdate;
String LastSpeed;
String LastLon;
String Location;
}
private ArrayList<Product> parsing(String jsonStr) {
// TODO Auto-generated method stub
try {
JSONArray Divi = new JSONArray(jsonStr);
ArrayList<Product> pProductArrayList = new ArrayList<Product>();
if (Divi != null) {
// looping through All data
for (int i = 0; i < Divi.length(); i++) {
JSONObject c = Divi.getJSONObject(i);
// Storing each json item values in variable
Product product = new Product();
product.divid = c.getString("DivisionID");
product.divname = c.getString("DivisionName");
JSONArray SDiv = c.getJSONArray("SubDivision");
ArrayList<SubCategory> pSubItemArrayList = new ArrayList<SubCategory>();
for (int j = 0; j < SDiv.length(); j++) {
JSONObject sub = SDiv.getJSONObject(j);
SubCategory subCategory = new SubCategory();
subCategory.SubDivisionID = sub
.getString("SubDivisionID");
subCategory.SubDivisionName = sub
.getString("SubDivisionName");
JSONArray Vehc = sub.getJSONArray("Vehicle");
ArrayList<Vehicle> mItemListArray = new ArrayList<Vehicle>();
for (int k = 0; k < Vehc.length(); k++) {
JSONObject veh = Vehc.getJSONObject(k);
Vehicle vehicle = new Vehicle();
vehicle.Nopol = veh.getString("Nopol");
vehicle.Location = veh.getString("Location");
vehicle.LastLon = veh.getString("LastLon");
vehicle.LastSpeed = veh.getString("LastSpeed");
vehicle.LastUpdate = veh.getString("LastUpdate");
mItemListArray.add(vehicle);
}
subCategory.Vehicles=mItemListArray;
pSubItemArrayList.add(subCategory);
}
product.subCategories = pSubItemArrayList;
pProductArrayList.add(product);
}
}
return pProductArrayList;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
我希望这段代码可以帮到你。