2

I have the following json code file named: 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>'}
}

I am trying this in jquery code but alert is not working

$.getJSON("sections.json", function(json) {
 alert('h');
});
dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Mar 23, 2010 at 9:43
2
  • 1
    Is the path to the file the correct one? Commented Mar 23, 2010 at 9:45
  • 1
    What does "not working" mean? What does happen? What if you examine the DOM and any XHR requests using Firebug? Commented Mar 23, 2010 at 10:42

2 Answers 2

1

Your JSON should be like this:

{
 "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>"}
}

The values must be double quoted to be valid JSON, single quotes won't do :) As of jQuery 1.4, invalid JSON is no longer allowed by default (they added some additional checks to ensure it's valid, and the JSON in your question is getting blocked by those :)

answered Mar 23, 2010 at 10:40
Sign up to request clarification or add additional context in comments.

Comments

0

Seems to be invalid JSON so JQuery fails silently (see http://api.jquery.com/jQuery.getJSON/). Try http://www.jsonlint.com/ to validate your JSON file.

E.g. for section 1 the value for html should be "<img src='../images/google.png' />"

Check out this answer for an example of html tags in JSON: HTML tags within JSON (in Python)

answered Mar 23, 2010 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.