JavaScript: converting the key value and JSON string without quotation marks into JSON object methods

Encounters problems, it took two days to resolve, deeply hard to come by, so do the following summary, farmers hope to meet code this problem more quickly find a solution!

var jsonArr= [{col:TO_CHAR(HZRQ,'YYYYMM'),type:=,values:201909},{col:RQ,type:=,values:201909}];

You want to convert it into a JSON object. Converted into JSON JSON string objects, use the JSON.parse () method for the above key name without this double quotes JSON string using the JSON.parse () being given parsing, can not resolve. This has become a very troublesome problem. So, try to use standard prevention still good, even though most of the time you will not encounter problems.
Following the key code is double quotes

jsonArr= jsonArr.replace(/(?:\s*['"]*)?([a-zA-Z0-9]+)(?:['"]*\s*)?:/g, "\"$1\":");

Or use the following method: jsonArr Replace ( / (\ S * ? {\ S * ? | \ S * ? , \ S * ? ) ( [ ' "]) ([A-zA-Z0-9] +? ) ([ ' "]?): / G,'. 1 $" $ . 3 " : ' ) ;

Of course, if there is no special symbols for the result value may be replaced with a regular expression above, but for the problems encountered in the current I, value = value contains _ <>, 'special symbol, etc. I have not found a better regular expression to be replaced, they can only think of the most stupid way to solve

jsonArr= jsonArr.replace(/":/g, "\":\"").replace(/,"/g, "\",\"").replace(/},/g, "\"},").replace(/}]/g, "\"}]");

console.log ( "jsonArr =", eval (jsonArr)); or using var json = eval ( '(' + jsonArr + ')');

Case

var jsonArr2 = [{
        "Color": 超窄边IPS,
        "SkuId": 1365289,
        "Size": 27英寸 }, { "Color": 21:9超宽IPS曲面, "SkuId": 1742140, "Size": 29英寸 }, { "Color": 17英寸(TN,5:4方屏), "SkuId": 1032147, "Size": 17英寸 }];

Because the content with the ":"

  1. (\w+):, Mishandled"21:9超宽IPS曲面"

  2. [{,]\s*(\w+):, Mishandled"17英寸(TN,5:4方屏)"

So for more than json string you will need to use the most stupid way to increase the value of the value in double quotes

 

 

 

Guess you like

Origin www.cnblogs.com/zhangliang88/p/11720736.html