0

当 json2html ( https://github.com/moappi/json2html ) 收到带有 array_of_objects 的 JSON 时。能够使用某些对象字段的值配置转换。我发现必须在转换配置中使用以下“数字”来解决我的转换。我的意思是,我需要使用下一个转换符号来解析我的 JSON:

array_of_objects。NUMBER .field_of_object

(解析器配置中用于获取对象字段值的最后一个文本字符串)

如何避免使用此号码?

请参阅下一个 JSON 示例。您可以在其中检查一些配置字符串,我添加到 JSON 作为示例,向您展示我想避免使用 NUMBER 的解析配置字符串:

“HITS__object.NUMBER .TIPO__object_field”

之后你会发现我目前正在使用的解析配置。

提前致谢

JSON WITH PARSING CONFIGURATION NOTES 

"hits": [                                   <-- OBJECT'S ARRAY hits
      {                                         <-- OBJECT ELEMENT 1
        "_index": "catalogo",  
        "_type": "default",
        "_id": "WCkCRHABF6tpWazT-hOd",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NO EXISTE", <--TO ACCESS IT'S VALUE YOU NEED TO PUT IN CONFIGURATION: hits.0.Tipo  (I don't want to use the number 0)
          "Título": "doc.odt",
          "Versión": "?",
          "Fecha creación": "2019-11-20T23:00:00Z",
          "Última Edición": "2019-12-09T23:00:00Z",
          "Estado": "NO EXISTE",
          "Clasificación": "NO EXISTE",
          "Utiliza plantilla": "si",
          "Alertar de": " "
        },

      },

      {                                         <-- OBJECT ELEMENT 2
        "_index": "catalogo",    
        "_type": "default",
        "_id": "eikDRHABF6tpWazTChYH",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NA", <--TO ACCESS IT'S VALUE YOU NEED TO PUT IN CONFIGURATION: hits.1.Tipo (I don't want to use the number 1)
          "Título": "doc.odp",
          "Versión": "?",
          "Fecha creación": "2019-10-30T23:00:00Z",
          "Última Edición": "2019-11-04T23:00:00Z",
          "Estado": "NA",
          "Clasificación": "NA",
          "Utiliza plantilla": "NA"
        },

      },
      {                                           <-- OBJECT ELEMENT 3
        "_index": "catalogo",
        "_type": "default",
        "_id": "zykCRHABF6tpWazT8xKK",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "VACIO",   <--TO ACCESS IT'S VALUE YOU NEED TO PUT IN CONFIGURATION: hits.2.Tipo  (I don't want to use the number 2)
          "Título": "otro.odt",
          "Versión": "1.1",
          "Fecha creación": "2019-09-18T22:00:00Z",
          "Última Edición": "2019-09-20T22:00:00Z",
          "Descripción": "Procedimiento",
          "Etiqueta": "sistemas",
          "Estado": "VACIO",
          "Utiliza plantilla": "si"
        },

      },  …   

PARSING CONFIGURATION 

 var transform =  {"tag":"table", "children":[
    {"tag":"tbody","children":[
        {"tag":"tr","children":[
            {"tag":"td","html":"${hits.0.Tipo}"}                  
        ]}
    ]}
]};

**** **** **** 新内容问题 **** **** ****

我要重新构建问题,问题是一样的

使用以下代码,我可以获得“_index”字段的值

<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8' />
    <title>JSON Visualizer</title>

    <!-- Add jQuery -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <!-- Bootstrap & Font Awesome  -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">

    <!-- Add JSON2HTML -->
    <script src='js/json2html.js' type='text/javascript'></script>
    <script src='js/jquery.json2html.js' type='text/javascript'></script>

    <!-- Le styles -->
    <link href="css/google-code-prettify/prettify.css" rel="stylesheet">
    <link href='css/layout.css' media='all' type='text/css' rel='stylesheet'/>
    <link href='css/index.css' media='all' type='text/css' rel='stylesheet'/>

    <!-- Main Scripts -->

</head>

<body>
 <div id="target_div"></div>

<script type="text/javascript">


var transforms =  {  
    "response":{"tag":"li","children":function() {
        return(json2html.transform(this.response,transforms.hitos));
    }},
    "hitos":{"tag":"li","children":[
        {"tag":"div","html":"${hitas.hits.1._index}"}
    ]}
};


var data = [{response:[

{
  "took": 3007,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hitas": {
    "total": 15000,
    "max_score": null,
    "hits": [
      {
        "_index": "catalogo1",
        "_type": "default",
        "_id": "WCkCRHABF6tpWazT-hOd",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NO EXISTE",
          "Título": "TEST.odt",
          "Versión": "?",
          "Fecha creación": "2019-11-20T23:00:00Z",
          "Última Edición": "2019-12-09T23:00:00Z",
          "Descripción": "Procedimiento",
          "Estado": "NO EXISTE",
          "Clasificación": "NO EXISTE",
          "Utiliza plantilla": "si",
          "Alertar de": " "
        },
        "fields": {
          "Última Edición": [
            "2019-12-09T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-11-20T23:00:00.000Z"
          ]
        },
        "sort": [
          1574290800000
        ]
      },
      {
        "_index": "catalogo2",
        "_type": "default",
        "_id": "eikDRHABF6tpWazTChYH",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NA",
          "Título": "PRE.odp",
          "Versión": "?",
          "Fecha creación": "2019-10-30T23:00:00Z",
          "Última Edición": "2019-11-04T23:00:00Z",
          "Descripción": "Presentacion",
          "Autor": "DESCONOCIDO",
          "Etiqueta": "base de datos",
          "Estado": "NA",
          "Clasificación": "NA",
          "Utiliza plantilla": "NA"
        },
        "fields": {
          "Última Edición": [
            "2019-11-04T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-10-30T23:00:00.000Z"
          ]
        },
        "sort": [
          1572476400000
        ]
      },
      {
        "_index": "catalogo3",
        "_type": "default",
        "_id": "zCkCRHABF6tpWazT8xKK",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "VACIO",
              "Título": "Cambios.odt",
          "Versión": "1.0",
          "Fecha creación": "2019-10-28T23:00:00Z",
          "Última Edición": "2018-10-30T23:00:00Z",
          "Descripción": "Cambios",
          "Etiqueta": "sistemas",
          "Estado": "VACIO",
          "Clasificación": "VACIO",
          "Utiliza plantilla": "si"
        },
        "fields": {
          "Última Edición": [
            "2018-10-30T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-10-28T23:00:00.000Z"
          ]
        },
        "sort": [
          1572303600000
        ]
      },


      .......

$('#target_div').html(json2html.transform(data,transforms.response));

</script>
</body>


</html>


RESPONSE OBTAINED
*
*
catalogo2

但是使用以下代码,要获取 _index 字段的值,我没有得到任何结果

<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8' />
    <title>JSON Visualizer</title>

    <!-- Add jQuery -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <!-- Bootstrap & Font Awesome  -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">

    <!-- Add JSON2HTML -->
    <script src='js/json2html.js' type='text/javascript'></script>
    <script src='js/jquery.json2html.js' type='text/javascript'></script>

    <!-- Le styles -->
    <link href="css/google-code-prettify/prettify.css" rel="stylesheet">
    <link href='css/layout.css' media='all' type='text/css' rel='stylesheet'/>
    <link href='css/index.css' media='all' type='text/css' rel='stylesheet'/>

    <!-- Main Scripts -->

</head>

<body>
 <div id="target_div"></div>

<script type="text/javascript">


var transforms =  {  
    "response":{"tag":"li","children":function() {
        return(json2html.transform(this.response.hitas.hits,transforms.hitos));
    }},
    "hitos":{"tag":"li","children":[
        {"tag":"div","html":"${_index}"}
    ]}
};


var data = [{response:[

{
  "took": 3007,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hitas": {
    "total": 15000,
    "max_score": null,
    "hits": [
      {
        "_index": "catalogo1",
        "_type": "default",
        "_id": "WCkCRHABF6tpWazT-hOd",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NO EXISTE",
          "Versión": "?",
          "Fecha creación": "2019-11-20T23:00:00Z",
          "Última Edición": "2019-12-09T23:00:00Z",
          "Descripción": "Procedimiento",
          "Etiqueta": "librería",
          "Estado": "NO EXISTE",
          "Clasificación": "NO EXISTE",
          "Utiliza plantilla": "si",
          "Alertar de": " "
        },
        "fields": {
          "Última Edición": [
            "2019-12-09T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-11-20T23:00:00.000Z"
          ]
        },
        "sort": [
          1574290800000
        ]
      },
      {
        "_index": "catalogo2",
        "_type": "default",
        "_id": "eikDRHABF6tpWazTChYH",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "NA",
          "Título": "test.odp",
          "Versión": "?",
          "Fecha creación": "2019-10-30T23:00:00Z",
          "Última Edición": "2019-11-04T23:00:00Z",
          "Descripción": "Test",
          "Autor": "DESCONOCIDO",
          "Etiqueta": "Test",
          "Estado": "NA",
          "Clasificación": "NA",
          "Utiliza plantilla": "NA"
        },
        "fields": {
          "Última Edición": [
            "2019-11-04T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-10-30T23:00:00.000Z"
          ]
        },
        "sort": [
          1572476400000
        ]
      },
      {
        "_index": "catalogo3",
        "_type": "default",
        "_id": "zCkCRHABF6tpWazT8xKK",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "VACIO",
          "Título": "titulo.odt",
          "Versión": "1.0",
          "Fecha creación": "2019-10-28T23:00:00Z",
          "Última Edición": "2018-10-30T23:00:00Z",
          "Descripción": "Título largo",
          "Etiqueta": "sistemas",
          "Estado": "VACIO",
          "Clasificación": "VACIO",
          "Utiliza plantilla": "si"
        },
        "fields": {
          "Última Edición": [
            "2018-10-30T23:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-10-28T23:00:00.000Z"
          ]
        },
        "sort": [
          1572303600000
        ]
      },     
      {
        "_index": "catalogo4",
        "_type": "default",
        "_id": "zykCRHABF6tpWazT8xKK",
        "_version": 1,
        "_score": null,
        "_source": {
          "Tipo": "VACIO",
          "Versión": "1.1",
          "Fecha creación": "2019-09-18T22:00:00Z",
          "Última Edición": "2019-09-20T22:00:00Z",
          "Descripción": "Procedimiento",
          "Estado": "VACIO",
          "Clasificación": "VACIO",
          "Utiliza plantilla": "si"
        },
        "fields": {
          "Última Edición": [
            "2019-09-20T22:00:00.000Z"
          ],
          "Fecha creación": [
            "2019-09-18T22:00:00.000Z"
          ]
        },
        "sort": [
          1568844000000
        ]
      },

       ....

}];



$('#target_div').html(json2html.transform(data,transforms.response));

</script>
</body>


</html>

RESPONSE OBTAINED

 WHITE SCREEN

对编辑 1 的回应


<html>
<head>
    <meta charset='UTF-8' />
    <title>JSON Visualizer</title>

    <!-- Add jQuery -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <!-- Bootstrap & Font Awesome  -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">

    <!-- Add JSON2HTML -->
    <script src='js/json2html.js' type='text/javascript'></script>
    <script src='js/jquery.json2html.js' type='text/javascript'></script>

    <!-- Le styles -->
    <link href="css/google-code-prettify/prettify.css" rel="stylesheet">
    <link href='css/layout.css' media='all' type='text/css' rel='stylesheet'/>
    <link href='css/index.css' media='all' type='text/css' rel='stylesheet'/>

    <!-- Main Scripts -->

</head>

<body>
 <div id="target_div"></div>

<script>


    var transforms =  {  
        "response":{"<>":"li","html":[
            {"<>":"h3","text":"Hitas ${took}"},
            {"<>":"ul","html":function() {
                return($.json2html(this.hitas.hits,transforms.hitos));
            }}
        ]},
        "hitos":{"<>":"li","html":[
            {"<>":"div","text":"${_index}"}
        ]}
    };


    var data = [

        {
            response:[
                {
                  "took": 3007,
                  "timed_out": false,
                  "_shards": {
                    "total": 1,
                    "successful": 1,
                    "skipped": 0,
                    "failed": 0
                  },
                  "hitas": {
                    "total": 15000,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "catalogo1",
                        "_type": "default",
                        "_id": "WCkCRHABF6tpWazT-hOd",
                        "_version": 1,
                        "_score": null,
                        "_source": {
                          "Tipo": "NO EXISTE",
                          "Versión": "?",
                          "Fecha creación": "2019-11-20T23:00:00Z",
                          "Última Edición": "2019-12-09T23:00:00Z",
                          "Descripción": "Procedimiento",
                          "Etiqueta": "librería",
                          "Estado": "NO EXISTE",
                          "Clasificación": "NO EXISTE",
                          "Utiliza plantilla": "si",
                          "Alertar de": " "
                        },
                        "fields": {
                          "Última Edición": [
                            "2019-12-09T23:00:00.000Z"
                          ],
                          "Fecha creación": [
                            "2019-11-20T23:00:00.000Z"
                          ]
                        },
                        "sort": [
                          1574290800000
                        ]
                      },
                      {
                        "_index": "catalogo2",
                        "_type": "default",
                        "_id": "eikDRHABF6tpWazTChYH",
                        "_version": 1,
                        "_score": null,
                        "_source": {
                          "Tipo": "NA",
                          "Título": "test.odp",
                          "Versión": "?",
                          "Fecha creación": "2019-10-30T23:00:00Z",
                          "Última Edición": "2019-11-04T23:00:00Z",
                          "Descripción": "Test",
                          "Autor": "DESCONOCIDO",
                          "Etiqueta": "Test",
                          "Estado": "NA",
                          "Clasificación": "NA",
                          "Utiliza plantilla": "NA"
                        },
                        "fields": {
                          "Última Edición": [
                            "2019-11-04T23:00:00.000Z"
                          ],
                          "Fecha creación": [
                            "2019-10-30T23:00:00.000Z"
                          ]
                        },
                        "sort": [
                          1572476400000
                        ]
                      },
                      {
                        "_index": "catalogo3",
                        "_type": "default",
                        "_id": "zCkCRHABF6tpWazT8xKK",
                        "_version": 1,
                        "_score": null,
                        "_source": {
                          "Tipo": "VACIO",
                          "Título": "titulo.odt",
                          "Versión": "1.0",
                          "Fecha creación": "2019-10-28T23:00:00Z",
                          "Última Edición": "2018-10-30T23:00:00Z",
                          "Descripción": "Título largo",
                          "Etiqueta": "sistemas",
                          "Estado": "VACIO",
                          "Clasificación": "VACIO",
                          "Utiliza plantilla": "si"
                        },
                        "fields": {
                          "Última Edición": [
                            "2018-10-30T23:00:00.000Z"
                          ],
                          "Fecha creación": [
                            "2019-10-28T23:00:00.000Z"
                          ]
                        },
                        "sort": [
                          1572303600000
                        ]
                      },     
                      {
                        "_index": "catalogo4",
                        "_type": "default",
                        "_id": "zykCRHABF6tpWazT8xKK",
                        "_version": 1,
                        "_score": null,
                        "_source": {
                          "Tipo": "VACIO",
                          "Versión": "1.1",
                          "Fecha creación": "2019-09-18T22:00:00Z",
                          "Última Edición": "2019-09-20T22:00:00Z",
                          "Descripción": "Procedimiento",
                          "Estado": "VACIO",
                          "Clasificación": "VACIO",
                          "Utiliza plantilla": "si"
                        },
                        "fields": {
                          "Última Edición": [
                            "2019-09-20T22:00:00.000Z"
                          ],
                          "Fecha creación": [
                            "2019-09-18T22:00:00.000Z"
                          ]
                        },
                        "sort": [
                          1568844000000
                        ]
                      }
                     ]
                    }
                }
            ]
        }
    ];


    var response = data[0].response;

    $('#target_div').html(json2html.transform(response,transforms.response));

</script>

</body>


</html>

谢谢

4

1 回答 1

1

如果您改为发布整个代码,那将是最好的,因为我不太确定您要在这里转换什么。如果要转换“命中”数组中的每个对象,可以执行以下操作

var transforms =  {  
			"response":{"<>":"li","html":[
				{"<>":"h3","text":"Hitas ${took}"},
				{"<>":"ul","html":function() {
					return($.json2html(this.hitas.hits,transforms.hitos));
				}}
			]},
			"hitos":{"<>":"li","html":[
				{"<>":"div","text":"${_index}"}
			]}
		};


		var data = [
			
			{
				response:[
					{
					  "took": 3007,
					  "timed_out": false,
					  "_shards": {
						"total": 1,
						"successful": 1,
						"skipped": 0,
						"failed": 0
					  },
					  "hitas": {
						"total": 15000,
						"max_score": null,
						"hits": [
						  {
							"_index": "catalogo1",
							"_type": "default",
							"_id": "WCkCRHABF6tpWazT-hOd",
							"_version": 1,
							"_score": null,
							"_source": {
							  "Tipo": "NO EXISTE",
							  "Versión": "?",
							  "Fecha creación": "2019-11-20T23:00:00Z",
							  "Última Edición": "2019-12-09T23:00:00Z",
							  "Descripción": "Procedimiento",
							  "Etiqueta": "librería",
							  "Estado": "NO EXISTE",
							  "Clasificación": "NO EXISTE",
							  "Utiliza plantilla": "si",
							  "Alertar de": " "
							},
							"fields": {
							  "Última Edición": [
								"2019-12-09T23:00:00.000Z"
							  ],
							  "Fecha creación": [
								"2019-11-20T23:00:00.000Z"
							  ]
							},
							"sort": [
							  1574290800000
							]
						  },
						  {
							"_index": "catalogo2",
							"_type": "default",
							"_id": "eikDRHABF6tpWazTChYH",
							"_version": 1,
							"_score": null,
							"_source": {
							  "Tipo": "NA",
							  "Título": "test.odp",
							  "Versión": "?",
							  "Fecha creación": "2019-10-30T23:00:00Z",
							  "Última Edición": "2019-11-04T23:00:00Z",
							  "Descripción": "Test",
							  "Autor": "DESCONOCIDO",
							  "Etiqueta": "Test",
							  "Estado": "NA",
							  "Clasificación": "NA",
							  "Utiliza plantilla": "NA"
							},
							"fields": {
							  "Última Edición": [
								"2019-11-04T23:00:00.000Z"
							  ],
							  "Fecha creación": [
								"2019-10-30T23:00:00.000Z"
							  ]
							},
							"sort": [
							  1572476400000
							]
						  },
						  {
							"_index": "catalogo3",
							"_type": "default",
							"_id": "zCkCRHABF6tpWazT8xKK",
							"_version": 1,
							"_score": null,
							"_source": {
							  "Tipo": "VACIO",
							  "Título": "titulo.odt",
							  "Versión": "1.0",
							  "Fecha creación": "2019-10-28T23:00:00Z",
							  "Última Edición": "2018-10-30T23:00:00Z",
							  "Descripción": "Título largo",
							  "Etiqueta": "sistemas",
							  "Estado": "VACIO",
							  "Clasificación": "VACIO",
							  "Utiliza plantilla": "si"
							},
							"fields": {
							  "Última Edición": [
								"2018-10-30T23:00:00.000Z"
							  ],
							  "Fecha creación": [
								"2019-10-28T23:00:00.000Z"
							  ]
							},
							"sort": [
							  1572303600000
							]
						  },     
						  {
							"_index": "catalogo4",
							"_type": "default",
							"_id": "zykCRHABF6tpWazT8xKK",
							"_version": 1,
							"_score": null,
							"_source": {
							  "Tipo": "VACIO",
							  "Versión": "1.1",
							  "Fecha creación": "2019-09-18T22:00:00Z",
							  "Última Edición": "2019-09-20T22:00:00Z",
							  "Descripción": "Procedimiento",
							  "Estado": "VACIO",
							  "Clasificación": "VACIO",
							  "Utiliza plantilla": "si"
							},
							"fields": {
							  "Última Edición": [
								"2019-09-20T22:00:00.000Z"
							  ],
							  "Fecha creación": [
								"2019-09-18T22:00:00.000Z"
							  ]
							},
							"sort": [
							  1568844000000
							]
						  }
						 ]
						}
					}
				]
			}
		];


		var response = data[0].response;

		$('#target').html(json2html.transform(response,transforms.response));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/json2html/1.4.0/json2html.min.js"></script>

 <ul id="target"></ul>

于 2020-03-10T22:32:22.033 回答