将FirebaseDatabase jsonObject转换为jsonArray,然后将该jsonArray转换为.xlsx格式

问题描述 投票:3回答:2

我正在从Firebase数据库中以JsonObject的形式获取DataSnapshot。我必须将此JsonObjec t转换为JsonArray,然后将该JsonArray转换为Excel格式,然后将该文件下载到移动存储中。我该怎么办?

这里是单击按钮的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) {
                }
            });
        }
    });

这是我在jsonObject中单击按钮时得到的响应:

{键= 13uhnjvczw,值= {survey_title =体育,Question = {4 = {type = 2,title =西甲最佳射手是谁?},0 = {选项= {0 = 13,1 = 5},类型= 1,标题=多少ucl具有真正的马德里赢了},1 = {选项= {0 =皇家马德里足球俱乐部,1 =利物浦足球俱乐部},类型= 1,title =谁是当前的UCL冠军?},3 = {type = 2,title =谁是UCL中的最佳进球手?},2 = {选项= {0 =克里斯蒂亚诺·罗纳尔多,1 =泽尼丁Zidane},类型= 1,标题=谁是Real的历史最佳射手马德里?}}}}

java android firebase-realtime-database android-json
2个回答
1
投票
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();
        }
    }
}

-1
投票
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.