0

I am getting JSON via ajax like this

{
    "questionTypes": [
        {
            "id": 1,
            "name": "text",
            "deleted": false,
            "version": 0
        },
        {
            "id": 2,
            "name": "rating",
            "deleted": false,
            "version": 0
        },
        {
            "id": 3,
            "name": "boolean",
            "deleted": false,
            "version": 0
        },
        {
            "id": 4,
            "name": "option",
            "deleted": false,
            "version": 0
        }
    ],
    "data": [
        {
            "id": 1,
            "category": "default",
            "question": "Staff Courtesy",
            "deleted": false,
            "version": 0
        },
        {
            "id": 2,
            "category": "default",
            "question": "Staff Response",
            "deleted": false,
            "version": 0
        },
        {
            "id": 3,
            "category": "default",
            "question": "Check In",
            "deleted": false,
            "version": 0
        },
        {
            "id": 4,
            "category": "default",
            "question": "Check Out",
            "deleted": false,
            "version": 0
        },
        {
            "id": 5,
            "category": "default",
            "question": "Travel Desk",
            "deleted": false,
            "version": 0
        },
        {
            "id": 6,
            "category": "default",
            "question": "Door Man",
            "deleted": false,
            "version": 0
        },
        {
            "id": 14,
            "category": "client",
            "question": "test question",
            "deleted": false,
            "version": 0
        },
        {
            "id": 15,
            "category": "client",
            "question": "test1",
            "deleted": false,
            "version": 0
        },
        {
            "id": 16,
            "category": "client",
            "question": "test2",
            "deleted": false,
            "version": 0
        },
        {
            "id": 17,
            "category": "client",
            "question": "test2",
            "deleted": false,
            "version": 0
        },
        {
            "id": 21,
            "category": "client",
            "question": "ggggg",
            "deleted": false,
            "version": 0
        }
    ]
}

I want to get the name fields from

"questionTypes": [
    {
        "id": 1,
        "name": "text",
        "deleted": false,
        "version": 0
    },
    {
        "id": 2,
        "name": "rating",
        "deleted": false,
        "version": 0
    },
    {
        "id": 3,
        "name": "boolean",
        "deleted": false,
        "version": 0
    },
    {
        "id": 4,
        "name": "option",
        "deleted": false,
        "version": 0
    }
],

$.each(data1.data.questionTypes, function(index, currPat) {

                   console.log(currPat.name); }

but it did not work ,I get undefined in console.Can any body tell me how to loop exactly

15
  • 3
    I don't see any username properties...? Commented May 26, 2014 at 10:08
  • 2
    What is this username you're trying to print out? It looks like name in your JSON? Commented May 26, 2014 at 10:08
  • 2
    jsfiddle.net/497Tx Commented May 26, 2014 at 10:12
  • 1
    You have data and questionTypes on the same level on your json response, so why you're trying to get questionTypes as a child of data field, consequently, you have to do this: data1.questionTypes, you got undefined values because there's no field named questionTypes inside data field. Commented May 26, 2014 at 10:25
  • 1
    @JqueryLearner i know, but this is not about using JSON or not, you're trying to access a field that doesn't exist on your JSON object hierarchy, so iterating data1.data.questionTypes will not give any values because it's undefined Commented May 26, 2014 at 10:52

5 Answers 5

2

Demo Fiddle

jQuery

$.each(json.data, function (index, currPat) {
    $(tbody).append('<tr><td><b>Question from data: ' + currPat.question + '</b></td><td><b>Names from questionTypes</b><select class="qType"></select></td></tr>');
});

var select = $('.qType');
$(json.questionTypes).each(function (index, currPat) {
    $(select).append('<option>' + currPat.name + '</option>');
});  

You must loop twice: Once for getting the question and then for getting name.

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

2 Comments

Thanks for the answer,+1.I will have a look
@JqueryLearner chat link..?
2

OK, you can do this ,

$(jsonObject.questionTypes).each(function(i,value){
     //get value here using value.name
     alert(value.name);
 });

And the problem was you were actually extracting questionTypes from datawhich is non existent there.

Comments

0

Fiddle Demo

data has no questionTypes

$.each(data1.questionTypes, function(index, currPat) {

                      alert(currPat.name);


    });

2 Comments

@downvoters could you tell me the reason for downvote then only i rectify my error
I need each row should be from data root so I did ` $.each(data1.data, function(index, currPat) { tRow += '<tr><td><input type="checkbox" class="cd" value=""/></td>'; tRow += '<td>'+currPat.question+'</td>';` and now each row should be having a drop down menu having options from questiontypes root
0

in your code I don't see any username properties, try to use this code.

$.each(data1.data.questionTypes, function(index, currPat) {

               console.log(currPat.name); }

Comments

0

Try this

$.each(json.questionTypes, function (index, currPat) {
    div=$('<div/>').text(currPat.name);
    $('#jsoncontainter').append(div);
});

http://jsfiddle.net/ardeezstyle/497Tx/1/

For your second part of the problem, here is a solution.

$.each(json.data, function (index, currQuest) {
    question=$('<div/>',{'class':'question'}).text(currQuest.question);
    answer=$('<div/>',{'class':'answer'});
    dropdown=$('<select/>');
    $.each(json.questionTypes, function (index, currPat) {
        option=$('<option/>').text(currPat.name).appendTo(dropdown);
    });
    answer.append(dropdown);
    $('#jsoncontainter').append(question).append(answer);
});

Fiddle

4 Comments

I want to show question from data root as row and each row should be having one drop down and that drop will have options from questionType.So how to do?
What is data root? Can you create a fiddle with the JSON assuming you are creating the question and dropdown options from the JSON.
under data you will find questions like Staff Courtesy,test2 .... .So this will be row

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.