2017-07-28 27 views
-3

如果我的JSON字符串太大,它不会在我的javascript中验证。例如,如果服务器返回15个结果,它将会失败。 如果它只返回10,它将被验证。JSON over 7k将无法验证

这里是失败的JSON

{ 
    "results":[ 
     { 
     "mls":"20480", 
     "address":"518 WALNUT", 
     "dbcity":"ALTA", 
     "dbstate":"IL", 
     "zip":"61414", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Great 1880 farm house with many updates including half circle screened back deck wants TLC. Large above ground pool", 
     "srcDB":"spi", 
     "beds":"4", 
     "baths":"1" 
     }, 
     { 
     "mls":"6145", 
     "address":"1074 Spoden", 
     "dbcity":"GALE", 
     "dbstate":"Il", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"This 3BR 2BA custom Ranch is located in Lawndale Acres. Features include", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"2" 
     }, 
     { 
     "mls":"6146", 
     "address":"529 SCOTCH ELM", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Enticing 3BD 2BA Tri-level home situated on a large", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"1" 
     }, 
     { 
     "mls":"157186", 
     "address":"277 WALNUT AVE", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"2 bedroom 1 bath bungalow. Spacious kitchen with pantry", 
     "srcDB":"spi", 
     "beds":"2", 
     "baths":"1" 
     }, 
     { 
     "mls":"6148", 
     "address":"404 Second", 
     "dbcity":"MONM", 
     "dbstate":"Il", 
     "zip":"61462", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"This 1895 Queen Anne Victorian near Monmouth College has an open staircase", 
     "srcDB":"spi", 
     "beds":"4", 
     "baths":"2" 
     }, 
     { 
     "mls":"175111", 
     "address":"483 KINGS CANYON", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61201", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Coveted 3 BR", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"2" 
     }, 
     { 
     "mls":"20491", 
     "address":"306 Austin", 
     "dbcity":"ABIN", 
     "dbstate":"IL", 
     "zip":"61410", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Charming 2BR home with many interior improvements having recently been done. You have to see this one! Attached garage. TMI Home Warranty.", 
     "srcDB":"spi", 
     "beds":"2", 
     "baths":"1" 
     }, 
     { 
     "mls":"20492", 
     "address":"614 DEPOT", 
     "dbcity":"ALTA", 
     "dbstate":"IL", 
     "zip":"61414", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Nice 3 bedroom ranch on crawl", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"1" 
     }, 
     { 
     "mls":"160268", 
     "address":"947 Frank St.", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"This cute and charming 2 bedroom home is immediately available. Move right in or put your special touches to it and make it your own. Some great features to notice are the new roof (2016)", 
     "srcDB":"spi", 
     "beds":"2", 
     "baths":"1" 
     }, 
     { 
     "mls":"6157", 
     "address":"2555 Costa", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Entertain handsomely in this exhilarating 5BR 4BA 2-story home situated on 2.93 acres. You can find many desirable features including: ADT home security system", 
     "srcDB":"spi", 
     "beds":"5", 
     "baths":"2" 
     }, 
     { 
     "mls":"20493", 
     "address":"432 HOLMES", 
     "dbcity":"ONEI", 
     "dbstate":"IL", 
     "zip":"61467", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Nice ranch home with many updates! New carpet", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"1" 
     }, 
     { 
     "mls":"6158", 
     "address":"168 Pleasant", 
     "dbcity":"GALE", 
     "dbstate":"Il", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Quaint 3BR brick bungalow situated on a nice large lot. Enjoy a peaceful night in the enclosed front porch overlooking the quiet street. Inviting features include natural woodwork & hardwood flooring", 
     "srcDB":"spi", 
     "beds":"3", 
     "baths":"1" 
     }, 
     { 
     "mls":"20494", 
     "address":"115 F", 
     "dbcity":"ALPH", 
     "dbstate":"IL", 
     "zip":"61413", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"2BR home on large corner lot w/ newer roof and covered deck.", 
     "srcDB":"spi", 
     "beds":"2", 
     "baths":"1" 
     }, 
     { 
     "mls":"20495", 
     "address":"263 FULTON", 
     "dbcity":"GALE", 
     "dbstate":"IL", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Spacious 4BR 2BA with formal LR & DR", 
     "srcDB":"spi", 
     "beds":"4", 
     "baths":"2" 
     }, 
     { 
     "mls":"20496", 
     "address":"39 Knox Rd 2150N", 
     "dbcity":"GALE", 
     "dbstate":"Il", 
     "zip":"61401", 
     "openHouse1":"", 
     "openHouse2":"", 
     "openHouseAvail":"", 
     "listPrice":"0", 
     "remarks":"Beautiful fenced-in 5 acres to enjoy family outings and create memories! Small pond and 3-stall shed for horses or other pets. Property is being sold ''As Is.'' Please see supplement for additional information.", 
     "srcDB":"spi", 
     "beds":"2", 
     "baths":"1" 
     } 
    ], 
    "counter":15, 
    "error":"0" 
} 

当我检查它在https://jsonformatter.curiousconcept.com/

我以前用过的更大的JSON字符串这验证。发生什么事?

我的JavaScript看起来像:

$.ajax({  
     url: "../cgi/search.exe",  
     dataType: "json",  
     type: "POST", 
     data: dataString, 
     error: ajaxError,   
     success: function(json){   
       if(json.error !== "0"){      
        alertify.alert("ERROR "+json.error); 
        return; 
       }    

和解析Ajax错误的功能是

// generic ajax error handler 
function ajaxError(request, type, errorThrown) { 
    var message = "There was an error with the AJAX request.\n"; 
    switch (type) { 
    case 'timeout': 
     message += "The request timed out."; 
     break; 
    case 'notmodified': 
     message += "The request was not modified but was not retrieved from the cache."; 
     break; 
    case 'parsererror': 
     message += "XML/Json format is bad.";  
     break; 
    default: 
     try{ 
      message += "HTTP Error (" + request.status + " " + request.statusText + ")."; 
     }catch(e){ 
      return; 
     } 
    } 
    alertify.alert(message); 
} 
+4

我能解析JSON就好了。我猜想在后来的其中一个记录中有一些隐藏的字符,它在创建问题时迷失了方向。 –

+0

您是否尝试对文件中的字符串进行硬编码并解析它?你在尝试使用哪些浏览器?什么是你得到的确切的错误信息(因为我也无法重现)?请访问帮助中心,阅读如何在此提问,包括制作MCVE。 –

+0

似乎解析就好[这里](https://jsfiddle.net/mirohristov/ds7dewvo/)。什么是所有的反对票?现在不能问一个问题。就像你必须已经完美。 Geez – Miro

回答

0

你不是说你做了什么样的错误。所以我猜测:

  1. 请确保您的服务器响应的MIME类型是application/json,否则你可能会试图解析您的JSON作为XML ...如果没有MIME类型,一些浏览器试图猜测它采用启发式(这可能适用于小的反应,并不能替他人)

  2. 如果你正在那么你的JSON被解析OK错误alertify.alert("ERROR "+json.error);。请注意,您的JSON有一个属性

"error":"0"

此值不会国旗在JSON本身就是一个错误。它是应用程序特定的。根据你的jQuery如何将JSON转换为JS对象,这个值可能会被转换为字符串或整数。你正在使用严格的平等比较。为了安全起见,改变

if(json.error !== "0"){ 

if(json.error == 1 (or true or "yes" or whatever signals an error in your application) 
+0

错误来自$ .ajax方法,错误信息本身来自函数ajaxError。 错误=“0”是从服务器传递错误时,与验证JSON不同。 我已经缩小了这个问题,在JSON的具体文本行 *“5间卧室的牧场瓦特/ 2辆车独立车库。财产出售''是''包括任何现有的电器'* 我不能为我的生活找出什么是错的 – Shawn

+0

@Shawn你可以把这个记录在你的问题?因为如果这是verbatim是什么意思之一,那么你已经转义'''围绕AS IS – Taplar

+0

我发现错误:在json字符串中有一个无效的字符(chr $(196),我会给凯文B的功劳。 – Shawn