2014-03-05 3 views
0

에 쓰려고합니다. 내 첫 번째 게시물입니다.중첩 된 JSON을 파일

안드로이드의 File에 중첩 된 JSON을 쓰려고 할 때 문제가 있습니다. 내게는 java.lang.StackOverflowError이 표시됩니다.

제 질문은 : 스택에 문제가 발생하지 않고 파일에 중첩 된 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(); 
    } 

오 !! !! BTW, 나는 .txt 파일에 같은 출력 뭔가가 필요합니다

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

을 파일에 쓸? – Prateek

+0

필요한 출력 유형을 입력 할 수 있습니까? – Solution

답변

0

내가 코드에 JSONObject를 만드는 잘못된 어떤 일이있을 것 같아요. 당신은 당신이 jsonObj.toString(를 호출하여 JSONObject에서 찌르기를 얻을 수 JSONObject를 작성하면 JSONObject를 만들기위한이 tutorial을 참조)하고 해당 문자열이 무엇을 달성하고자 할

+0

사실 나는이 튜토리얼을 따라 왔으며 JSON을 만드는 것은 잘못된 것이 없다고 생각합니다. 어쨌든이 튜토리얼은 간단한 JSON (JSONArray 포함)을 사용하지만 중첩 된 코드는 내 코드에서 사용하지 않는다. 어쨌든 고마워! – user3382975

+0

어떤 점에서 예외가 발생하면 JSONObject를 인쇄하여 제대로 작성되었는지 확인하십시오. –

+0

이 시점에서 수행중인 작업은 동일한 객체에 같은 객체를 넣으려고합니다. 시도 { fingerPrint.put ("fingerPrint", fingerPrint); } catch (JSONException e) { Log.v (TAG, "JSON 생성 중 구문 분석 오류 \"fingerPrint \ ""); e.printStackTrace(); } –

관련 문제