2017-06-22 64 views
0

串到JSON的模塊不能正常工作

var str2json = require('string-to-json');   
var data={ 
      "GTIN" : "GTIN 3", 
      "Target Market" : "Target Market 3", 
      "Global Location Provider Name(GLN) 3" : "Global Location Provider Name(GLN) 3", 
      "Information Provider Name 3" : "Information Provider Name 3", 
      "Product Overview" : "Product Overview 3", 
     } 

我使用str2json模塊組數據,我當我使用var output = str2json.convert(data);它不是爲我工作,但如果將其換算成JSON對象, 我使用

var output = str2json.convert({ 
      "GTIN" : "GTIN 3", 
      "Target Market" : "Target Market 3", 
      "Global Location Provider Name(GLN) 3" : "Global Location Provider Name(GLN) 3", 
      "Information Provider Name 3" : "Information Provider Name 3", 
      "Product Overview" : "Product Overview 3", 
     }) 

它是工作的罰款,並給予了把這樣

{ GTIN: 'GTIN 3', 
    'Target Market': 'Target Market 3', 
    'Global Location Provider Name(GLN) 3': 'Global Location Provider Name(GLN) 3', 
    'Information Provider Name 3': 'Information Provider Name 3', 
    'Product Overview': 'Product Overview 3' } 

那麼什麼是錯誤與var output = str2json.convert(data);

+0

'data'看起來不像字符串。它看起來像一個有效的數據對象。 –

+0

'這不適合我'這是什麼意思?有錯誤嗎?如果是這樣,哪些錯誤? –

+0

閱讀文檔https://www.npmjs.com/package/string-to-json後,我不認爲你需要'string-to-json'庫。你的鑰匙中沒有任何點。 –

回答

0

對於字符串到JSON的轉換,你可以使用內置的JSON.parse()來參考:JSON -Documentation

更多在數據對象在你的代碼是一個有效的JSON對象(如果尾隨逗號被刪除)。

{ "GTIN": "GTIN 3", "Target Market": "Target Market 3", "Global Location Provider Name(GLN) 3": "Global Location Provider Name(GLN) 3", "Information Provider Name 3": "Information Provider Name 3", "Product Overview": "Product Overview 3" }