将字符串转换为JavaScript中的对象

时间:2019-05-11 01:46:39

标签: javascript

我有以下数据:{来自:“玩家”,至:“团队”,颜色:“黑色”,破折号:[3,2],arr:“ LineFork”}},我使用Javascript从textFile中检索到

当我松开它时,它被视为String。但我需要它成为一个对象 PS:无需将其转换为{“ from”:“玩家”,“ to”:“团队”,“ color”:“黑色”,“破折号”:[3,2],“ arr”:“ LineFork”}} 我需要保持原样。因为我应该将其推入数组

这是代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
<script>
var linkDataArray =[       
        { from: "awards_coaches", to: "coaches" ,color: "black", dash: [3, 2] , arr:"LineFork"},
]
</script>

<body>

<div id="sample">
  <input type="file" id="openFile"/>
<br>
<pre id="fileContents"></pre>
<script>
     document.getElementById('openFile').addEventListener('change',function(){
    var fr=new FileReader();
    fr.onload= function(){      
        var t=this.result;
        var object=t.split('},');
        for(var index in object) {
linkDataArray.push(object[index]);
}


    }
    fr.readAsText(this.files[0]);

});

</script>

</div> 

</body>

我收到此错误: 未捕获的错误:GraphLinksModel.linkDataArray仅包含对象,而不包含:{from:“ awards_coaches”,to:“ coachs”,color:“ black”,破折号:[3,2],arr:“ LineFork”}

有人知道该怎么做? b

0 个答案:

没有答案