本文介绍了尝试打印嵌套Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var key = " ";
var myBio = {
              "name":"Sathya",
               "age":"23",
            "position":"Soft.Engineer",
             "email":
                     {
                       "email1":"[email protected]",
                        "email2":"[email protected]"},
                     };

for (key in myBio){
   var y = myBio[key]; 
   console.log(key+" : "+ y);   
        }

输出:


  • 名称:Sathya

  • 年龄:23​​

  • 位置:Soft.Engineer

  • 电子邮件:[object Object]

  • name : Sathya
  • age : 23
  • position : Soft.Engineer
  • email : [object Object]

必需的输出:


  • 姓名:Sathya

  • 年龄:23​​

  • 位置: Soft.Engineer

  • 电子邮件:

    • name : Sathya
    • age : 23
    • position : Soft.Engineer
    • email :

      我可以分别打印此电子邮件使用另一个循环。但是我只想用主循环打印。使用JavaScript可以做到这一点吗?

      I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??

      推荐答案

      尝试一下:

      var myBio = {
          "name":"Sathya",
          "age":"23",
          "position":"Soft.Engineer",
          "email": {
              "email1":"[email protected]",
              "email2":"[email protected]"
          }
      };
      
      var result = parseJSON(myBio);
      
      console.log(result);
      
      function parseJSON(obj) {
          var parsedData = '';
          for(var i in obj) {
              if (typeof obj[i] == 'object') {
                  parsedData += parseJSON(obj[i]);
              }else {
                  parsedData += i +' : '+ obj[i];
              }//end if
              parsedData += '\n';
          }//end for
          return parsedData;
      }//end function

      这篇关于尝试打印嵌套Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:28