2

The JSON Response I'm getting,

     {
        "user_data": {
        "id": 22,
        "first_name": xxx,
        "last_name": xxx,
        "phone_number": "123456789",
        },
        "question_with_answers" : [
            {
           "id": 1,
           "question_name": "Features of our project that you like (select one 
            or more):",
           "answers": [
                {
                "id": 19,
                "question_id": 1,
                "customer_id": 22,
                "option": "Location",
                },
                {
                "id": 20,
                "question_id": 1,
                "customer_id": 22,
                "option": "Architecture",
                 }, 
               ]
            },
            {
            "id": 2,
            "question_name": "Features of our project that you DID NOT like 
             (select one or more):",
                 "answers": [
                 {
                "id": 22,
                "question_id": 2,
                "customer_id": 22,
                "option": "Location",
                 },

                ]
             },
             {
             "id": 3,
             "question_name": "How soon are you looking to buy a new home?",
             "answers": [
                    {
                       "id": 24,
                       "question_id": 3,
                       "customer_id": 22,
                       "option": "Immediately",
                   }
                 ]
              }
           ]
          }

So that is how JSON response looks like, now I need to display data using jquery

My js code

     function openModal(Id){
     var CustomerId = Id;
     $.ajax({
        type : 'get',
        url: 'customer-details',
        data: {
            'CustomerId':CustomerId
        },

        success: function(data)
        {
            //what I have to do here
        }
     })
} 

The output I want is,

first_name : xxx           
last_name  : xxx 
phone_number : 123456789

1.Features of our project that you like (select one or more):
ans: Location, Architecture

2.Features of our project that you DID NOT like (select one or more)
ans: Location

3.How soon are you looking to buy a new home?
ans: Immediately

Thats how my output should look like from above json response,Is it possible to get above output using that Json response I got There are two variable that I have passed from backend like user_data and question_with_answers

2
  • can you add html elements to a page using javascript or jquery? Commented Jul 29, 2017 at 9:58
  • 1
    Yes I can @marzelin Commented Jul 29, 2017 at 9:58

2 Answers 2

1

In success handler first of all make your json into a array using

var obj = JSON.parse(data);

Now in data you have array of your response and you can use this like

obj.first_name

obj.last_name

Note:- may be you have issue in JSON.parse so use first

var data = json.stringify(data)

after that use Json.parse function and use above data variable this data

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

4 Comments

what second varable data??there are two valiable please take a look
var data = JSON.parse(data); here data is the json which you get from php and pass in success handler and some time give give error because json convert in string yes I know josn is a string but json have a particular format so its give error if you face error then use var data = json.stringify(data) before json.parse function and in stringify function you pass the json which you used in success handler may be you got my point both data is same
I think obj.user_data.first_name will work after parsing.
@falero80s:- yes you are right user_data skip from my eye thanks to point out
1

You have to set the dataType in the $.ajax call, set it to dataType:"json"...

After that, you got on the data variable in the success the json object and you can access it like data.user_data or data.id or data.first_name.

If you dont define the json as dataType, it will not work properly.

Addition

If you want to display the content of "question_with_answer" you have to iterate trough it, like ....

for (i in data.question_with_answer) { alert(data.qestion_with_answer[i]); }

3 Comments

hello, @rebru I can able to display first object user_data but there is an array in it that is where I'm getting confused.
How can I display question_with_answers array values
I did edited my first post, for arrays in objects you have to iterate trough it.

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.