2

I am trying to parse the following JSON feed into HTML, but not getting any results.

{
  @name: "App name",
  license: [
    {
      @name: "Apache License 2.0",
      component: [
        {
          name: "Product 1",
          url: "http://www.url.com"
        }, {
          name: "Product 2",
          url: "http://www.url.com"
        }, {
          name: "Product 3",
          url: "http://www.url.com"
        }
      ],
      licensetext: " license text here"
    }
  ],
  isProjectComplete: "true"
}

With this jQuery:

$.getJSON("https://json-feed-url.json", function (data) {
  var jsondata = json;
  var output = "<ul>";
  for (var i in jsondata.events) {
    output += "<li>" 
      + jsondata.component[i].name + " " 
      + jsondata.component[i].url + "--"
      + jsondata.license[i].licensetext
      + "</li>";
  }

  output += "</ul>";
  document.getElementById("content").innerHTML = output;
});

To this div

<div id="content"></div>

Not sure why its not parsing correctly. Here's a jsFiddle.

3
  • 4
    Not sure your JSON is valid at all, validate here. A valid JSON would start with {"@name": "App name"Also, on the other hand, for cross-domain requests you would need JSONp. Commented Jan 15, 2015 at 16:06
  • Your keys need to be quoted: e.g. "@name" instead of @name, "license" instead of license, etc. Commented Jan 15, 2015 at 16:12
  • @skobaljic, the JSON is valid, and is coming directly from an AWS instance. I'd share the URL, but it has a secret key that is required to view the feed. Commented Jan 15, 2015 at 16:49

2 Answers 2

1

You have several errors, starting from invalid JSON you posted. Corrected it should work as you can see HERE and below:

var yourJson = {
    "@name": "App name",
        "license": [{"@name": "Apache License 2.0",
            "component": [{
            "name": "Product 1",
                "url": "http://www.url.com"
        }, {
            "name": "Product 2",
                "url": "http://www.url.com"
        }, {
            "name": "Product 3",
                "url": "http://www.url.com"
        }],
            "licensetext": " license text here"
    }],
        "isProjectComplete": "true"
};

function convertJson(data) {
    var jsondata = data;
    var output = "<ul>";
    for (var i in jsondata.license[0].component) {
        output += "<li>" + jsondata.license[0].component[i].name + " " + jsondata.license[0].component[i].url + "--" + jsondata.license[0].licensetext + "</li>";
    }

    output += "</ul>";
    document.getElementById("content").innerHTML = output;
};

convertJson(yourJson);

You should be more careful and you should learn to use debugging tools more effective.

Sign up to request clarification or add additional context in comments.

5 Comments

my JSON feed is correct. I'm using the JSON View extension in chrome to view my feed(which takes out the double quotes) I'm not sure of the syntax to change from var yourJson = {} to using the URL
Thanks for the help. I updated the jsfiddle(with actual url removed), but its still not pulling the feed in. It is when I have the actual data, but not with the URL.
Working on two different versions, but for now using the static content. Also trying to figure out how to apply an href to the url output
I am encapsulating the url with "<a href='license[0].component[i].url'>" + jsondata.license[0].component[i].url + "</a>", but its not applying the URL accordingly.
0

You have a typo in jsFiddle

// WRONG
$.getJSON("http://jsonurl.json";, function (data) {
// CORRECT
$.getJSON("http://jsonurl.json", function (data) {

2 Comments

Maybe I'm getting old, but the WRONG and CORRECT versions look exactly the same... I think you meant to remove the semicolon in the CORRECT version?
Yes, I meant to remove the semicolon.

Your Answer

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.