简体   繁体   English

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

[英]How to fix quotation marks to ajax json? (correct stringify)

I have JSON like below, how to make it correct with javascript or some php script? 我有如下所示的JSON,如何使用javascript或一些php脚本使其正确? I think it should have quotation marks on arrays. 我认为它应该在数组上带有引号。

like 喜欢

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

My AJAX/Javascript is like below 我的AJAX / Javascript如下所示

$.ajax({
    url: "http://domain/corsproxy?url=http://json.json/json.json",
    type: "GET",
    timeout: 3000,
    dataType: "json",
    success: function(parse) {
        var strHtml = '';
        $.each(parse, function(key, value) {
            strHtml += ' <span class="location">' + value.title + '</span><br />';
        });

        document.getElementById("results").innerHTML =
            strHtml;
    },
    error: function(jqXHR, exception) {
        console.log(jqXHR);
    }
});

You can fix the json using this regex:- 您可以使用此正则表达式修复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

To convert an javascript object to a valid JSON string all you need is: 要将javascript对象转换为有效的JSON字符串,您需要做的是:

JSON.stringify(yourObject) JSON.stringify JSON.stringify(yourObject) JSON.stringify

So in your example: 因此,在您的示例中:

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

Try to use JSON.stringify to convert it to json string. 尝试使用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); }); 
 <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> 

See this jsfiddle: 看到这个jsfiddle:

https://jsfiddle.net/rqmk4sx5/ https://jsfiddle.net/rqmk4sx5/

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

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

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

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