user3036950
user3036950

Reputation:

Parsing my json file

I am trying to parse a json file using jquery but im not getting any data back,

My jquery code:

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url: 'http://anteatercycles.co.uk/xml/sensa.json',
        success: function (json) {
            var data1 = json.data[0];
            var result1 = '<h3>' + data1.merchant_category + '</h3>' + '<p>' + data1.description + '</p>' ;
            $('#description1').append(result1);
        }
    });
});

<div id="description1"></div>

Thanks in advanced.

edit to below still not retrieving any data?

$(document).ready(function () {

$.ajax({ type: 'GET', dataType: "json", url: 'http://anteatercycles.co.uk/xml/sensa.json', success: function (json) { json=JSON.parse(json);//parse json data var data1 = json.data[0]; var result1 = '

' + data1.merchant_category + '

' + '

' + data1.description + '

' ; $('#description1').append(result1); } }); });

</script>   

<div id="description1"></div>

Upvotes: 0

Views: 176

Answers (4)

Quentin
Quentin

Reputation: 943564

Look at the data your server is sending for that JSON:

%  curl -I http://anteatercycles.co.uk/xml/sensa.json
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Thu, 04 Sep 2014 10:41:58 GMT
ETag: "fe1328ea-4ff3e-5023b007f6708"
Content-Type: text/plain
Content-Length: 327486
Accept-Ranges: bytes
Date: Thu, 04 Sep 2014 12:10:03 GMT
X-Varnish: 2518742114 2518618326
Age: 72
Via: 1.1 varnish
Connection: keep-alive

The important line is the Content-Type. It is claiming it is text/plain but it should be saying application/javascript.

You should fix the server so it gives the correct content-type for that file.

As a hacky work-around, you can tell jQuery to ignore the content type and treat it as JSON anyway.

Add dataType: "json" to your ajax options:

$.ajax({
    type: 'GET',
    dataType: "json",

Additionally, the JSON doesn't return an object with a data property. So accessing json.data makes no sense. You want json[0].

Upvotes: 0

Shaik Md N Rasool
Shaik Md N Rasool

Reputation: 500

if you are expecting a JSON response from your service you need to give dataType:"JSON" then this will automatically returns a JavaScript object.The JSON data is parsed in a strict manner.any malformed JSON is rejected and a parse error is thrown or may be you are getting empty response from the service.

Upvotes: 0

suraj baliyan
suraj baliyan

Reputation: 71

Use JSON.parse() or set dataType:"json"

 success: function (json)
      {   json=JSON.parse(json);//parse json data
          var data1 = json.data[0];
          var result1 = '<h3>' + data1.merchant_category + '</h3>' +
          '<p>' + data1.description + '</p>' ;
          $('#description1').append(result1);
      }

Upvotes: 3

Joe
Joe

Reputation: 15802

Adding in dataType: 'json' as an option on the AJAX call should work. If you tell jQuery it's JSON data, it should parse it into an object for you. It's most likely that the server isn't returning the JSON file with the correct MIME type so jQuery is just assuming it's plain text.

Upvotes: 3

Related Questions