【问题标题】:How to get JSON object inside an object which is inside JSON array如何在 JSON 数组内的对象中获取 JSON 对象
【发布时间】:2019-09-21 16:09:16
【问题描述】:

我有一个聊天的 JSON 转储文件。我需要将其提取出来并打印到网站上。我是初学者,请帮助我。

我有我的 slack 工作区的 JSON 转储文件。我想从中提取一些关键细节。我想要位于 JSON 数组内部的 user_profile 对象中的 real_name。如何提取它。另外我如何提取所有 real_name 值(我的意思是有嵌套对象)。请帮我解决这个问题。

[
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "hey there",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "welcome",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "Help me",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    }
]

请帮助我,我是否也可以使用 JAVASCRIPT 和 ajax 将这些值转换为 HTML?

【问题讨论】:

  • 格式化代码
  • 请确保您粘贴您的代码或使用到目前为止所做的尝试。 “注意:”所以没有地方问“为我做”。如果你被困在某个地方,这是为了帮助。在此处提问之前,请确保您通过 google 搜索 :)。

标签: javascript arrays json multidimensional-array javascript-objects


【解决方案1】:

只需像这样遍历 JSON。 (注意空值)

var obj = [
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "hey there",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "welcome",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "Help me",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    }
];


for (var i in obj)
{
     var type = obj[i].type;
     var source_team = obj[i].source_team;

     var user_profile = obj[i].user_profile;

     var real_name = user_profile.real_name;


    alert(source_team+" : "+real_name);

}

【讨论】:

    【解决方案2】:

    如果我理解正确,您想提取真实姓名列表。 正如你所说,你正在使用 JavaScript。 将数组分配给变量让我们说 arr;

        var arr = [
        {
            "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
            "type": "message",
            "text": "hey there",
            "source_team": "TN4AF0V5W",
            "team": "TN4AF0V5W",
            "user_profile": {
                "real_name": "marvelmohinish99",
                "team": "TN4AF0V5W"
            }
        },......,
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
            "type": "message",
            "text": "Help me",
            "source_team": "TN4AF0V5W",
            "team": "TN4AF0V5W",
            "user_profile": {
                "real_name": "marvelmohinish99",
                "team": "TN4AF0V5W"
            }
        }];
    
    var list_real_name = arr.map((userObj)=>{
      return userObj['user_profile']['real_name'];
    })
    
    

    【讨论】:

    • 我得到未定义的输出。为获得输出而执行的任何先决条件操作
    【解决方案3】:

    如果您在文件(本地)或远程有 json,

    以下脚本将起作用

         <script>
                function fetchJSONFile(path, callback) {
            var httpRequest = new XMLHttpRequest();
            httpRequest.onreadystatechange = function() {
                if (httpRequest.readyState === 4) {
                    if (httpRequest.status === 200) {
                        var data = JSON.parse(httpRequest.responseText);
                        if (callback) callback(data);
                    }
                }
            };
            httpRequest.open('GET', path);
            httpRequest.send(); 
        }
    
        // this requests the file and executes a callback with the parsed result once
        //   it is available. You can give URL just in case you are provided with URL.
    
    fetchJSONFile('test.json', function(data){
            // do something with your data
            console.log(data[0].user_profile.real_name);
        });
            </script>
    

    使用 Ajax

    <script>
        $(document).ready(function(){
            $.ajax({
        url: 'test.json',
        type: "GET",
        dataType: 'json',
        success: function (data) {
            $.each(data,function(i,data)
            {
              console.log(data.user_profile.real_name);//This will loop 
          //through the result
            });
            console.log(data[0].user_profile.real_name); //Show only first //result
            }
    
         });
         });
    
    </script>
    

    别忘了在ajax代码前加上Jquery

    【讨论】:

      【解决方案4】:

      var obj = [{
          "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
          "type": "message",
          "text": "hey there",
          "source_team": "TN4AF0V5W",
          "team": "TN4AF0V5W",
          "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
          }
        },
        {
          "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
          "type": "message",
          "text": "welcome",
          "source_team": "TN4AF0V5W",
          "team": "TN4AF0V5W",
          "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
          }
        },
        {
          "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
          "type": "message",
          "text": "Help me",
          "source_team": "TN4AF0V5W",
          "team": "TN4AF0V5W",
          "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
          }
        }
      ];
      
      
      for (var i = 0; i < obj.length; i++) {
        console.log("Real name" + i + ": " + obj[i].user_profile.real_name);
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-24
        相关资源
        最近更新 更多