2015-10-15 175 views
1

我使用JavaScript和循環通過提交的表單的值,並試圖建立一個兒子對象的形式值。javascript將JSON字符串轉換爲JSON對象

這是最終目標的一個例子,我需要:

{ 
    "DataObject": { 
    "user": { "-name": "username" }, 
    "contentFile": { 
     "-filename": "Breaking_News", 
     "lock": { "-fileIsBeingEdited": "false" }, 
     "content": { 
     "line": [ 
      { 
      "-index": "1", 
      "-text": "this is the header" 
      }, 
      { 
      "-index": "2", 
      "-text": "this is the first line" 
      }, 
      { 
      "-index": "3", 
      "-text": "this is the second line" 
      } 
     ] 
     } 
    } 
    } 
} 

到目前爲止,我加入了所有這些數據的字符串作爲,這似乎是我可以插入表單值的唯一方法(在線陣列)到對象的中間。

var jsonStr = '{' 
     + 'iceteaDataObject: {' 
     + 'user: {"-name": "hindsc52"},' 
      + 'contentFile: {' 
      + '"-filename": "Ticker",' 
      + 'lock: { "-fileIsBeingEdited": "false" },' 
      + 'content: {' 
      + 'line: [' 

    for(var i = 0; i < elem.length; i++) { 
     if(!elem[i].value == '') { 
     jsonStr += '{' 
     jsonStr += "-index: " + i + ','; 
     jsonStr += "-text: " + elem[i].value; 
     jsonStr += '},' 
     } 
    } 

    jsonStr += ']}}}}'; 

    console.log(JSON.parse(jsonData)); 

但是,當運行這個我得到的錯誤:意外的令牌'我'。

我試圖用力地使用,但只是再次輸出整個sting。

+0

檢查字符串的格式,這是不正確。將代碼粘貼到JSONLint中並查看錯誤 – Tushar

+0

因爲您的第一條指令沒有結束,所以您需要在'for'循環之前添加一個額外的';'。 –

+0

你不應該這樣創建一個JSON字符串。如果表單字段的值包含''',那麼它將會失敗,除了JSON格式的鍵需要被'''引用「,所以你根本不會創建一個有效的JSON字符串。意想不到的'i'是因爲你的鍵'iceteaDataObject'沒有被引用。創建一個普通的JavaScript對象並使用['JSON.stringify'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) –

回答

1

你並不需要或想要JSON對於這一點,只是生成對象:

// Sample data 
 
var elem = [{ 
 
    value: "one" 
 
}, { 
 
    value: "two" 
 
}]; 
 

 
// Build the object 
 
var obj = { 
 
    "DataObject": { 
 
    "user": { 
 
     "-name": "username" 
 
    }, 
 
    "contentFile": { 
 
     "-filename": "Breaking_News", 
 
     "lock": { 
 
     "-fileIsBeingEdited": "false" 
 
     }, 
 
     "content": { 
 
     "line": [] 
 
     } 
 
    } 
 
    } 
 
}; 
 
var line = obj.DataObject.contentFile.content.line; 
 
elem.forEach(function(entry, index) { 
 
    if (entry.value != '') { 
 
    line.push({ 
 
     "-index": index, 
 
     "-text": entry.value 
 
    }); 
 
    } 
 
}); 
 

 
// Show result: 
 
document.body.innerHTML = 
 
    "<pre>" + 
 
    JSON.stringify(obj, null, 2) + 
 
    "</pre>";


附註:你不檢查空字符串是這樣的:

if (!entry.value == '') { // <== Incorrect 

您可以使用:

if (entry.value != '') { 

或:

if (entry.value) { 
+1

謝謝你這個完美的工作我需要的。 – chinds

0

你不應該建立JSON這樣的,但使用JSON.stringify()(見MDN doc)代替:

var myObject={foo:"bar"}; 
var myJSON=JSON.stringify(myObject); 
console.log(myJSON); //echo {"foo":"bar"} 
0

這裏是另一種方法:

var json = { 
    iceteaDataObject: { 
    "-name": "hindsc52" 
    }, 
    contentFile: { 
    "-filename": "Ticker", 
    lock: { "-fileIsBeingEdited": "false" }, 
    content: {line: []} 
    } 
} 
for(var i = 0; i < elem.length; i++) { 
     if(!elem[i].value == '') { 
     json.contentFile.content.line.push({"-index": i,"-text": elem[i].value } 
     } 
    } 
var jsonStr = JSON.stringify(json); 
0

您需要將所有的鍵放在引號f或者這個工作。正如其他人指出的那樣,你並不是真的應該這樣做。

如果你還想做你的方式,試試這個:

var jsonStr = '{' 
     + '"iceteaDataObject": {' 
     + '"user": {"-name": "hindsc52"},' 
     + '"contentFile": {' 
     + '"-filename": "Ticker",' 
     + '"lock": { "-fileIsBeingEdited": "false" },' 
     + '"content": {' 
     + '"line": [' 

for(var i = 0; i < elem.length; i++) { 
    if(!elem[i].value == '') { 
    jsonStr += '{' 
    jsonStr += '"-index": ' + i + ','; 
    jsonStr += '"-text": ' + '"' + elem[i].value + '"'; 
    jsonStr += '},' 
    } 
} 

jsonStr += ']}}}}'; 
相關問題