0

I have json array like this one:

[{"code" : "A1", "desc" : "desc1"},{"code" : "A2", "desc" : "desc2"}]

How should i parse it in jquery to get data like this:

A1 - desc1
A2 - desc2

I tried lots of different ways, but none of them works. At the moment i have a code like this:

$.ajax({
    url: 'my_url',
    success: function(data) {
        $.each(data, function (key, val) {
            alert(val.code);
        });
    },
    error: function() {
        alert('problem');
    }
});
1
  • 1
    You need to use $.parseJSON on data Commented Oct 11, 2012 at 17:05

3 Answers 3

2

You can also try setting the

dataType:'json' // to your ajax request

After adding this try to check with parseJson and sans parseJson

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

Comments

0

Use

jQuery.parseJSON( json )

Change code to

$.ajax({
   url: 'my_url',
   success: function(data) {
      var jsonData = jQuery.parseJSON(data);
      $.each(jsonData, function (key, val) {
        alert(val.code);
      });
  },
  error: function() {
    alert('problem');
  }
});

EDIT: Working fiddle

Cheers!!

2 Comments

I tried your solution, but it does not work. Previously it alerted "undefined" and now i don't get any alert.
Mine is failing with $.parseJSON( data );
0

Ok, i solved my problem. The problem was my hosting provider. I am using Codeigniter as a framework, and return result as json. My hosting provides adds 3 additional lines to the result (uses them for tracking opened pages). Problem was, that i was checking json, which i see in browser, but when i rightclicked and checked source code, i found those lines.

Comments

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.