我有以下 json 代码文件,名为:sections.json
{
"section1": { "priority": 1, "html": '<img src="../images/google.png" />'},
"section2": { "priority": 2, "html": '<input type="button" value="Login" />'},
"section3": { "priority": 3, "html": '<div>Some text</div>'},
"section4": { "priority": 4, "html": '<div>Some text</div>'},
"section5": { "priority": 5, "html": '<select><option>option1</option> <option>option2</option></select>'}
}
我正在 jquery 代码中尝试此操作,但警报不起作用
$.getJSON("sections.json", function(json) {
alert('h');
});
你的 JSON 应该是这样的:
{
"section1": { "priority": 1, "html": "<img src='../images/google.png' />"},
"section2": { "priority": 2, "html": "<input type='button' value='Login' />"},
"section3": { "priority": 3, "html": "<div>Some text</div>"},
"section4": { "priority": 4, "html": "<div>Some text</div>"},
"section5": { "priority": 5, "html": "<select><option>option1</option> <option>option2</option></select>"}
}
值必须用双引号括起来才能成为有效的 JSON,单引号不行:) 从 jQuery 1.4 开始,默认情况下不再允许无效的 JSON(他们添加了一些额外的检查以确保其有效,并且中的 JSON你的问题被那些人挡住了:)
)。尝试 http://www.jsonlint.com/ 验证您的 JSON 文件。 例如对于第 1 部分,html 的值应为
"<img src='../images/google.png' />"
查看此答案,了解 JSON 中 html 标签的示例:JSON 中的 HTML 标签(Python 中)