0

I am trying to extract the json content in my javascript by running it but I am not getting the desired output

My JSON is of the form

 var json = [{
        "html": "abc.jpg", //testing this failed
        "col": 1,
        "row": 1,
        "size_y": 2,
        "size_x": 2
    }, {
        "html": "def.jpg", 
        "col": 4,
        "row": 1,
        "size_y": 2,
        "size_x": 2
    },

    {
        "html": "bac.jpg",
        "col": 6,
        "row": 1,
        "size_y": 2,
        "size_x": 2
    },


    {
        "html": "xyz.jpg",
        "col": 1,
        "row": 3,
        "size_y": 1,
        "size_x": 1
    }, {
        "html": "Brand.jpg",
        "col": 4,
        "row": 3,
        "size_y": 1,
        "size_x": 1
    },

    {
        "html": "Brand.jpg",
        "col": 6,
        "row": 3,
        "size_y": 1,
        "size_x": 1
    }

    ];

The loop which I am trying to extract content from JSON is

for(var index=0;index<json.length;index++) {
    gridster.add_widget('<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/'+json[index].html+'"%}"></li>',json[index].size_x,json[index].size_y,json[index].col,json[index].row);
};

What I want is

<img src="{% static "images/abc.jpg"%}"> #for first elemet

I am sure I am missing some comma in it but not sure where exactly

Update 1

The "{% static 'images/abc.jpg' %}" This is exact comma structure I want in my ouput

Update 2

Here's a snippet console logging the arguments I'm creating:

var json = [{
    "html": "abc.jpg", //testing this failed
    "col": 1,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "def.jpg",
    "col": 4,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "bac.jpg",
    "col": 6,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "xyz.jpg",
    "col": 1,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }, {
    "html": "Brand.jpg",
    "col": 4,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }, {
    "html": "Brand.jpg",
    "col": 6,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }
];

for (var index = 0; index < json.length; index++) {
  console.log('<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/' + json[index].html + '"%}"></li>', json[index].size_x, json[index].size_y, json[index].col, json[index].row);
};
connexo
  • 53,704
  • 14
  • 91
  • 128
Rookie_123
  • 1,975
  • 3
  • 15
  • 33

3 Answers3

1

Your loop is returning this

<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/abc.jpg"%}"></li> 2 2 1 1
<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/def.jpg,"%}"></li> 2 2 4 1
<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/bac.jpg"%}"></li> 2 2 6 1
<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/xyz.jpg"%}"></li> 1 1 1 3
<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/Brand.jpg"%}"></li> 1 1 4 3
<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static "images/Brand.jpg"%}"></li> 1 1 6 3

You might have extra comma here: "images/def.jpg," at index=1

1

You have a problem with the use of quotes. Fix it be prefixing your inner ' characters with a slash \' (this is called escaping):

gridster.add_widget('<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% static \'images/'+json[index].html+'\'%}"></li>',json[index].size_x,json[index].size_y,json[index].col,json[index].row);

var json = [{
    "html": "abc.jpg", //testing this failed
    "col": 1,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "def.jpg",
    "col": 4,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "bac.jpg",
    "col": 6,
    "row": 1,
    "size_y": 2,
    "size_x": 2
  }, {
    "html": "xyz.jpg",
    "col": 1,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }, {
    "html": "Brand.jpg",
    "col": 4,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }, {
    "html": "Brand.jpg",
    "col": 6,
    "row": 3,
    "size_y": 1,
    "size_x": 1
  }
];

for (var index = 0; index < json.length; index++) {
  console.log('<img src="{% static \'images/' + json[index].html + '\' %}">');
};
connexo
  • 53,704
  • 14
  • 91
  • 128
  • It says `Could not parse the remainder: '\'images/'+json[index].html+' from '\'images/'+json[index].html+` – Rookie_123 Mar 22 '18 at 11:32
  • 1
    In that case the problem is related to `gridster.add_widget` whatever that is, however this answer gives you the changes necessary to create your desired output. – connexo Mar 22 '18 at 11:33
  • Yeah same may be the case Thanks for your help – Rookie_123 Mar 22 '18 at 11:34
0

if you replace gridster.add_widget with console.log, the output looks fine:

<img src="{% static "images/abc.jpg"%}">

Try adding space before closing tag:

..json[index].html+'" %}"></li>
                     ^here
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26