简体   繁体   English

如何将字符串转换为json

[英]how to convert a string to json

I am not able to convert a string to json object format.我无法将字符串转换为 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]

* * * *

If I try to convert string to json object I am getting this error:如果我尝试将字符串转换为 json 对象,我会收到此错误:

Error name:

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

My OUTPUT:我的输出:

"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} 
        ]
} 
]"

As a side note, constructing JSON by hand seems like a bad approach.作为旁注,手动构建 JSON 似乎是一种糟糕的方法。 You should instead build custom javascript data structures and just use JSON.stringify() on it to convert it to JSON您应该改为构建自定义 javascript 数据结构,并在其上使用 JSON.stringify() 将其转换为 JSON

eg:例如:

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

Try removing the d: from your output.尝试从输出中删除d: :。 This is not a valid JSON string.这不是有效的 JSON 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM