如何在json2html库中使用li标签

时间:2016-03-02 11:15:53

标签: javascript html json json2html

我正在使用Json2Html将json转换为Html。我发现使用li标签很困难。非常感谢任何帮助。

 var data = [{
      "testSuite": [{
          "testCase": [{
              "testCaseName": "tc1",
              "testStep": [{
                  "result": "true",
                  "testStepName": "ts1",
                  "screenShot": "image"
                }, {
                  "result": "true",
                  "testStepName": "ts2",
                  "screenShot": "image"
                }] //End of TestStep
            },

          ],
          "testSuiteName": "testSuite1",
        }] // End of testSuite
    }];

var transform = {
    // Printing the Execution stack 

    "testSuite": {

      "tag": "ul",
      "children": function() {

        return (json2html.transform(this.testSuite, transform.testSuiteName));
      }
    },
    "testSuiteName": {
      "tag": "li",
      "html": "${testSuiteName}",
      "children": function() {
        return ('<ul>' + json2html.transform(this.testCase, transform.testCaseNameRetrieval) + '</ul>');
      }
    },

    "testCaseNameRetrieval": {
      "tag": "li",

      "children": function() {
        return (json2html.transform(this, transform.testCaseName));
      }
    },

    "testCaseName": {
      "tag": "li",
      "html": "${testCaseName}",
      "children": function() {
        return (json2html.transform(this.testStep, transform.testStepRetrieval));
      }
    },

    "testStepRetrieval": {
      "tag": "li",
      "children": function() {
        return ('<ul>' + json2html.transform(this, transform.testStep) + '</ul>');
      }
    },

    "testStep": {
      "tag": "li",

      "html": "${testStepName}",
      "children":

        return ('<ul>' + json2html.transform(this, transform.testStepResultDescription) + '</ul>');
    }

  },


  "testStepResultDescription": {
    "tag": "li",
    "children": [{
      "tag": "div",
      "html": "${screenShot}               -              ${result}"
    }]
  }
}; //End of HTML template definition(transform)

$('#json').json2html(data, transform.testSuite);

从上述过渡生成的Html:

 <li>
       testSuite1
       <ul>
          <li>
          <li>tc1
          <li> 
             <ul>
                <li>
                   ts1
                   <ul>
                      <li>
                         <div>image               -              true</div>
                      </li>
                   </ul>
                </li>
             </ul>
          </li> 
          <li>
             <ul>
                <li>
                   ts2
                   <ul>
                      <li>
                         <div>image               -              true</div>
                      </li>
                   </ul>
                </li>
             </ul>
          </li>
          </li></li>
       </ul>
    </li>

我想要的Html格式是

<li>
   testSuite1
   <ul>
        <li>tc1
         <ul>
            <li>
               ts1
               <ul>
                  <li>
                     <div>image               -              true</div>
                  </li>
               </ul>
            </li>
         </ul>
<ul>
            <li>
               ts2
               <ul>
                  <li>
                     <div>image               -              true</div>
                  </li>
               </ul>
            </li>
         </ul>
      </li>
       </ul>
</li>

请帮我删除json2html过渡中的额外li标签。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。

    var data = [{
      "testSuite": [{
          "testCase": [{
            "testCaseName": "tc1",
            "testStep": [{
                "result": "true",
                "testStepName": "ts1",
                "screenShot": "image"
              }, {
                "result": "true",
                "testStepName": "ts2",
                "screenShot": "image"
              }] //End of TestStep
          }, ],
          "testSuiteName": "testSuite1",
        }] // End of testSuite
    }];

    var transform = {
    // Printing the Execution stack 

        "testSuite": {
            "tag": "ul",
            "children": function() {
                return (json2html.transform(this.testSuite, transform.testSuiteName));
            }
        },

        "testSuiteName": {
            "tag": "li",
            "html": "${testSuiteName}",
            "children": function() {
                return ('<ul>' +json2html.transform(this.testCase, transform.testCaseName) + '</ul>');
            }
        },

        "testCaseName": {
            "tag": "li",
            "html": "${testCaseName}",
            "children": function() {
                return (json2html.transform(this.testStep, transform.testStepRetrieval));
            }
        },

        "testStepRetrieval": {
            "tag": "ul",
            "children": function() {
                return (json2html.transform(this, transform.testStep));
            }
        },

        "testStep": {
            "tag": "li",
            "html": "${testStepName}",
            "children":function(){
              return ('<ul>' + json2html.transform(this, transform.testStepResultDescription) + '</ul>');
             }
         },


    "testStepResultDescription": {
        "tag": "li",
        "children": [{
            "tag": "div",
            "html": "${screenShot}               -              ${result}"
         }]
     }
  };

希望这会对你有所帮助。