0

I have this JSON file with nested array > queue_list.

[{
    "id_shop": "2",
    "shop_name": "Brackets",
    "queue_list": [{
        "id_queue_current": "1",
        "queue_name": "Queue 1",
        "queue_current_number": "12"
    }, {
        "id_queue_current": "2",
        "queue_name": "Queue 2",
        "queue_current_number": "23"
    }]
}]

With my jQuery/ajax I can read correctly, id_shop and shop_name, but cannot read elements in queue_list. How can I access at this elements?

    $.ajax({
      type: "GET",
      url: "shop_list.php",
      dataType: 'html',
      success: function(data) { 
            data = JSON.parse(data); 
            data.forEach(function(dataItem){
            var id_shop = dataItem.id_shop;
            var shop_name = dataItem.shop_name;
            var queue_current_number = dataItem.current_number;
            var queue_name = dataItem.queue_name; //this element
             $("#header h1").html('<div>'+shop_name+'</div>');
                 $("#shop_queue").html('<li data-categoryId ="'+id_shop+'"><a href="#shop_'+id_shop+'">'+queue_name+'</a></li>');
         });
         }
    }); 

thank your very much!

3 Answers 3

1

Try with below solution,

var test = 
[{
    "id_shop": "2",
    "shop_name": "Brackets",
    "queue_list": [{
        "id_queue_current": "1",
        "queue_name": "Queue 1",
        "queue_current_number": "12"
    }, {
        "id_queue_current": "2",
        "queue_name": "Queue 2",
        "queue_current_number": "23"
    }]
}]

     test.forEach(function(dataItem){
            var id_shop = dataItem.id_shop;
            var shop_name = dataItem.shop_name;
            var queue_data = dataItem.queue_list; 
            queue_data.forEach(function(queueItem){
                var queue_id= queueItem.id_queue_current;
                alert(queue_id);
                var queue_name= queueItem.queue_name;
                alert(queue_name);
                var queue_current_number= queueItem.queue_current_number;
                alert(queue_current_number);
            })
         })
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

0

You might have to do something like this inside your success method

var queue_name;

        var queue_lst = dataItem.queue_list.each(function(item, index){
            queue_name=JSON.parse(item).queue_name;
        }); 

Comments

0

you cannot read and access the elements in queue_list by merely calling dataItem.current_number or dataItem.queue_name. You can access the elements inside the JSON object queue_list by calling elements through queue_list like dataItem.queue_list.current_number and dataItem.queue_list.queue_name.

Hope this solved the problem.

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.