我在页面加载之前使用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);
}
答案 0 :(得分:1)
尝试使用双引号替换单引号,如果属性名称和值包含在双引号中,则JSON.parse有效:
string geojsonObject = @"
{
""type"": ""FeatureCollection"",
""crs"": {
""type"": ""name"",
""properties"": {
""name"": ""EPSG:4326""
}
}...."