我有一个Json文件,如下所示:
{
"fileExtension":["xls","xlsx"],
"fileType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"fileName": "Dummy123",
"worksheetCount":"3",
"worksheetList":["ws1","ws2","ws3"],
"worksheet":
{
"ws1":
{
"worksheetName":"Employee Info",
"sectionCount":"1",
"sectionList":["Employee"],
"sectionName":"Employee",
"sectionType":"table",
"Employee":
{
"columnCount": "3",
"beginningRowNumber": "2",
"endingRowNumber": "7",
"columnList":[
"columns":
{
"c1":
{
"c1_header": "EMP ID",
"c1_datatype" :"Numeric"
},
"c2":
{
"c2_header": "EMP NAME",
"c2_datatype" : "String"
},
"c3":
{
"c3_header": "DESIGNATION",
"c3_datatype":"String"
}
}
}
}
}
}
此处的工作表计数和部分计数以及列数会因“ws1
”等每个对象而异。我的json中有许多对象,如ws1
。但是在每个部分中,部分计数和列数都有所不同,每个部分和每个列都有json对象,我想得到每个部分和列以及工作表的值,而不重复我的代码。有人可以帮帮我。
答案 0 :(得分:0)
JSJSONObject worksheets=rootobject.getJSONObject("worksheet");
JSONArray worksheetName=rootobject.getJSONArray("worksheetList");
int lenght=worksheetName.lenght();
for(int i=0;i<lenght;i++)
{
JSJSONObject ws=worksheets.getJSONObject(worksheetName.getString(i));
JSONArray sectionList=rootobject.getJSONArray("sectionList");
for(int j=0;j<sectionList.lenght();j++)
{
JSJSONObject section=ws.getJSONObject(sectionList.getString(j));
JSONArray columnList=section.getJSONArray("columnList");
for(int k=0;k<columnList.lenght();k++)
{
JSJSONObject column=ws.getJSONObject(columnList.getString(k));
JSONArray keys = column.names ();
for (int l = 0; l < keys.length (); l++) {
String key = keys.getString (l); // Here's your key
JSJSONObject c1_c2_c3=column.getJSONObject(key);//c1_c2_c3 indicate c1 or c2 or c3
JSONArray c1_c2_c3Keys = c1_c2_c3.names ();
for (int m = 0; m < c1_c2_c3Keys.length (); m++) {
String c1_c2_c3key = c1_c2_c3Keys.getString (m); // Here's your key i.e. c1_header,c1_datatype,c2_header
String header_datatype=c1_c2_c3.getString(c1_c2_c3key);//header_datatype indicate header or datatype i.e. EMP ID ,Numeric
}
}
}
}
}
希望这会对你有所帮助。