2017-04-26 142 views
-2

在json中转换数组javascript在javascript中如何转换数组javascript javascript json

[{ 
    "userName": "Rodrigo", 
    "date": "25 de abril de 2017", 
    "score": 5 
}] 
[{ 
    "userName": "Jon", 
    "date": "24 de abril de 2016", 
    "score": 4 
}] 

错误:在第5行

我的代码

var result = []; 
var allInfo = { 
    userName, 
    date, 
    score 
}; 
result[i] = allInfo; 
return JSON.stringify(result); 
+0

等等,什么?不应该被_to_? –

+2

这已经是一个JSON数组.... –

+0

可能重复[转换数组到JSON](http://stackoverflow.com/questions/2295496/convert-array-to-json) –

回答

1

在假设您所提供的例子是无效的,你要一个JavaScript对象到JSON转换,使用分析器错误JSON.stringify()

var test = [{ 
 
    "userName": "Rodrigo", 
 
    "date": "25 de abril de 2017", 
 
    "score": 5 
 
}, { 
 
    "userName": "Jon", 
 
    "date": "24 de abril de 2016", 
 
    "score": 4 
 
}]; 
 

 
console.log(JSON.stringify(test));