0

Pulling my hair out here, i know it' the structure of how JSOn works.

There is a multidimensional array that is undefined from a web API. structure is

{
"totalCount": 30,
"entries": [{"title": "item1","description": "your amazing item 1"}], 
"price": [{"value": "$23.34","discount": "none"}],
"newINfo" = 20
},
{
"entries": [{"title": "item2","description": "your amazing item 2"}], 
"price": [{"value": "$23.34","discount": "none"}],
"newINfo": = 50
},

Jquery > if i set $.each(data, to> $.each(data.entires and remove the entires from where its called on the content string, it works one at a time, but then the array items are not linked.

$.getJSON('/theWebApi', function (data) {
    var content = '';
    $.each(data, function(i, object) {
        content += '<div>' + object.entires.title + '</div>';
        content += '<div>' + object.price.value + '</div>';
    });
    $(content).appendTo(".test")
 }); 
4
  • explain more.., Question is confusing.. Commented Mar 21, 2016 at 6:52
  • There is an error in your json. "newINfo" = 20 should be "newINfo" : 20 Commented Mar 21, 2016 at 6:53
  • @jitendra so im trying to access the API to display information on my page. the API is a separate entity that can not be changed. there is currently around 30 records in there, that are going to scale up. i can access individual multidimensional array elements by specifying data.entries, however the whole array item is then not linked, so i can't call price, name, description etc together. Commented Mar 22, 2016 at 1:23
  • @AdityaParab was just me writing it wrong here, not an error, sorry Commented Mar 22, 2016 at 1:24

3 Answers 3

3

The entries and price are Array so you won't be able to do object.entries.title directly. You're gonna have to iterate over entries and price properties like,

$.getJSON('/theWebApi', function (data) {
    var content = '';
    $.each(data, function(i, object) {
        $.each(object.entries, function(k, v){
            content += '<div>' + v.title + '</div>';
        });
        $.each(object.price, function(k, v){
            content += '<div>' + v.value + '</div>';
        });
    });
    $(content).appendTo(".test")
 }); 
Sign up to request clarification or add additional context in comments.

2 Comments

tried this, getting an error in the consol "jquery-1.11.1.min.js:2 Uncaught TypeError: Cannot read property 'length' of undefined"
Got it, was a semi-colon in the wrong place, always the smallest of things, thank you for your help, much appreciated it. your logic was very sound and helped a lot.
0

Try changing to

{
    "totalCount": 30,
    "entries": {"title": "item1","description": "your amazing item 1"}, 
    "price": {"value": "$23.34","discount": "none"},
    "newINfo" = 20
},
{
    "entries": {"title": "item2","description": "your amazing item 2"}, 
    "price": {"value": "$23.34","discount": "none"},
    "newINfo" = 50
},

2 Comments

can't change the api, wish i could, its very poorly written
Then try entries[0].title and price[0].value. This should work assuming that are only one entry is expected entries.
0

This might help!

$.getJSON('/theWebApi', function (data) {
  var content = '';

  $.each(data, function(i, v) {
    $.each(v.entries, function(j, entry) {
      $.each(v.price, function(j, price) {
        content += '<div>'+ entry.title +'</div>';
        content += '<div>'+ price.value +'</div>';
      });
    });
  });

  $('.test').append(content);
});

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.