0

I am having a JSON file as mentioned below :-

{   head: {
    link: [],
    vars: [
      "CompanyName",
      "Company_Name",
      "Foundation_URI",
      "Foundation_Name",
      "Latitude",
      "Longitude"
    ]   },   results: {
    distinct: false,
    ordered: true,
    bindings: [
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/United_Christian_Broadcasters"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "United Christian Broadcasters"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      },
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/United_Christian_Broadcasters"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "UCB Media"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      },
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/Kathmandu_%28company%29"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Kathmandu"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      }
    ]   } }

The problem that I face is I am using $.getJSON() which is available in JQuery and am not able to retrieve relevant information from the JSON file. The code I use is as follows :-

$.getJSON("data.json", function(data) {
       $.each(data.results.bindings,function(i,res)
               {
                    $('#jsonData').text(res.CompanyName.value);

                }
               );
     });

I dont even get an error :( . I am using eclipse for Dynamic web projects and using tomcat server for deploying my application. The JSON file is a local file in the WEB-INF directory itself and the $.getJSON() call is able to easily fetch it. The only problem is that I am not able to retrieve the data ??

When I replace the $.getJSON() by the code below i get an error stating error:[object Object]

 $.ajax({
      url: "data.json",
      dataType: 'json',
      success: function(data){alert("SUCCESS");},
      error: function(e){alert("error:"+e);}
    });
0

4 Answers 4

6

$.getJSON() will fail silently if the JSON Data is not formatted correctly. Your JSON data is missing double-quotes around the key values.

Use the following format checker to correct your errors: http://jsonformatter.curiousconcept.com/

Good Luck!

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

Comments

1

Make a note that the following portion is invalid:

xml:lang: "en", 

either

xml:{lang: "en"},

or

xml:"", lang: "en", 

Comments

1

jQuery's AJAX methods mostly fail silently - lucky for you I know what the problem is.

Your JSON is invalid - all keys in JSON MUST have double quotes around them.

See here for more details / an easy way to check: http://jsonlint.com/

EDIT: Just occurred to me that if you're using Tomcat, you could use the excellent java net.sf.json library to translate arrays, lists, lists of beans etc to json directly without hand coding anything. You can find it here: http://json-lib.sourceforge.net/

Comments

0

Generally, jQuery JSON commands are working with JSON strings where each key has to be quoted. So try {"head" : ...} instead of {head : ...}

1 Comment

too many capable people around :-) but the answer still holds

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.