-1

I have this JSON

var b = {
  "Id": 1,
  "ProductCode": 148,
  "Battery": "",
  "OS": "",
  "SimCardCount": null,
  "CPU": "",
  "ScreenSize": null,
  "InternalStorage": null,
  "RAM": null,
  "MemoryCard": null,
  "Have3G": false,
  "Have4G": false,
  "Camera": null,
  "FrontCamera": null
};

but this titles is not to be fixed. For example : maybe "Battery" changes to "Color".

I want to show this JSON in to the html Dynamically

user692942
  • 16,398
  • 7
  • 76
  • 175

3 Answers3

1

you can loop through the object like

for(index in b)
{
    console.log(index, b[index]);
}

when the index is an id you can access the elements with

var id = '#'+index;
var $elm = $(id);
Chris
  • 221
  • 1
  • 8
1

You can loop an object for its key and value pairs using Jquery's .each:

  //loop the object using JQuery each
   $.each(b, function(key, value) {
      //display the key and value pairs however you want
      $("p").append(key + ' is ' + value + "<br>");
  });

Example

Cory
  • 1,263
  • 16
  • 31
1

By using $.each you can loop through json obj and bind to your html

//Html table

var row = "";
     $.each(b, function(key, value) {
           row+="<tr><td>'"+key+"'</td><td>'"+value+"'</td></tr>"
        });

$("#myTable").html(row);