Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试将嵌套JSON写入文件_Java_Android_Json_File_Stack Overflow - Fatal编程技术网

Java 尝试将嵌套JSON写入文件

Java 尝试将嵌套JSON写入文件,java,android,json,file,stack-overflow,Java,Android,Json,File,Stack Overflow,这是我在这里的第一篇文章 嗯,我在Android中尝试将嵌套JSON写入文件时遇到了一个问题。 它显示了一个java.lang.StackOverflowerError 我的问题是:是否有任何方法可以将嵌套的JSON写入到文件中而不会出现任何堆栈问题 我将向您展示嵌套的JSON以及我试图写入文件的方式 JSONObject fingerPrint = new JSONObject(); try { fingerPrint.put("fingerPrint", f

这是我在这里的第一篇文章

嗯,我在Android中尝试将嵌套JSON写入文件时遇到了一个问题。 它显示了一个
java.lang.StackOverflowerError

我的问题是:是否有任何方法可以将嵌套的JSON写入到文件中而不会出现任何堆栈问题

我将向您展示嵌套的JSON以及我试图写入文件的方式

    JSONObject fingerPrint = new JSONObject();

    try {
        fingerPrint.put("fingerPrint", fingerPrint);
    } catch (JSONException e) {
        Log.v(TAG, "Error parsing creating JSON \"fingerPrint\"");
        e.printStackTrace();
    }


    JSONObject fingerPrintMaterial = new JSONObject();

    try {
        fingerPrintMaterial.put("methodType", FingerprintFacade.MAC_SHA256);
        fingerPrintMaterial.put("keySize", 5);
    } catch (JSONException e) {
        Log.v(TAG, "Error parsing creating JSON \"fingerPrintMaterial\"");
        e.printStackTrace();
    }


    JSONObject userFingerPrint = new JSONObject();

    try {
        userFingerPrint.put("fingerPrint",  fingerPrint);
        userFingerPrint.put("fingerPrintMaterial", fingerPrintMaterial);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
这就是我试图写的方式:

    try {
        BufferedWriter file=new BufferedWriter(new FileWriter(pathToNonEncryptedTxt+filename, true));
        //FileWriter file = new FileWriter(pathToNonEncryptedTxt+filename);
        file.write(userDigitalContent.toString(2));
        file.newLine();
        file.write(userFingerPrint.toString(2));
        file.flush();
        file.close();
    } catch (IOException e) {
        Log.v(TAG, "Error saving JSON into "+filename);
        e.printStackTrace();
    } catch (JSONException e) {
        Log.v(TAG, "Error saving JSON into "+filename);
        e.printStackTrace();
    }
哦!!顺便说一句,我需要将类似的内容输出到.txt文件中:

    {
    "userFingerPrint": {
            "fingerPrint": {
                "fingerPrint": "fingerPrintValue"
            },
            "fingerPrintMaterial": {
                 "methodType": "methodTypeValue",
                 "keySize": "5"
            }
}

我想在代码中创建
JSONObject
是有问题的。有关创建
JSONObject
的信息,请参阅此部分。一旦创建
JSONObject
,您可以通过调用
JSONObject.toString(
)从
JSONObject
获取Sting然后将该字符串写入文件

您想要实现什么?您可以输入您需要的输出类型吗?实际上,我遵循了该教程,我认为创建JSON没有错。无论如何,该教程使用一个简单的JSON(带有JSONArray),而不是我在代码中使用的嵌套JSON。无论如何谢谢你!当你遇到异常时,试着打印JSONObject并检查它的创建是否正确当你试图将同一个对象放在同一个对象中时。试试{fingerPrint.put(“fingerPrint”,fingerPrint);}catch(JSONException e){Log.v(TAG,“创建JSON时出错\“fingerPrint\”);e.printStackTrace();}哇……谢谢……你解决了我的问题。我知道这是一个荒谬的问题,但它困扰了我好几天…谢谢大家!!如果这已经解决了你的问题,请接受这个答案