简体   繁体   English

如何使用javascript从JSON返回的对象中提取特定值?

[英]How do I extract a specific value from a JSON returned object with javascript?

I'm doing the codecademy javascript API tutorial using calls to the Sunlight Foundations API. 我正在使用对Sunlight Foundations API的调用来编写codecademy javascript API教程。 i have completed the full codecacemy javascript course, and may be missing soemthign simple. 我已经完成了完整的codecacemy javascript课程,并且可能缺少soemthign简单。

I would like to extract the number of results from the returned JSON Object, which is currently being displayed in HTML thanks to jquery. 我想从返回的JSON对象中提取结果的数量,这要归功于jquery,它目前正在HTML中显示。

here is a sample of the returned object: 这是返回对象的示例:

{
"num_found": 347,
"results": [
    {
        "speaker_state": "NC",
        "speaker_first": "Virginia",
        "congress": 111,
        "title": "CULTIVATING AMERICAN ENERGY RESOURCES",
        "origin_url": "http://origin.www.gpo.gov/fdsys/pkg/CREC-2009-07-30/html/CREC-2009-07-30-pt1-PgH9197.htm",
        "number": 117,
        "pages": "H9197-H9203",
        "volume": 155,
        "chamber": "House",
        "session": 1,
        "speaking": [
            "Well, I think that this is a great segue to talk about the other subject that we wanted to talk about tonight, which is health care, and what is happening with the health care debate."
        ],
        "capitolwords_url": "http://capitolwords.org/date/2009/07/30/H9197_cultivating-american-energy-resources/",
        "speaker_party": "R",
        "date": "2009-07-30",
        "bills": null,
        "bioguide_id": "F000450",
        "order": 14,
        "speaker_last": "Foxx",
        "speaker_raw": "ms. foxx"
    },
    ...
]

} }

Here is the last statement that shows the results: 这是显示结果的最后一条语句:

  var query_params = { apikey: 'f6ab5f2e4f69444b9f2c0a44d9a5223d',
                   phrase: word
                 };

  var endpoint = 'http://capitolwords.org/api/text.json'

  var options = {
    data: query_params,
    type: 'GET',
    dataType: 'jsonp'      
  };

    var request = jQuery.ajax(endpoint, options)
                        .done(showResponse);

If the last line above returns the object, how do I chain a method onto .done() which will extract for example, "num_found"? 如果上面的最后一行返回了对象,我如何将方法链接到.done()上,该方法将提取例如“ num_found”?

That's the hint provided is to chain a method, but I don't quite get this. 提供的提示是链接方法,但我不太明白。 Thanks for any help. 谢谢你的帮助。

You can execute a function after the AJAX request has (successfully) finished by adding the "success" option: 您可以在AJAX请求(成功)完成之后执行功能,方法是添加“成功”选项:

var query_params = { 

   apikey:    'f6ab5f2e4f69444b9f2c0a44d9a5223d',
   phrase:    word
};

$.ajax({

   url:       'http://capitolwords.org/api/text.json',
   data:      query_params,
   type:      'GET',
   dataType:  'jsonp',
   success:   showResponse
});

function showResponse(response) {

   alert(response.num_found);
}

It will be passed the response from the server as an object, because jQuery does all the complicated JSONP work for you. 它将作为对象从服务器传递响应,因为jQuery为您完成了所有复杂的JSONP工作。 This example will execute the function showResponse and and alert the value of num_found. 此示例将执行函数showResponse并警告num_found的值。

This might also help you: http://learn.jquery.com/ajax/working-with-jsonp/ 这也可能对您有帮助: http : //learn.jquery.com/ajax/working-with-jsonp/

Not sure but I think this is what you want: 不确定,但我认为这是您想要的:

.done(function(data){
     var myVar = .....
     showResponse(myVar);
});

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

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