简体   繁体   English

如何使用JavaScript打印JSON对象的特定部分?

[英]How can I print a specific part of a JSON object with JavaScript?

I would like to know what to use to transform the document from JSON to an array and the print the part of the array the user wants. 我想知道用什么将文档从JSON转换为数组,并打印用户想要的数组部分。 Also, how can I put it in an HTML document so the user can search any part of the array. 另外,如何将其放在HTML文档中,以便用户可以搜索数组的任何部分。

Below is the JSON . 以下是JSON

 { "A": { "1": { "1\º": [ "Semestre 1" ] }, "2": { "1\º": [ "Semestre 1" ] } }, "B": [ ], "c": { "2": { "1\º": [ "Semestre 1" ] }, "3": { "1\º": [ "Semestre 1" ] }, "44": { "1\º": [ "Semestre 1" ] }, "G6": { "1\º": [ "Semestre 1" ] }, "GP98": { "1\º": [ "Semestre 1" ] }, "654": { "1\º": [ "Semestre 1" ] }, "5556": { "1\º": [ "Semestre 1" ] }, "7654": { "1\º": [ "Semestre 1" ] } } } 

Thanks for your help. 谢谢你的帮助。

Depending on what you want your strings/collection of strings to look like you can do one of the below options. 根据您希望您的字符串/字符串集合看起来像什么,可以执行以下选项之一。

JSON.Stringify will convert a JavaScript object or value to a JSON string. JSON.Stringify将JavaScript对象或值转换为JSON字符串。

printDataWithStringify = (x) => {
  console.log(JSON.stringify(data[x]))
}

> {"1":{"1º":["Semestre 1"]},"2":{"1º":["Semestre 1"]}}

If you want to go even further, you can try the below code. 如果您想更进一步,可以尝试以下代码。

var printedStrings = []

checkNestedData = (x, y) => {

  if (typeof(x[y]) === 'object' && !Array.isArray(x[y][property1])) {
    printedStrings.push(y)
    for (var property1 in x[y]) {
      checkNestedData(x[y], property1)
    }
  } else {
    printedStrings.push(x[y]);
  }

}


printDataWithKeysAndValues = (x) => {
  var part = data[x]
  for (var property1 in part) {
    checkNestedData(part, property1)
  }
  console.log(printedStrings)
}

> 1,1º,Semestre 1,2,1º,Semestre 1

The above code utilizes a for...in loop which is used to loop through JavaScript objects. 上面的代码利用了for...in循环,该循环用于遍历JavaScript对象。 part is the object you get when you retrieve information from data at the key of x (in this case, "A" ). part是从x的键(在本例中为"A" )中的data中检索信息时获得的对象。 property1 represents the key for the current object ( part ) and is the iterator for the for...in loop as we loop through part . property1表示当前对象( part )的键,并且是我们遍历part for...in循环的迭代器。

To take it even further, checkNestedData checks to see if there is another object nested in the current object. 为了更进一步, checkNestedData检查是否在当前对象中嵌套了另一个对象。 If it's an object (that doesn't have a array for a child), you push y (remember this is the key ( property1 ) from the original loop) into the intialized printedStrings array. 如果它是一个对象(没有用于孩子的数组),则将y (请记住,这是原始循环中的键( property1 ))推入初始化的printedStrings数组中。 Then perform another loop and you run checkNestedData recursively on the current iterative in the new loop. 然后执行另一个循环,然后对新循环中的当前迭代递归运行checkNestedData

This recursive callback will run until the last child is not a populated object. 此递归回调将一直运行,直到最后一个子代不是填充对象为止。

Once we've looped through the entire object, storing the keys and values we've extracted from the object (including nested objects) we simply console.log the final array which contains all of our keys and value from that portion ( "A" ) of data. 一旦遍历整个对象,存储从对象中提取的键和值(包括嵌套对象),我们就可以简单地console.log最终数组,其中包含该部分中所有的键和值( "A" ) 数据的。

Depending on how you want the string to be formatted, you can append/remove things from the strings via interpolation or concatenation. 根据您希望字符串格式化的方式,您可以通过插值或串联从字符串中追加/删除内容。 But this solution should get you every key and value and store them as a string in an array. 但是此解决方案应该为您提供每个键和值,并将它们作为字符串存储在数组中。

 var data = { "A": { "1": { "1\º": [ "Semestre 1" ] }, "2": { "1\º": [ "Semestre 1" ] } }, "B": [ ], "c": { "2": { "1\º": [ "Semestre 1" ] }, "3": { "1\º": [ "Semestre 1" ] }, "44": { "1\º": [ "Semestre 1" ] }, "G6": { "1\º": [ "Semestre 1" ] }, "GP98": { "1\º": [ "Semestre 1" ] }, "654": { "1\º": [ "Semestre 1" ] }, "5556": { "1\º": [ "Semestre 1" ] }, "7654": { "1\º": [ "Semestre 1" ] } } } printDataWithStringify = (x) => { console.log('STRINGIFY: ' + JSON.stringify(data[x])) } var printedStrings = [] checkNestedData = (x, y) => { if (typeof(x[y]) === 'object' && !Array.isArray(x[y][property1])) { printedStrings.push(y) for (var property1 in x[y]) { checkNestedData(x[y], property1) } } else { printedStrings.push(x[y]); } } printDataWithKeysAndValues = (x) => { var part = data[x] for (var property1 in part) { checkNestedData(part, property1) } console.log('ALL KEYS AND VALUES: ' + printedStrings) } printDataWithStringify("A") printDataWithKeysAndValues("A") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Javascript 中打印 json 的特定部分? - How to print specific part of json in Javascript? 我 JSON.stringify 一个对象,但如何在 Javascript 中打印出对象的特定元素 - I JSON.stringify an object but how do I print out specific elements of object in Javascript 如何在 javascript 中打印此 json object? - How to print this json object in javascript? 我如何 JSON 只编码 Javascript object 的一部分? - How do I JSON encode only part of a Javascript object? 我如何使用nodejs select json 数据的特定部分? - How can i select a specific part of json data with nodejs? 在JavaScript中,如何在将JSON对象循环使用时将多个数组转换为HTML表并打印它们 - In JavaScript how can I convert multiple arrays into HTML tables and print them when for-looping a JSON object Javascript:如何从输出中选择特定零件 - Javascript: How can I Choose a specific Part from an Output 如何将特定的 Json 部分作为独立对象返回? - How to return specific Json part as independent object? 如何在Javascript中通过键名和值获取json的子级部分? - how can i get a children part of a json by keyname and value in Javascript? 我如何使用javascript或php从Web浏览器中打印页面或页面的一部分而无需打印窗口 - how i can print a page or part of the page from web browser without print window using javascript or php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM