2016-10-18 192 views
-1

我無法將字符串轉換爲json對象格式。如何將字符串轉換爲json

var objvaluecon = []; 
var resultvalue = "d: "; 
resultvalue += "["; 
var objectcreation = [];   
b = 1; 
for (j = 0; j <= Object.keys(uniqueArray).length; j++) {    
    arrayvaluearr = uniqueArray[j]; 
    if (typeof arrayvaluearr != "undefined") {    
     k = 1; 
     resultvalue += "{ key: '" + uniqueArray[j] + "', values: ["; 
     for (i = 0; i < Object.keys(obj).length; i++) { 
      var objvaluecon = [i]; 
      //console.log(Object.keys(obj[i]).length); 
      $.each(obj[i], function (key, value) { 
       if (value == uniqueArray[j]) { 
        if (totalsum[j] == k) { 
         resultvalue += "{ key: '" + obj[i]["SecLevelFailurRes"] + "', value: " + obj[i]["percentage"] + "} "; 
         console.log(value + " : " + obj[i]["SecLevelFailurRes"] + " : " + obj[i]["percentage"]); 
        } else { 
         resultvalue += "{ key: '" + obj[i]["SecLevelFailurRes"] + "', value: " + obj[i]["percentage"] + "}, "; 
         console.log(value + " : " + obj[i]["SecLevelFailurRes"] + " : " + obj[i]["percentage"]); 
        }        
        k++; 
       } 
      }); 
     } 

     if (b == Object.keys(uniqueArray).length) { 
      resultvalue += "]} "; 
     } else { 
      console.log(b); 
      console.log(Object.keys(uniqueArray).length); 
      resultvalue += "]}, "; 
     }     
     b++; 
    } 


    } 
    resultvalue += "]"; 

[Output result Screen shot][1] 

* *

如果我試圖將字符串轉換爲JSON對象,我收到此錯誤:

Error name: 

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 4 of the JSON data 
[Learn More] 

我的輸出:

"d:[ 
{ 
    key: 'Delay in Delivery [1.82%, 10]', 
    values: 
    [ 
     { key: 'Above 3 days delay', value: 0.18}, 
     { key: 'High delivery timelines', value: 1.27}, 
     { key: 'Less than 3 days delay', value: 0.18}, 
     { key: 'Over 10 days delay', value: 0.18} 
    ] 
}, 
{ 
    key: 'Lack of Collection [36.91%, 203]', 
    values: 
     [ 
      { key: 'Better collections with competitors', value: 0.36}, 
      { key: 'Limited colours', value: 6.91}, 
      { key: 'Limited Designs', value: 8.55}, 
      { key: 'Limited fabric options', value: 10.55}, 
      { key: 'Limited readymade collections', value: 5.09}, 
      { key: 'Readymade clothes unavailable', value: 0.73}, 
      { key: 'Sizes are unavailable', value: 2.18}, 
      { key: 'Stock not available', value: 2.55} 
     ] 
}, 
{ 
    key: 'Offers related issues [18.55%, 102]', 
    values: 
     [ 
      { key: 'Cashback not received', value: 0.91}, 
      { key: 'Less offers and discounts', value: 1.27}, 
      { key: 'No Offers or discounts', value: 12.91}, 
      { key: 'Non transparent charges/conditions', value: 3.09}, 
      { key: 'Unable to redeem points/coupons with offers', value: 0.36} 
     ] 
}, 
{ 
    key: 'Unhappy with Product quality [16.18%, 89]', 
    values: 
    [ 
     { key: 'Colour faded', value: 6.18}, 
     { key: 'Poor Fabric quality', value: 9.64}, 
     { key: 'Poor readymade clothes', value: 0.36} 
    ] 
}, 
{ 
    key: 'Staff not helpful [5.82%, 32]', 
    values: 
     [ 
      { key: 'Complaint unresolved', value: 1.45}, 
      { key: 'Rude or unprofessional behaviour', value: 0.91}, 
      { key: 'Staff did not follow instructions', value: 0.18}, 
      { key: 'Unhappy with staff response', value: 3.27} 
     ] 
}, 
{ 
    key: 'Prices related issue [16.00%, 88]', 
    values: 
     [ 
      { key: 'Demanded for more pay', value: 0.36}, 
      { key: 'Higher prices', value: 9.82}, 
      { key: 'Higher prices compared to Competitors', value: 4.18}, 
      { key: 'Higher prices for stitching', value: 1.45}, 
      { key: 'Limited fabric options', value: 0.18} 
     ] 
}, 
{ 
    key: 'Reasons beyond TAS control [14.00%, 77]', 
    values: 
     [ 
      { key: 'Did not have the need', value: 3.09}, 
      { key: 'Far away from my place', value: 0.73}, 
      { key: 'Financial constraints', value: 1.27}, 
      { key: 'Have purchased enough last time', value: 0.55}, 
      { key: 'I did visit recently', value: 1.64}, 
      { key: 'I was occupied', value: 1.09}, 
      { key: 'I was travelling', value: 0.73}, 
      { key: 'No occasion to buy', value: 0.73}, 
      { key: 'Staying at different city now', value: 4.18} 
     ] 
}, 
{ 
    key: 'Stitching was unsatisfactory [19.27%, 106]', 
    values: 
     [ 
      { key: 'Loose fit', value: 2.73}, 
      { key: 'Poor stitching quality', value: 7.09}, 
      { key: 'Tailoring service unavailable', value: 0.36}, 
      { key: 'Tight fit', value: 2.91}, 
      { key: 'Unhappy with fit', value: 6.18} 
     ] 
}, 
{ 
    key: 'Marketing related [2.55%, 14]', 
    values: 
     [ 
      { key: 'More advertisement to be done', value: 0.18}, 
      { key: 'Unaware of a store nearby', value: 2.36} 
     ] 
} 
]" 
+0

錯誤告訴你到底什麼是錯的。你在第1行第4列丟失了一個大括號。 – TheValyreanGroup

+0

一個問題......'j <='應該是'j <' –

+0

似乎也有很多隱式全局變量,除非聲明在其他地方。 –

回答

0

作爲一個側面說明,手工構建JSON似乎是一種糟糕的做法。而應該建立自定義JavaScript數據結構和只使用JSON.stringify()就可以將其轉換成JSON

如:

var myDataStructure = {}; 
// Build some custom keys    
for (x=1; x<3; x++) { 
    myDataStructure["key"+x]=x; 
} 
// Add a custom array type key  
myDataStructure["anArrayKey"]=[1,2,3]; 

// Result would be -> {"key1":1,"key2":2,"anArrayKey":[1,2,3]} 
var asJson = JSON.stringify(myDataStructure); 
0

嘗試從你的輸出取出d:。這不是有效的JSON字符串。