在 Html 中打印 Json 数据

Print Json Data in Html

我想在 HTML 页 w.r.t 中显示 json 数据。我将从视频中获得的帧。

我正在写我想要在 HTML 页中的 json 格式:

frame : 1 ▼
          object_detected : 1
          object_classname : car ▼
                              co-ordinates:
                                           x : 20
                                           y : 45
                                           width : 300
                                           height : 100

frame : 2 ▼
          object_detected : 2
          object_classname : car ▼
                              co-ordinates:
                                           x : 20
                                           y : 45
                                           width : 300
                                           height : 100

          object_class : car ▼
                              co-ordinates:
                                           x : 24
                                           y : 5
                                           width : 70
                                           height : 10

► / ▼ - 这是向下的实心箭头标记,用户必须手动单击那里 (►) 它将 (▼) 显示其中包含的数据。

json 文件数据:

var data = [
{
    "Frame_count":1,

        "Objects_detected":1,
        "Objects_classname":"car",
        output:{

            "x":20,
            "y":45,
            "width":300,
            "height":100,
        },

},
{
    "Frame_count":2,

        "Objects_detected":2,
        "Objects_classname":"car",
        output:{

            "x":20,
            "y":45,
            "width":300,
            "height":100,

            "x":25,
            "y":5,
            "width":70,
            "height":10,

        },

}]

编辑 Matthias Schmidt 为我的代码添加了箭头更改功能

我制作了这个片段。这是你想要的? 您也可以在 https://jsfiddle.net/hkedLpbq/8/

查看

var data = [
{
    "Frame_count":1,

        "Objects_detected":1,
        "Objects_classname":"car",
        output:{

            "x":20,
            "y":45,
            "width":300,
            "height":100,
        },

},
{
    "Frame_count":2,

        "Objects_detected":2,
        "Objects_classname":"car",
        output:{

            "x":20,
            "y":45,
            "width":300,
            "height":100,

            "x":25,
            "y":5,
            "width":70,
            "height":10,

        },

}];


function getTemplate(index, frame, objects){
 let template = '<div class="wrapper" id='+ index +'>' +
  '<div class="frame">'+ frame +'</div>' +
  
  '<div class="subWrapper">' +
    '<div class="objects">'+ objects +'</div>' +
    '</div>' +
  '</div>' +
'</div>';
return template;
}

function getSubTemplate(className, x, y, width, height){
 let subTemplate = '<div class="className">'+ className +'</div>' +
    
    '<div class="subSubWrapper">' +
      '<div class="co-ordinates"></div>' +
      
      '<div class="subSubSubWrapper">' +
        '<div class="x">'+ x +'</div>' +
        '<div class="y">'+ y +'</div>' +
        '<div class="width">'+ width +'</div>' +
        '<div class="height">'+ height +'</div>' +
      '</div>';
 return subTemplate;
}


for(var i = 0; i < data.length; i++){
 var frameCount = data[i].Frame_count;
  
 if(data[i].Objects_detected > 0){
   var objectsDetected = data[i].Objects_detected;
    
    if(data[i].Objects_classname){
    var objectsClassname = data[i].Objects_classname;
   }
    if(data[i].output.x){
    var x = data[i].output.x;
   }
    if(data[i].output.y){
    var y = data[i].output.y;
   }
    if(data[i].output.width){
    var width = data[i].output.width;
   }
    if(data[i].output.height){
    var height = data[i].output.height;
   }
    
    $(".content").append(getTemplate(i, frameCount, objectsDetected));
    for(var ii = 0; ii < data[i].Objects_detected; ii++){
     $(".wrapper#"+i).find(".subWrapper").append(getSubTemplate(objectsClassname, x, y, width, height));
    }
    
  } else {
   $(".content").append(getTemplate(i, frameCount, 0));
  }
}

$(".frame").on("click", function(){
 var $clickedParent = $(this).parent();
  $(this).toggleClass("open");
  var $subWrapper = $clickedParent.find(".subWrapper");
  
  if($subWrapper.css("display") == "none"){
   $subWrapper.css("display", "block");
  } else if($subWrapper.css("display") == "block"){
   $subWrapper.css("display", "none");  
  }
});

$(".className").on("click", function(){
 var $toOpen = $(this).next();
  $(this).toggleClass("open");
  
  if($toOpen.css("display") == "none"){
   $toOpen.css("display", "block");
    $toOpen.children().css("display", "block");
  } else if($toOpen.css("display") == "block"){
   $toOpen.css("display", "none");
    $toOpen.children().css("display", "none");
  }
});
@charset "UTF-8";
body {
  font-family: sans-serif;
}

*:before, *:after {
  color: #3a3a3a;
  font-weight: bold;
}

.wrapper {
  width: 100%;
  color: #e24444;
}
.wrapper .frame:before {
  content: "Frame: ";
}
.wrapper .frame:after {
  content: " ►";
}
.wrapper .subWrapper {
  margin-left: 10%;
  display: none;
}
.wrapper .subWrapper .objects:before {
  content: "Objects Detected: ";
}
.wrapper .subWrapper .className:before {
  content: "Class Name: ";
}
.wrapper .subWrapper .className:after {
  content: " ►";
}
.wrapper .open:after {
  content: " ▼" !important;
}
.wrapper .subSubWrapper {
  margin-left: 20%;
  display: none;
}
.wrapper .subSubWrapper .co-ordinates:before {
  content: "Co-ordinates: ";
}
.wrapper .subSubSubWrapper {
  margin-left: 30%;
  display: none;
}
.wrapper .subSubSubWrapper .x:before {
  content: "X: ";
}
.wrapper .subSubSubWrapper .y:before {
  content: "Y: ";
}
.wrapper .subSubSubWrapper .width:before {
  content: "Width: ";
}
.wrapper .subSubSubWrapper .height:before {
  content: "Height: ";
}

.visible {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
  
</div>