简体   繁体   English

存储使用javascript返回的json数据

[英]Store json data returned using javascript

I have json string retuned via a ajax call 我通过ajax调用重新调整了json字符串

$.ajax({
            type: 'GET',
            url: quoteURL,
            dataType: 'json',
            timeout: 10000,
            crossDomain: true,
                 success: function(result) {
                /// required code       
               }

});

The returned json response from server is 从服务器返回的json响应是

{
   _emptyscopedata: [
      {},
      {}
   ],
   errMsgBuffer: {
      errMsg: ''
   },
   descriptor: [
      {
         template: 'projects/mobile/market/mostactives.xsl',
         componentname: 'getmostactives'
      },
      {
         template: '',
         componentname: 'quotelist'
      }
   ],
   'invest.mostactive': {
      MoverExchange: 'NSDQ',
      MoverType: 'ACT',
      urlType: ''
   },
   quotelist: {},
   'quote.quote': [
      {
         timezoneid: 'EST',
         change: '0.01',
         halted: '0',
         type: 'EQ',
         bidsize: '2900',
         fastmarket: '0',
         asksize: '300',
         close: '16.64',
         timestamp: 'May 18, 2011 3:00 PM EST',
         open: '16.64',
         productid: 'CSCO:NSDQ:EQ',
         bid: '16.63',
         exchange: 'NSDQ',
         symbol: 'CSCO',
         news: '0',
         quotetype: '2',
         percentchange: '0.0006',
         symboldesc: 'CISCO SYS INC COM',
         price: '16.65',
         utctime: '1305748800',
         volume: '92738240',
         high: '16.66',
         quotestatus: '0',
         low: '16.34',
         ask: '16.64',
         timestring2: '05/18/11 04:00 PM ET'
      },
      {
         timezoneid: 'EST',
         change: '0.04',
         halted: '0',
         type: 'EQ',
         bidsize: '91200',
         fastmarket: '0',
         asksize: '241000',
         close: '2.14',
         timestamp: 'May 18, 2011 3:00 PM EST',
         open: '2.13',
         productid: 'SIRI:NSDQ:EQ',
         bid: '2.17',
         exchange: 'NSDQ',
         symbol: 'SIRI',
         news: '0',
         quotetype: '2',
         percentchange: '0.0187',
         symboldesc: 'SIRIUS XM RADIO INC COM',
         price: '2.18',
         utctime: '1305748800',
         volume: '74540998',
         high: '2.2',
         quotestatus: '0',
         low: '2.12',
         ask: '2.18',
         timestring2: '05/18/11 04:00 PM ET'
      }
   ]
}

I want to show some of the values of the response on the site. 我想在网站上显示一些响应值。 But I am not able to retrieve the values. 但我无法检索这些值。 Can someone help. 有人可以帮忙吗

$.ajax({ 
    type: 'GET', 
    url: quoteURL, 
    dataType: 'json', 
    timeout: 10000, 
    crossDomain: true, 
    success: function(result) {
        alert(result.descriptor[0].template);
    }
});

and if you wanted to loop through all descriptors: 如果你想循环遍历所有描述符:

$.each(result.descriptor, function() {
    var template = this.template;
    var componentname = this.componentname;
    // TODO: process the template and componentname
});

or: 要么:

alert(result['invest.mostactive'].MoverExchange);

etc... depending on what you want to show 等等......取决于你想要展示的内容

You can use define a global variable in case you want to use the response out the success function scope. 如果要使用成功函数范围内的响应,可以使用定义全局变量。 Just use 只是用

var cachedResp;//at global scope
$.ajax({ 
    type: 'GET', 
    url: quoteURL, 
    dataType: 'json', 
    timeout: 10000, 
    crossDomain: true, 
    success: function(result) {
        cachedResp = result; //this is now available out of the function
    }
});

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

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