[英]Convert FirebaseDatabase jsonObject to jsonArray and then that jsonArray to .xlsx format
I'm getting DataSnapshot from a Firebase database as a JsonObject
.我从 Firebase 数据库中获取 DataSnapshot 作为JsonObject
。 I have to convert this JsonObjec
t into JsonArray and then that JsonArray into Excel format and then download that file into mobile storage.我必须将此JsonObjec
t 转换为 JsonArray,然后将该 JsonArray 转换为 Excel 格式,然后将该文件下载到移动存储中。 How can I do this?我怎样才能做到这一点?
Here I am getting the DataSnapshot
on button click:在这里,我在按钮单击时获取DataSnapshot
:
btnJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
long a = dataSnapshot.getChildrenCount();
System.out.println("lc" + a);
for (DataSnapshot ds : dataSnapshot.getChildren()) {
System.out.println("response1" + ds);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
Here is the response I am getting on button click in jsonObject:这是我在 jsonObject 中单击按钮时得到的响应:
{ key = 13uhnjvczw, value = {survey_title=Sports, questions={4={type=2, title=Who is the top goal scorer in La Liga?}, 0={options={0=13, 1=5}, type=1, title=How many ucl has real madrid won}, 1={options={0=Real Madrid fc, 1=Liverpool fc}, type=1, title=Who is the current UCL champion?}, 3={type=2, title=Who is the top goal scorer in UCL?}, 2={options={0=Cristiano Ronaldo, 1=Zinedine Zidane}, type=1, title=Who is the all time top scorer of Real Madrid?}}} } { key = 13uhnjvczw, value = {survey_title=Sports, questions={4={type=2, title=谁是西甲最佳射手?}, 0={options={0=13, 1=5} , type=1, title=皇马赢了多少个ucl}, 1={options={0=皇马, 1=利物浦}, type=1, title=谁是现在的UCL冠军?}, 3 ={type=2, title=谁是 UCL 的最佳射手?}, 2={options={0=Cristiano Ronaldo, 1=Zinedine Zidane}, type=1, title=谁是历史最佳射手皇家马德里?}}} }
public void saveCsv(JSONArray outerArray) throws IOException, JSONException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
String fileName = referenceNo + " Result";
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/";
File dir = new File(rootPath);
if (!dir.exists()) {
dir.mkdir();
}
File file = null;
file = new File(rootPath, fileName);
if (!file.exists()) {
progressDialog.dismiss();
file.createNewFile();
}
if (file.exists()) {
progressDialog.dismiss();
CSVWriter writer = new CSVWriter(new FileWriter(file), ',');
for (int i = 0; i < outerArray.length(); i++) {
JSONArray innerJsonArray = (JSONArray) outerArray.getJSONArray(i);
arrayOfArrays[k] = stringArray1;
writer.writeNext(arrayOfArrays[k]);
System.out.println("aa " + Arrays.toString(arrayOfArrays[k]));
}
}
writer.close();
Toast.makeText(this, fileName + " is been saved at " + rootPath, Toast.LENGTH_LONG).show();
}
}
}
File csvFile = new File(directory, Utils.CSV_FILE_NAME);
try
{
csvFile.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(csvFile));
String heading[] = {"vid", "vnumber", "vname", "vyear", "fcdate", "insdate",
"perdate", "gtax", "polcer"};
csvWrite.writeNext(heading);
for (int i = 0; i < arrayList.size(); i++)
{
YourModel model = arrayList.get(i);// You can also use JsonObject from Json array.
String arrStr[] = {String.valueOf(model.getVid()),
String.valueOf(model.getVnumber()), model.getVname(), model.getVyear(),
model.getFcdate(), model.getInsdate(), model.getPerdate(),model.getGtax(),
model.getPolcer()
csvWrite.writeNext(arrStr);
}
csvWrite.close();
} catch (Exception sqlEx)
{
Log.e(TAG, sqlEx.getMessage(), sqlEx);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.