问题
I have a nested json data and multilevel expandablelistview. The JSON data is looks like this :
[
{
"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"
}
]
}
]
}
]
this is how i parsing the json data and is working (EDIT):
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);
i am try to storing JSON into this 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));
Desired output :
| Tyuio | <-- parent list
| FM222 | -- child list
| 00571564 | <-- grandchild list #1
| KNOWHERE |
| 106.82176 |
| 35284802 | <-- grandchild list #2
| KNOWHERE2 |
| 10124.228 |
I have a problem to store and show the data into ArrayList. I don't get DivisionName (parent-list), SubdivisionName (child-list), and all of vehicle array (grandchild-list). This JSON data is dynamic. I can't finding out how to solve this problem. I have seen many online tutorials but those was not helpful for me. If I'm not very clear in my question apologies, just ask and I'll clarify.. Thank you in advance
回答1:
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;
}
i hpoe this code will help you.
来源:https://stackoverflow.com/questions/27290226/android-storing-nested-json-object-to-arraylist-multilevel-expandablelistview