如何将字符串转换为json

时间:2016-10-18 17:00:14

标签: javascript json json.net

我无法将字符串转换为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} 
        ]
} 
]"

2 个答案:

答案 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);

答案 1 :(得分:0)

尝试从输出中删除d:。这不是有效的JSON字符串。