本机 javascript JSON.parse 抛出类型错误
Native javascript JSON.parse throws typeError
我无法正确解析 JSON 字符串。好像没办法用JSON.parse。
eval 成功,但我想要安全的方式:)
我以这种方式在 aspx 服务 class 中查询网站:
[OperationContract]
public String queryNominatim(String request)
{
string uri = "http://nominatim.openstreetmap.org/search.php?q=" + request + nominatimParams;
string response = "from ajax";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = HttpContext.Current.Request.UserAgent;
req.Method = "POST";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Encoding enc = Encoding.GetEncoding(resp.CharacterSet);
StreamReader reader = new StreamReader(resp.GetResponseStream(), enc);
response = reader.ReadToEnd();
reader.Close();
return response;
}
其中请求是街道名称,如 "windmühlenstraße"。
完整的 uri 是“http://nominatim.openstreetmap.org/search.php?q=windmühlenstraße&format=json&countrycodes=de&addressdetails=1”
答案是一个 JSON 字符串,我只想将其传递给调用 javascript 代码。 http://jsonlint.com/ 验证为正确。
但是在javascript中,这段代码
arr = JSON.Parse(response);
抛出异常:
TypeError: JSON.Parse is not a function
这是我目前发现的:
JSON.Parse
已存在并且正在运行。我在 javascript 中成功地尝试了另一个 json-string 硬编码。
arr = eval("(" + response + ")");
按预期工作。数组和对象是完全可访问的。
我通过以下方式将转义的 unicode 字符服务器端转换为 unicode 字符:
private string DecodeEncodedNonAsciiCharacters(string value)
{
return Regex.Replace(
value,
@"\u(?<Value>[a-zA-Z0-9]{4})",
m =>
{
return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
});
}
但无论如何都会抛出异常。
- 我将答案硬编码为 Javascript:
var original = "[{\"place_id\":\"2413006\",\"licence\":\"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright\",\"osm_type\":\"node\",\"osm_id\":\"344446896\",\"boundingbox\":[\"52.3739283\",\"52.3740283\",\"9.7434778\",\"9.7435778\"],\"lat\":\"52.3739783\",\"lon\":\"9.7435278\",\"display_name\":\"6, Theaterstra\u00dfe, Mitte, Hannover, Region Hannover, Niedersachsen, 30159, Deutschland\",\"class\":\"place\",\"type\":\"house\",\"importance\":0.311,\"address\":{\"house_number\":\"6\",\"road\":\"Theaterstra\u00dfe\",\"suburb\":\"Mitte\",\"city_district\":\"Mitte\",\"city\":\"Hannover\",\"county\":\"Region Hannover\",\"state\":\"Niedersachsen\",\"postcode\":\"30159\",\"country\":\"Deutschland\",\"country_code\":\"de\"}}]";
jsObject = JSON.parse(original);
alert(jsObject[0] + ": " + jsObject[0].display_name);
这是成功的。显示名称已显示。
在 json 字符串中转换和转义 unicode 没有区别。 Firefox 显示正确的字母。
在chrome中,错误拼写为:typeerror: undefined is not a function。
IE: typeerror: das objekt unterstützt die Eigenschaft oder Methode "parse" nicht。含义:对象不支持 属性 或方法 "parse".
怎么了?我想念复制和粘贴转换的东西吗?
我错过了什么???
JSON.Parse
不存在JavaScript,你要JSON.parse
。小写 p!
根据我从您的第一个代码片段 arr = JSON.Parse(response);
得知的情况,您正在尝试使用 JSON.parse
的大写版本。第二个代码片段在所有现代浏览器中对我来说都很好。请参阅文档 here。 JavaScript 区分大小写。
我无法正确解析 JSON 字符串。好像没办法用JSON.parse。 eval 成功,但我想要安全的方式:) 我以这种方式在 aspx 服务 class 中查询网站:
[OperationContract]
public String queryNominatim(String request)
{
string uri = "http://nominatim.openstreetmap.org/search.php?q=" + request + nominatimParams;
string response = "from ajax";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = HttpContext.Current.Request.UserAgent;
req.Method = "POST";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Encoding enc = Encoding.GetEncoding(resp.CharacterSet);
StreamReader reader = new StreamReader(resp.GetResponseStream(), enc);
response = reader.ReadToEnd();
reader.Close();
return response;
}
其中请求是街道名称,如 "windmühlenstraße"。 完整的 uri 是“http://nominatim.openstreetmap.org/search.php?q=windmühlenstraße&format=json&countrycodes=de&addressdetails=1”
答案是一个 JSON 字符串,我只想将其传递给调用 javascript 代码。 http://jsonlint.com/ 验证为正确。
但是在javascript中,这段代码
arr = JSON.Parse(response);
抛出异常:
TypeError: JSON.Parse is not a function
这是我目前发现的:
JSON.Parse
已存在并且正在运行。我在 javascript 中成功地尝试了另一个 json-string 硬编码。arr = eval("(" + response + ")");
按预期工作。数组和对象是完全可访问的。我通过以下方式将转义的 unicode 字符服务器端转换为 unicode 字符:
private string DecodeEncodedNonAsciiCharacters(string value) { return Regex.Replace( value, @"\u(?<Value>[a-zA-Z0-9]{4})", m => { return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString(); }); }
但无论如何都会抛出异常。
- 我将答案硬编码为 Javascript:
var original = "[{\"place_id\":\"2413006\",\"licence\":\"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright\",\"osm_type\":\"node\",\"osm_id\":\"344446896\",\"boundingbox\":[\"52.3739283\",\"52.3740283\",\"9.7434778\",\"9.7435778\"],\"lat\":\"52.3739783\",\"lon\":\"9.7435278\",\"display_name\":\"6, Theaterstra\u00dfe, Mitte, Hannover, Region Hannover, Niedersachsen, 30159, Deutschland\",\"class\":\"place\",\"type\":\"house\",\"importance\":0.311,\"address\":{\"house_number\":\"6\",\"road\":\"Theaterstra\u00dfe\",\"suburb\":\"Mitte\",\"city_district\":\"Mitte\",\"city\":\"Hannover\",\"county\":\"Region Hannover\",\"state\":\"Niedersachsen\",\"postcode\":\"30159\",\"country\":\"Deutschland\",\"country_code\":\"de\"}}]";
jsObject = JSON.parse(original);
alert(jsObject[0] + ": " + jsObject[0].display_name);
这是成功的。显示名称已显示。
在 json 字符串中转换和转义 unicode 没有区别。 Firefox 显示正确的字母。
在chrome中,错误拼写为:typeerror: undefined is not a function。 IE: typeerror: das objekt unterstützt die Eigenschaft oder Methode "parse" nicht。含义:对象不支持 属性 或方法 "parse".
怎么了?我想念复制和粘贴转换的东西吗? 我错过了什么???
JSON.Parse
不存在JavaScript,你要JSON.parse
。小写 p!
根据我从您的第一个代码片段 arr = JSON.Parse(response);
得知的情况,您正在尝试使用 JSON.parse
的大写版本。第二个代码片段在所有现代浏览器中对我来说都很好。请参阅文档 here。 JavaScript 区分大小写。