简体   繁体   English

如何在JavaScript中加载和打印JSON数组/对象

[英]How to load and print json array/object in javascript

I do not have any json and d3 knowledge (just started to read few hours back) but have very basic javascript knowledge. 我没有任何json和d3知识(刚刚开始阅读几个小时),但是具有非常基本的javascript知识。 I have to load a json file and print all the array and objects on the console using d3. 我必须加载json文件并使用d3在控制台上打印所有数组和对象。 I was wondering if anyone can help me to solve it. 我想知道是否有人可以帮助我解决问题。 Actually, I did it but does not work :( My json file. 其实,我做到了,但不起作用:(我的json文件。

 {
    "addressfile": "info", 
        "struct": {
          "address": [
               [
                  "A", 
                  "B", 
               ], 
               [
                  "B", 
                  "C", 
               ], 
         ], 

           "address1": {
           "address2": {
           "address3": {
                  "zip": [
                      "NUMBER", 
                        0
                     ]
                  }, 
            "address_type": "Home"
            }, 
         }
     }, 
    "COUNTRY": {}, 
    }

My javascript code... 我的JavaScript代码...

<!DOCTYPE html>
<meta charset="utf-8">
<style>
<body>
<script>
//LOADING JSON FILE
d3.json("address.json", function(error, root) {
      if (error) return console.error(error);
          for (var p in location) if (location.hasOwnProperty(p)) {
               console.log(p + " : " + location[p]);
           }
       }
 </script>
 </body> 
 </html>

Please help me to solve it... 请帮我解决...

Try 尝试

d3.json("appinfo.json", function(location) { 

I know the docs say the callback takes two parameters, in a project I recently did with d3 version 3.4.13, the callback function would only work if I only passed it the data parameter. 我知道文档说回调函数有两个参数,在我最近使用d3版本3.4.13的项目中,仅当我仅将data参数传递给回调函数时,回调函数才有效。

your code missing a closing parenthesis - ); 您的代码缺少右括号-);

correct code would be : 正确的代码是:

<script>
    d3.json("appinfo.json", function(error, root) {
         if (error) return console.error(error);

          console.log(root) // output -your JSON data as pojo

          //for (var p in location) if (location.hasOwnProperty(p)) {
          //     console.log(p + " : " + location[p]);
          //}
    });
</script>
  • make sure you write correct url to your json file in first parameter to d3.json(). 确保在d3.json()的第一个参数中将正确的url写入json文件。
  • make sure that appinfo.json contain correct json object, you can test it at http://jsonlint.com/ 确保appinfo.json包含正确的json对象,您可以在http://jsonlint.com/上对其进行测试

Thanks everyone.. After struggling I got the solution.. Solution: External Json files are not supported by the browsers ..so I needed to use webserver. 谢谢大家。.经过努力后,我得到了解决方案..解决方案:浏览器不支持外部Json文件..所以我需要使用Web服务器。 Then I was able to see the output of it in the console. 然后,我可以在控制台中看到它的输出。 the final code: 最终代码:

<!DOCTYPE html>
<meta charset="utf-8">
 <head>
   <script src="http://d3js.org/d3.v3.min.js"></script>
 </head>
 <body>
   <script>
        d3.json("address.json", function(location) {
        console.log(location)
  });
   </script>
 </body>
 </html>

Hope it might help others to solve in minutes not in hours like me..... 希望它可以帮助别人在几分钟之内解决问题,而不是像我这样的小时。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM