简体   繁体   English

如何深入到JSON树的底部

[英]How to get to the bottom of a JSON tree

I come to you today with maybe a noob-ish question. 今天,我可能会提出一个新手问题。 I'm trying to parse a JSON response from the server on my client side but I seem to have hit a wall as I can't actually acces the data inside of it. 我正在尝试从客户端服务器解析JSON响应,但由于无法实际访问其中的数据,因此似乎遇到了麻烦。 Here's the JSON content I'm trying to go through. 这是我要尝试的JSON内容。 I just need the name as in name:"Topic3" 我只需要名称中的name:"Topic3"

'{
 links: [ ],
 content: [
    {
       links: [ ],
       content: {
          name: "Topic3",
          technology: {
             name: "Java",
             technology_id: 1
           },
           topic_id: 3
       },
   id: null
   },
   ],
   id: null
}'

Any help would be greatly appreciated. 任何帮助将不胜感激。 Also this is my JS code that I tried to work on it with. 这也是我尝试使用的JS代码。

$.ajax({
            type:'GET',
            dataType: 'json',
            url: "technologies/"+ option+ "/topics"
        }).then(function(data)
        {
            for(i=0;i<data.content.length;i++)
            {
                var topic = document.createElement("button");
                var content= (Object.keys(data.content[i]));
                topic.name= content.content.name;
                AddTopic.appendChild(topic);
            }
        });

To set button text use innerHTML property 要设置按钮文本,请使用innerHTML属性

            var topic = document.createElement("button");
            var content = data.content[i];
            topic.innerHTML = content.content.name;
            AddTopic.appendChild(topic);

So I'm assuming you need to iterate through and find every available topic - in that case I believe this should work 因此,我假设您需要遍历并找到每个可用的主题-在这种情况下,我认为这应该可行

 $.ajax({ type:'GET', dataType: 'json', url: "technologies/"+ option+ "/topics" }).then(function(data) { for(i of data.content) { var topic = document.createElement("button"); topic.name= i.content.name; AddTopic.appendChild(topic); } }); 

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

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