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

标签 java android firebase-realtime-database android-json

我从 Firebase 数据库获取 DataSnapshot 作为 JsonObject。我必须将此 JsonObject 转换为 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 中点击按钮时得到的响应:

{ 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?}}} }

最佳答案

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();
        }
    }
}

关于java - 将 FirebaseDatabase jsonObject 转换为 jsonArray,然后将 jsonArray 转换为 .xlsx 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669585/

相关文章:

Android 反向地理编码 getLocality 通常返回 null

java - File#delete 不删除文件

android - 如何使用 Fiddler4 从 Android App 捕获 HTTPS(TLS 1.0) 通信?

java - Selenium Java - 通过 JavascriptExecutor 获取页面源

android - 搜索文本文件的不确定性

swift - 按时间顺序快速排序 Firebase 帖子

ios - Swift 3 中的 UIPickerView 和子类 Array

swift - Firebase 类型 "ViewController"的值没有成员 'ref'

java - 如何设置 Spring Security 以允许来自特定应用程序的 REST 调用?

C# 相当于 Java 的 charAt()?