【问题标题】:JSON from javascript in index.html fileindex.html 文件中来自 JavaScript 的 JSON
【发布时间】:2014-02-28 00:01:09
【问题描述】:

我有一个我正在尝试使用 Json 构建的简历结构。简历部分如下:

Cisco
- Software Engineer
    - Try different technologies to make user friendly applications
    - Playing with javascript and Json to create a personal website 

我有一个 JSON 结构如下:

{
"sections":
[
{
  "company": "Cisco",
  "title":"Software Engineer",
  "desc": 
        [
        {
          "line1": "Try different technologies to make user friendly applications",
          "line2": "Playing with javascript and Json to create a personal website"
        }
        ]
}
] 
}

我正在使用 index.html 中的以下脚本来获取 json 数据:

  <div ng:switch-when="job" itemscope itemprop="organization" itemtype="http://schema.org/Organization">
            <section class="job {{isLast(section.data.length,$index)}}"  ng:repeat="job in section.data">
             <h2>{{job.company|microdata:"name"}}{{job.url|external}}</h2>
             <h3>{{job.title}}</h3>
             <h2>{{job.desc.line1}}</h2>
             <h2>{{job.desc.line2}}</h2>
             </section>
</div>

我没有得到 line1 & line2 的预期输出。是我做错了什么还是渗透到了键值对中的第 1 级?

我是一个完整的 json 和 javascript 新手,所以请指导。谢谢。

【问题讨论】:

    标签: javascript jquery html json angularjs


    【解决方案1】:

    Desc 是一个数组,其中包含一个对象。您可能想要更改 JSON,从 data 中删除 [] 括号,然后将其设为对象(即 {})。

    {
        "sections": [{
            "company": "Cisco",
            "title": "Software Engineer",
            "desc": {
                "line1": "Try different technologies to make user friendly applications",
                "line2": "Playing with javascript and Json to create a personal website"
            }
        }]
    }
    

    【讨论】:

      【解决方案2】:

      您已将 desc 创建为一个数组,如果您想在不更改 HTML 的情况下使用它,请将“desc”更改为:

      "desc": {
                "line1": "Try different technologies to make user friendly applications",
                "line2": "Playing with javascript and Json to create a personal website"
              }
      

      【讨论】:

        【解决方案3】:

        在您的数据中, desc 是一个数组。我建议对您的数据模型稍作更改(在您的源或控制器中进行)以通过 ng-repeat 指令利用数组值。

        {
              "company": "Cisco",
              "title":"Software Engineer",
              "desc": [
                 "Try different technologies to make user friendly applications",
                  "Playing with javascript and Json to create a personal website"
              ]
        }
        

        请参阅jsfiddle example

        【讨论】:

        • 在这种情况下,他还必须更改 HTML。
        • 当然,但这会更干净(干:一行模板处理所有潜在的行)。这种数据结构远非正确。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多