2017-01-24 51 views
1

我檢索從PHP文件中的JSON字符串如下:保存JSON字符串文件無法正常工作

async fetchAvailableSets(){ 
    try { 
     let response = await fetch('http://example.com/getdata.php', { 
      method: 'POST', 
      headers: { 
       'Accept': 'application/json', 
       'Content-Type': 'application/json' 
      } 

     }) 
     let responseJson = await response.json() 
     .then((responseData) => { 

      console.log(responseData) /* This displays the JSON text */ 


      RNFS.writeFile(RNFS.DocumentDirectoryPath + 'info.json', responseData) 
      .then(() => { 
       RNFS.readFile(RNFS.DocumentDirectoryPath + 'info.json') 
       .then((contents) => { 
        console.log(contents) /* Nothing is displayed */ 
       }) 
      }) 
     }) 
    } 
    . 
    . 
    . 
} 

如果我改變的WriteFile行:

RNFS.writeFile(RNFS.DocumentDirectoryPath + 'info.json', responseData.toString()) 

那麼所有我寫入文件是「[object Object]」。看起來這是一個字符串,而不是一個合法的對象。

我覺得它不應該將其轉換爲字符串,因爲它已經是一個字符串。另外,爲什麼將它轉換爲字符串使其成爲一個對象,這有點令人困惑。

我在做什麼錯?

+1

如果你做'JSON.stringify(responseData)',它會給你同樣的東西嗎? –

+0

@MattAft謝謝,這工作! – kojow7

回答

0
JSON.stringify(responseData); 

會更合適;

相關問題