JSON.parse没有'不能使用Javascript

JSON.parse doesn't work in Javascript?

本文关键字:不能 Javascript parse 没有 JSON      更新时间:2023-09-26

我在页面加载之前使用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""
            }
        }...."