2016-08-03 83 views
0

起初我有一個包含字符串數據如何從JavaScript或AngularJS中的數組字符串中獲取數組?

var stringArray = "[{'middleName':'T','lastName':'TEST5LAST','occupation':'QA','homeAddressZipcode':'85001','entitySequenceNumber':7,'homeAddressCity':'PHOENIX','ssn':'111111115','cellPhone':'4807363273','employer':'ABC','workPhoneExtension':'5555','entityType':'JOINT','homePhone':'4807363272','identificationType':'0','email':'[email protected]','workPhone':'4807363274','firstName':'TEST5FIRST','homeAddressStreet':'8000','homeAddressState':'AZ'}]"; 

有沒有在JavaScript或AngularJS的功能,讓我建立一個真正的JavaScript數組是這樣?:

var realArray = [ 
    { 
    "middleName":"T", 
    "lastName":"TEST1LAST", 
    "occupation":"HR", 
    ... 
    }, 
    { 
    "middleName":"T", 
    "lastName":"TEST5LAST", 
    "occupation":"QA", 
    ... 
    }, 
    ... 
]; 
+3

你的樣本串具有一個結束']'但沒有開口'[',順便說一句。 –

+1

AngularJS中的原生函數將是['angular.fromJson(stringArray);'](https://docs.angularjs.org/api/ng/function/angular.fromJson) – mzmm56

+0

是的,S McCrohan,你是對的。這是我的錯字。更正它。 –

回答

1

試試這個變量。

realArray = JSON.parse(stringArray); 
0

JSON.parse(stringArray)返回JSON對象

相關問題