Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

javascript - How to read this JSON with jquery

How do I read this JSON with Jquery? Lets say I want get all the data with the "quote" object. Many thanks!

cbfunc({
    "query": {
        "count": "4",
        "created": "2010-12-01T15:21:42Z",
        "lang": "en-US",
        "diagnostics": {
            "publiclyCallable": "true",
            "url": [{
                "execution-time": "3",
                "proxy": "DEFAULT",
                "content": "http://datatables.org/alltables.env"
            },
            {
                "execution-time": "1",
                "proxy": "DEFAULT",
                "content": "http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml"
            },
            {
                "execution-time": "12",
                "proxy": "DEFAULT",
                "content": "http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=YHOO,AAPL,GOOG,MSFT"
            },
            {
                "execution-time": "18",
                "content": "select * from csv where url=@url and columns='Ask,AverageDailyVolume,Bid,AskRealtime,BidRealtime,BookValue,Change&PercentChange,Change,Commission,ChangeRealtime,AfterHoursChangeRealtime,DividendShare,LastTradeDate,TradeDate,EarningsShare,ErrorIndicationreturnedforsymbolchangedinvalid,EPSEstimateCurrentYear,EPSEstimateNextYear,EPSEstimateNextQuarter,DaysLow,DaysHigh,YearLow,YearHigh,HoldingsGainPercent,AnnualizedGain,HoldingsGain,HoldingsGainPercentRealtime,HoldingsGainRealtime,MoreInfo,OrderBookRealtime,MarketCapitalization,MarketCapRealtime,EBITDA,ChangeFromYearLow,PercentChangeFromYearLow,LastTradeRealtimeWithTime,ChangePercentRealtime,ChangeFromYearHigh,PercebtChangeFromYearHigh,LastTradeWithTime,LastTradePriceOnly,HighLimit,LowLimit,DaysRange,DaysRangeRealtime,FiftydayMovingAverage,TwoHundreddayMovingAverage,ChangeFromTwoHundreddayMovingAverage,PercentChangeFromTwoHundreddayMovingAverage,ChangeFromFiftydayMovingAverage,PercentChangeFromFiftydayMovingAverage,Name,Notes,Open,PreviousClose,PricePaid,ChangeinPercent,PriceSales,PriceBook,ExDividendDate,PERatio,DividendPayDate,PERatioRealtime,PEGRatio,PriceEPSEstimateCurrentYear,PriceEPSEstimateNextYear,Symbol,SharesOwned,ShortRatio,LastTradeTime,TickerTrend,OneyrTargetPrice,Volume,HoldingsValue,HoldingsValueRealtime,YearRange,DaysValueChange,DaysValueChangeRealtime,StockExchange,DividendYield'"
            }],
            "javascript": {
                "execution-time": "71",
                "instructions-used": "278867",
                "table-name": "yahoo.finance.quotes"
            },
            "user-time": "92",
            "service-time": "16",
            "build-version": "9962"
        },
        "results": {
            "quote": [{
                "symbol": "YHOO",
                "Ask": "16.21",
                "AverageDailyVolume": "24744700",
                "Bid": "16.20",
                "AskRealtime": "16.21",
                "BidRealtime": "16.20",
                "BookValue": "9.277",
                "Change_PercentChange": "+0.393 - +2.48%",
                "Change": "+0.393",
                "Commission": null,
                "ChangeRealtime": "+0.393",
                "AfterHoursChangeRealtime": "N/A - N/A",
                "DividendShare": "0.00",
                "LastTradeDate": "12/1/2010",
                "TradeDate": null,
                "EarningsShare": "0.77",
                "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
                "EPSEstimateCurrentYear": "0.86",
                "EPSEstimateNextYear": "0.79",
                "EPSEstimateNextQuarter": "0.19",
                "DaysLow": null,
                "DaysHigh": "16.23",
                "YearLow": "12.94",
                "YearHigh": "19.12",
                "HoldingsGainPercent": "- - -",
                "AnnualizedGain": "-",
                "HoldingsGain": null,
                "HoldingsGainPercentRealtime": "N/A - N/A",
                "HoldingsGainRealtime": null,
                "MoreInfo": "cnsprmiIed",
                "OrderBookRealtime": "N/A",
                "MarketCapitalization": "21.127B",
                "MarketCapRealtime": null,
                "EBITDA": "1.399B",
                "ChangeFromYearLow": "+3.268",
                "PercentChangeFromYearLow": "+25.26%",
                "LastTradeRealtimeWithTime": "N/A - <b>16.208</b>",
                "ChangePercentRealtime": "N/A - +2.48%",
                "ChangeFromYearHigh": "-2.912",
                "PercebtChangeFromYearHigh": "-15.23%",
                "LastTradeWithTime": "10:05am - <b>16.208</b>",
                "LastTradePriceOnly": "16.208",
                "HighLimit": null,
                "LowLimit": null,
                "DaysRange": "N/A - 16.23",
                "DaysRangeRealtime": "N/A - N/A",
                "FiftydayMovingAverage": "16.1875",
                "TwoHundreddayMovingAverage": "14.9097",
                "ChangeFromTwoHundreddayMovingAverage": "+1.2983",
                "PercentChangeFromTwoHundreddayMovingAverage": "+8.71%",
                "ChangeFromFiftydayMovingAverage": "+0.0205",
                "PercentChangeFromFiftydayMovingAverage": "+0.13%",
                "Name": "Yahoo! Inc.",
                "Notes": "-",
                "Open": "16.04",
                "PreviousClose": "15.815",
                "PricePaid": null,
                "ChangeinPercent": "+2.48%",
                "PriceSales": "3.16",
                "PriceBook": "1.70",
                "ExDividendDate": "12-May-04",
                "PERatio": "20.54",
                "DividendPayDate": "N/A",
                "PERatioRealtime": null,
                "PEGRatio": "1.40",
                "PriceEPSEstimateCurrentYear": "18.39",
                "PriceEPSEstimateNextYear": "20.02",
                "Symbol": "YHOO",
                "SharesOwned": null,
                "ShortRatio": "2.40",
                "LastTradeTime": "10:05am",
                "TickerTrend": " ++--++ ",
                "OneyrTargetPrice": "18.07",
                "Volume": "2751507",
                "HoldingsValue": null,
                "HoldingsValueRealtime": null,
                "YearRange": "12.94 - 19.12",
                "DaysValueChange": "- - +2.48%",
                "DaysValueChangeRealtime": "N/A - N/A",
                "StockExchange": "NasdaqNM",
                "DividendYield": null,
                "PercentChange": "+2.48%"
            },
            {
                "symbol": "AAPL",
                "Ask": "316.04",
                "AverageDailyVolume": "18898400",
                "Bid": "316.00",
                "AskRealtime": "316.04",
                "BidRealtime": "316.00",
                "BookValue": "52.175",
                "Change_PercentChange": "+4.87 - +1.57%",
                "Change": "+4.87",
                "Commission": null,
                "ChangeRealtime": "+4.87",
                "AfterHoursChangeRealtime": "N/A - N/A",
                "DividendShare": "0.00",
                "LastTradeDate": "12/1/2010",
                "TradeDate": null,
                "EarningsShare": "15.154",
                "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
                "EPSEstimateCurrentYear": "18.97",
                "EPSEstimateNextYear": "22.06",
                "EPSEstimateNextQuarter": "4.22",
                "DaysLow": "315.00",
                "DaysHigh": "316.50",
                "YearLow": "188.68",
                "YearHigh": "321.30",
                "HoldingsGainPercent": "- - -",
                "AnnualizedGain": "-",
                "HoldingsGain": null,
                "HoldingsGainPercentRealtime": "N/A - N/A",
                "HoldingsGainRealtime": null,
                "MoreInfo": "cnsprmiIed",
                "OrderBookRealtime": "N/A",
                "MarketCapitalization": "289.9B",
                "MarketCapRealtime": null,
                "EBITDA": "19.364B",
                "ChangeFromYearLow": "+127.34",
                "PercentChangeFromYearLow": "+67.49%",
                "LastTradeRealtimeWithTime": "N/A - <b>316.02</b>",
                "ChangePercentRealtime": "N/A - +1.57%",
                "ChangeFromYearHigh": "-5.28",
                "PercebtChangeFromYearHigh": "-1.64%",
                "LastTradeWithTime": "10:05am - <b>316.02</b>",
                "LastTradePriceOnly": "316.02",
                "HighLimit": null,
                "LowLimit": null,
                "DaysRange": "315.00 - 316.50",
                "DaysRangeRealtime": "N/A - N/A",
                "FiftydayMovingAverage": "309.455",
                "TwoHundreddayMovingAverage": "273.218",
                "ChangeFromTwoHundreddayMovingAverage": "+42.802",
                "PercentChangeFromTwoHundreddayMovingAverage": "+15.67%",
                "ChangeFromFiftydayMovingAverage": "+6.565",
                "PercentChangeFromFiftydayMovingAverage": "+2.12%",
                "Name": "Apple Inc.",
                "Notes": "-",
                "Open": "315.665",
                "PreviousClose": "311.15",
                "PricePaid": null,
                "ChangeinPercent": "+1.57%",
                "PriceSales": "4.38",
                "PriceBook": "5.96",
                "ExDividendDate": "21-Nov-95",
                "PERatio": "20.53",
                "DividendPayDate": "N/A",
                "PERatioRealtime": null,
                "PEGRatio": "0.83",
                "PriceEPSEstimateCurrentYear": "16.40",
                "PriceEPSEstimateNextYear": "14.10",
                "Symbol": "AAPL",
                "SharesOwned": null,
                "ShortRatio": "0.50",
                "LastTradeTime": "10:05am",
                "TickerTrend": " -=+-+- ",
                "OneyrTargetPrice": "365.38",
                "Volume": "3679572",
                "HoldingsValue": null,
                "HoldingsValueRealtime": null,
                "YearRange": "188.68 - 321.30",
                "DaysValueChange": "- - +1.57%",
                "DaysValueChangeRealtime": "N/A - N/A",
                "StockExchange": "NasdaqNM",
                "DividendYield": null,
                "PercentChange": "+1.57%"
            },
            {
                "symbol": "GOOG",
                "Ask": "568.94",
                "AverageDailyVolume": "3086980",
                "Bid": "568.55",
                "AskRealtime": "568.94",
                "BidRealtime": "568.55",
                "BookValue": "135.606",
                "Change_PercentChange": "+13.208 - +2.38%",
                "Change": "+13.208",
                "Commission": null,
                "ChangeRealtime": "+13.208",
                "AfterHoursChangeRealtime": "N/A - N/A",
                "DividendShare": "0.00",
                "LastTradeDate": "12/1/2010",
                "TradeDate": null,
                "EarningsShare": "24.616",
                "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
                "EPSEstimateCurrentYear": "28.79",
                "EPSEstimateNextYear": "33.37",
                "EPSEstimateNextQuarter": "7.92",
                "DaysLow": "562.40",
                "DaysHigh": "570.43",
                "YearLow": "433.63",
                "YearHigh": "630.85",
                "HoldingsGainPercent": "- - -",
                "AnnualizedGain": "-",
                "HoldingsGain": null,
                "HoldingsGainPercentRealtime": "N/A - N/A",
                "HoldingsGainRealtime": null,
                "MoreInfo": "cnprmiIed",
                "OrderBookRealtime": "N/A",
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you have there is not JSON. It's JSON with padding. You have to remove "cbfunc(" and the ending ")" to be able to parse it as JSON. Then you can use:

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.query.results.quote );

to get an object back.

You can also declare a function called function cbfunc(o){} and then just do eval() around your string but this is probably not the safest way. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...