2017-12-16 36 views
0

Tizen 프로젝트 (모바일 용)의 기존 JSON 파일에 새 정보를 쓰고 싶습니다. 나는 그것이 PHP (또는 Node.js)없이 가능하지 않으며 Tizen이 PHP를 지원하지 않는다는 것을 발견했다. PHP없이 (그리고 로컬 DB를 만들지 않고) JSON에 새로운 데이터를 보내는 방법이 있습니까?Tizen Studio의 JSON 파일에 새 정보를 쓰는 방법

+0

당신이 필요로하는 모든

FileSystem Guide는 https://developer.tizen.org/community/code-snippet/web-code-snippet/readwrite-file 그것은 작동 –

답변

0

예, Tizen FileSystem API 및 JSON.parse()의 사용은 가능합니다. & JSON.stringify().

enter image description here

체크 아웃 안내선 및 구현에 대한 API 참조 :

var res,file,text,jsonInit,obj,jsonString; 

function createFile(){ 
    tizen.filesystem.resolve("documents", function(dir) { 
     res = dir.createDirectory("res"); 
     file = res.createFile("data.json"); 

     file.openStream(
       "w", 
       function(fs) { 
        jsonInit = '{"data1":"a","data2":"b"}'; 
        fs.write(jsonInit); 
        alert("JSON file Created"); 
        fs.close(); 
       }, function(e) { 
        console.log("Error " + e.message); 
       }, "UTF-8"); 
     }); 
    } 
function addInfo(){ 
    tizen.filesystem.resolve("documents", function(dir) { 
      file = dir.resolve("res/data.json"); 
      file.openStream(
      "rw", 
      function(fs) { 
       text = fs.read(file.fileSize);    
       var obj = JSON.parse(text); 
       obj.data3 = 'c'; 
       jsonString = JSON.stringify(obj); 
       fs.position = 0; 
       fs.write(jsonString); 
       fs.close(); 
       alert("New Info added on data3 key");   
      }, function(e) { 
       console.log("Error " + e.message); 
      }, "UTF-8"); 
     }); 
    } 

function readFromFile(){ 
    tizen.filesystem.resolve("documents", function(dir) 
      { 
       var file = dir.resolve("res/data.json"); 
       file.openStream(
        "r", 
        function(fs) { 
         text = fs.read(file.fileSize);  
         fs.close(); 
         obj = JSON.parse(text); 
         alert("Test read --> value on data2:" +obj.data2); 
         alert("Test read --> value on data3:" +obj.data3); 
        }, function(e) { 
         console.log("Error " + e.message); 
        }, "UTF-8"); 
      }); 
    } 

테스트 데모 [Chris G으로 코멘트에 명시된].

FileSystem API References

+0

하지만 내가 쓰고 싶은 Tizen Project의 기존 파일에 추가합니다. 파일에는 '././my_file.json'과 같은 경로가 있습니다. 코드에서 경로를 변경하려고했지만 작동하지 않았습니다. – user242336

+0

JSON 파일은 어디에 있습니까? Tizen-Studio에서 응용 프로그램의 Project Dir에 있습니까? 또는 Tizen 장치의 FileSystem? Tizen Mobile 장치의 FileSystem에서 디렉토리 경로를 공유하십시오. –