【问题标题】:Access values in the JSO访问 JSO 中的值
【发布时间】:2016-01-22 18:00:06
【问题描述】:

我正在尝试使用 JSONP 帖子通过 JavaScript 客户端应用程序调用 Alchemy News API。

我的 JSON 结果如下所示:

<results>
<status>OK</status>
<usage>
By accessing AlchemyAPI or using information generated by AlchemyAPI
</usage>
<totalTransactions>50</totalTransactions>
<result>
   <docs>
      <element>
         <id>ODE0ODI3NTg3MHwxNDUzMzc2NDkz</id>
         <source>
            <enriched>
               <url>
                  <title>
                     Mich. Company Offers $15 Million To Acquire Firm
                  </title>
               </url>
          </enriched>
      </source>
      <timestamp>1453376493</timestamp>
</element>

我可以使用以下代码成功调用API(取自How do I get the JSON from an API request into my page's javascript?

 function loadDoc() {
        $.ajax({
            url: 'https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=MY_KEY&outputMode=json&outputMode=json&start=now-7d&end=now&count=1&return=enriched,original',
            dataType: 'jsonp',
            jsonp: 'jsonp',
            type: "get",
                success: function (res) {
                if (res["status"] === "OK") {
                    alert("Good!");
                }
                else if (res["status"] === "ERROR") {
                    alert("Bad!");
                }
            },
            error: function (jqxhr) {
                //console.log(jqxhr);
            }
        });
}

我真正苦苦挣扎的是访问返回对象中的数据。
我可以毫无问题地读取诸如 status 之类的顶级值,但我似乎可以从架构中更深层次的信息中加载任何值。

谁能发布我如何访问 title 字段中的数据?

非常感谢。

【问题讨论】:

    标签: alchemyapi


    【解决方案1】:

    啊,我找到了解决方案(我没有意识到 docs 是一个数组)。这是对任何人有用的代码。

    function loadDoc() {
        $.ajax({
            url: 'https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=key&outputMode=json&outputMode=json&start=now-1d&end=now&count=2&q.enriched.url.enrichedTitle.relations.relation=|action.verb.text=acquire,object.entities.entity.type=Company|&return=enriched.url.title',
            dataType: 'jsonp',
            jsonp: 'jsonp',
            type: "get",
            data: { },
            success: function (data) {
                if (data["status"] === "OK") {
    
                var html = '';
                results = data.result.docs;
    
                    for (var i = 0; i < results.length; i++) {
    
                        newsTitle = results[i].source.enriched.url.title;
    
                    html = html + '<a href="#">' + newsTitle + '</a></br />';
                    }
    
                $('#results').html(html);
    
                }
                else if (data["status"] === "ERROR") {
                    alert("Bad!");
                }
            },
            error: function (error) {
                //console.log(error);
            }
        });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多