4

i am newbie in JSON. i just started learning JSON before 30 mins.

i want to print JSON array in jquery.

 $.ajax({
                url : '/exp/resp.php',
                type : 'POST',
                dataType : 'json',
              //  data : 'uname='+uname+'&pass='+pass,
                success : function (data)
                {
                    alert(data);

                }
            });

now it's printing abc,def,ghi,jkl,mno. so i want to print it separate like abc def ghi etc.. i referred this answer but it didn't help...

1
  • 4
    data is not a "JSON array". Even though the response is JSON-encoded data, jQuery will have parsed the data already. So data is just a normal JavaScript array. The question/problem itself has nothing to do with JSON. Commented Oct 3, 2013 at 6:11

3 Answers 3

16

Why not print something generic like:

JSON.stringify(data);

This should work with either an object or an array.

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

2 Comments

it's not printing anything
right, this one just converts the object into a string. If you want to print it you have to use "console.log(JSON.stringify(data));" or "alert(...);" or whatever...
7

if data is an array then

alert(data.join(' '));

5 Comments

tnx for answer.. i want to ask you one more thing that if i want to access it like data.1, data.2 something like this? how ot do it? i m returning it from php using json_encode($array)..
@pandit you can use the index to access it.. like data[0], data[1]... data[data.length - 1]
thanks.... and one more question that if i want to send json object from php file then how send?
@pandit not sure... not a PHP guy... i think there is a json_encode call
@pandit Arun is right, you can convert an array into a json string with json_encode and you can convert an json string into an array using json_dncode...
2

Since your data is an array Check the following tutorial

Join the elements of an array into a string:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join();

The result of energy will be:

Banana,Orange,Apple,Mango

Have a look at this http://www.w3schools.com/jsref/jsref_join.asp

For your code use Arun P Johny's solutions

alert(data.join(' '));

1 Comment

tnx for answer in detail.. i want to ask you one more thing that if i want to access it like data.1, data.2 something like this? how ot do it? i m returning it from php using json_encode($array)..

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.