[英]JSON.parse doesn't work in Javascript?
我在頁面加載之前使用ajax獲取一些Json字符串。 從我的處理程序中,我返回格式化json的字符串。 而且我必須在javascript中使用json。 為此,我嘗試JSON.parse(myJsonString),它不起作用。 當我提醒它沒有出現時。 我要去哪里錯了?
var geojsonObject2 ;
$.ajax({
url: "LoadHandler.ashx",
success: function getFromDBCallback(geojsonObject) {
//var temp='['+geojsonObject+']'// I also try that, it don't work
var obj = JSON.parse(geojsonObject);
alert(obj);// for checking but nothing show up here?
geojsonObject2 = obj;
},
async: false
});
這是我的處理程序:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
#region json string
string geojsonObject = @"
{
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:4326'
}
},
'features': [
{
'type': 'Feature',
'properties': {
'any-property': 'feature1'
},
'geometry': {
'type': 'Point',
'coordinates': [21.54967, 38.70250]
}
},
{
'type': 'Feature',
'properties': {
'any-property': 'feature2'
},
'geometry': {
'type': 'LineString',
'coordinates': [
[21.54967, 38.70250], [22.54967, 39.70250]
]
}
}
]
}
";
#endregion
//context.Response.Write(js.Serialize(geojsonObject));
context.Response.Write(geojsonObject);
}
嘗試如下將雙引號替換為單引號,如果屬性名稱和值用雙引號引起來,則JSON.parse可以工作:
string geojsonObject = @"
{
""type"": ""FeatureCollection"",
""crs"": {
""type"": ""name"",
""properties"": {
""name"": ""EPSG:4326""
}
}...."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.