如何修复ajax json的引号? (正确的字符串化)

时间:2016-09-13 10:40:27

标签: javascript php jquery json ajax

我有如下JSON,如何使用javascript或某些PHP脚本使其正确?我认为它应该在数组上有引号。

喜欢

"a|b|c"

我的AJAX / Javascript如下所示

global winsTotal

4 个答案:

答案 0 :(得分:0)

您可以使用此正则表达式修复json: -

result = string.replace(/\s*\n*\\+/g, ''); // Removes the spaces 
result = result.replace(/([a-z0-9A-Z][^:"},]*)(?=\s*:)/g, '"$1"'); // Puts quotation marks around keynames of the object

答案 1 :(得分:0)

要将javascript对象转换为有效的JSON字符串,您只需:

JSON.stringify(yourObject) JSON.stringify

所以在你的例子中:

var yourObject = { 1: [ {id:"171113524", title:"xxxx", category:"4",
start:"20160913062500", stop:"20160913093000"} , {id:"171115415",
title:"xxxx", category:"1",
start:"20160913093000", stop:"20160913100000"} , {id:"171115421",
title:"xxxx", category:"2", start:"20160913100000",
stop:"20160913104702"} , {id:"171115471", title:"xxxx", 
category:"6", start:"20160913104702",
stop:"20160913110000"} , {id:"17111049", title:"xxxx",
category:"4", start:"20160913110000", stop:"20160913110500"} ,
{id:"17111335", title:"xxxx", category:"4",
start:"20160913110500", stop:"20160913111200"} , {id:"17111354",
title:"xxxx", category:"4",
start:"20160913111200", stop:"20160913111900"} ] };


var yourJSON = JSON.stringify(yourObject);
console.log(yourJSON);


{"1":[{"id":"171113524","title":"xxxx","category":"4","start":"20160913062500",   "stop":"20160913093000"},{"id":"171115415","title":"xxxx","category":"1","start":"20160913093000","stop":"20160913100000"},{"id":"171115421","title":"xxxx","category":"2","start":"20160913100000","stop":"20160913104702"},{"id":"171115471","title":"xxxx","category":"6","start":"20160913104702","stop":"20160913110000"},{"id":"17111049","title":"xxxx","category":"4","start":"20160913110000","stop":"20160913110500"},{"id":"17111335","title":"xxxx","category":"4","start":"20160913110500","stop":"20160913111200"},{"id":"17111354","title":"xxxx","category":"4","start":"20160913111200","stop":"20160913111900"}]}"`

答案 2 :(得分:0)

尝试使用JSON.stringify将其转换为json字符串。



    $(function () {
        //define a json object
        var employee = { name: "Test Name", street: "Test Street", phone: "111 2222222" };

        //use JSON.stringify to convert it to json string
        var jsonstring = JSON.stringify(employee);
        $("#result").append('<p>json string: ' + jsonstring + '</p>');

        //convert json string to json object using JSON.parse function
        
        var jsonobject = JSON.parse(jsonstring);
        var info = '<ul><li>Name:' + jsonobject.name + '</li><li>Street:' + jsonobject.street + '</li><li>Phone:' + jsonobject.phone + '</li></ul>';

        $("#result").append('<p>json object:</p>');
        $("#result").append(info);
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" runat="server">
    <div>
        <div id="result"></div>
    </div>
</form>
&#13;
&#13;
&#13;

见这个jsfiddle:

https://jsfiddle.net/rqmk4sx5/

答案 3 :(得分:0)

你可以通过以下方式修复它:

var obJ = { "1":[ {"id":"171113524", "title":"xxxx", "category":"4", { 1:[ {id:"171113524", title:"xxxx", category:"4", start:"20160913062500", stop:"20160913093000"} , {id:"171115415", title:"xxxx", category:"1", start:"20160913093000", stop:"20160913100000"} , {id:"171115421", title:"xxxx", category:"2", start:"20160913100000", stop:"20160913104702"} , {id:"171115471", title:"xxxx ", category:"6", start:"20160913104702", stop:"20160913110000"} , {id:"17111049", title:"xxxx", category:"4", start:"20160913110000", stop:"20160913110500"} , {id:"17111335", title:"xxxx", category:"4", start:"20160913110500", stop:"20160913111200"} , {id:"17111354", title:"xxxx", category:"4", start:"20160913111200", stop:"20160913111900"}

var newJson = JSON.stringify(obJ); console.log(newJson);