【问题标题】:How to handle JSON string in jQuery?如何在 jQuery 中处理 JSON 字符串?
【发布时间】:2014-03-07 10:10:33
【问题描述】:

我有一个如下的 JSON 字符串

    [{"partner-code":"AMEX",
"partner":"American Express",
"issuer":"American Express",
"summary":[],
"description":[],
"url":"http://www.qantas.com.au/fflyer/dyn/partners/card-american-express",
"email":[],
"phone-number":[],
"mediafile":[],
"bonus-offer-summary":"Earn up to 50,000 bonus Points with the Qantas American Express Ultimate Card. Up to 7,500 bonus Points also available on the Qantas American Express Premium and Discovery Cards. Conditions and spend criteria apply.",
"bonus-offer-description":"You could earn up to 50,000 bonus points with the Qantas American Express Ultimate Card when you apply by 31 December 2013, are approved and meet the eligible spend criterias<sup>***</sup>. Also, you could earn up to 30,000 bonus points with the Qantas American Express Premium Card<sup>^^</sup> or up to 7,500 bonus points with the Qantas American Express Discovery Card<sup>**</sup> when you apply, are approved by 31 December 2013 and meet the eligible spend criteria.",
"show-bonus-offer":"",
"card-offerings":[{"card-name":"Qantas American Express Discovery Card",
                    "earning-points":"This credit card automatically earns Qantas Frequent Flyer points",
                    "website":"http://www.americanexpress.com/au/content/frequent-flyer-cards/qantas-discovery-card/?filter=qanseries&bkgd=3&PID=35&BUID=CCG&AFFID=Qantas&CRTV=Earn&PSKU=AQDB&sourcecode=25A996J001&CPID=100036881",
                    "contact-phone-number":"1300 736 978",
                    "usage-type":"Personal",
                    "card-level":"Classic",
                    "card-type-01":"American Express",
                    "annual-fee":"0","opt-in-fee":"0",
                    "supplementary-card-fee":"0",
                    "supplementary-card-fee-descriptor":"annual Card fee per Supplementary Card<sup>~</sup>",
                    "interest-rate":"20.74",
                    "interest-free-period":"Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup>",
                    "earned-area":"<ul>\r\n\t<li>1 point per $1 eligible spend<sup>*</sup></li>\r\n\t<li>1 extra point per $1 spent in Australia on selected Qantas products &amp; services<sup>+</sup></li>\r\n\t<li>Uncapped points earning<sup>^</sup></li>\r\n\t<li>This introductory offer is available to new<sup>**</sup> American Express Cardmembers only.</li>\r\n</ul>\r\n",
                    "other-features":"<ul>\r\n\t<li>Interest rate of 20.74% p.a.<sup>#</sup></li>\r\n\t<li>Annual card fee of $0</li>\r\n\t<li>$0 annual Card fee per Supplementary Card<sup>~</sup></li>\r\n\t<li>Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup></li>\r\n\t<li>5,000 points after $300 spend<sup>**</sup> on purchases in first 3 months of becoming a Cardmember</li>\r\n\t<li>2,500 points after first Card spend<sup>**</sup> in Australia on selected Qantas products &amp; services in Australia<sup>+</sup></li>\r\n\t<li>Introductory offer available to new<sup>**</sup> American Express Cardmembers only</li>\r\n</ul>\r\n"},
                    {"card-name":"Qantas American Express Premium Card",
                    "earning-points":"This credit card automatically earns Qantas Frequent Flyer points",
                    "website":"http://www.americanexpress.com/au/content/frequent-flyer-cards/qantas-premium-card/?filter=qanseries&bkgd=3&PID=35&BUID=CCG&AFFID=Qantas&CRTV=Earn&PSKU=MB9&sourcecode=25A9974001&CPID=100036882",
                    "contact-phone-number":"1800 040 398",
                    "usage-type":"Personal",
                    "card-level":"Gold",
                    "card-type-01":"American Express",
                    "annual-fee":"249","opt-in-fee":"0",
                    "supplementary-card-fee":"0",
                    "interest-rate":"20.74",
                    "cash-advance-rate":"0",
                    "interest-free-period":"Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup>","earned-area":"<ul class=\"noindent\">\r\n\t<li>You can earn 2 points per dollar spent<sup>*</sup>. Click 'View Website' above for further details.</li>\r\n\t<li>Uncapped points earning<sup>^</sup></li>\r\n</ul>\r\n","other-features":"<ul class=\"noindent\">\r\n\t<li>Interest rate of 20.74% p.a.<sup>#</sup></li>\r\n\t<li>Annual card fee of $249</li>\r\n\t<li>$0 annual Card fee per Supplementary Card<sup>~</sup></li>\r\n\t<li>Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup></li>\r\n\t<li>5,000 points after first Card spend<sup>^^</sup> on eligible purchases</li>\r\n\t<li>2,500 points after first Card spend<sup>^^</sup> on selected Qantas products &amp; services in Australia<sup>+</sup></li>\r\n\t<li>22,500 points after $500 spend within the first three months<sup>^^</sup></li>\r\n\t<li>This introductory offer available to new<sup>^^</sup> American Express Cardmembers only.</li>\r\n\t<li>Complimentary Domestic and Overseas Travel Insurance<sup>##</sup> when you pay for travel with your Card</li>\r\n\t<li>2 Qantas Club invitations per year<sup>~~</sup> after your first Card spend on selected Qantas products &amp; services<sup>+</sup> in Australia</li>\r\n</ul>\r\n"},{"card-name":"Qantas American Express Ultimate Card","earning-points":"This credit card automatically earns Qantas Frequent Flyer points","website":"http://www.americanexpress.com/au/content/frequent-flyer-cards/qantas-ultimate/?filter=qanseries",
                    "contact-phone-number":"1300 736 978",
                    "usage-type":"Personal",
                    "card-level":"Platinum",
                    "card-type-01":"American Express",
                    "interest-free-period":"Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup>",
                    "earned-area":"<ul class=\"noindent\">\r\n\t<li>You can earn:</li>\r\n\t<li>3 Qantas points<sup>*</sup> per dollar spent on eligible Card purchases at:\r\n\t<ul type=\"disc\">\r\n\t\t<li>Tens of thousands of restaurants in Australia<sup>$$</sup></li>\r\n\t\t<li>Selected Qantas products and services in Australia<sup>+</sup></li>\r\n\t</ul>\r\n\t</li>\r\n\t<li>2 points per dollar spent* on eligible Card purchases at:\r\n\t<ul type=\"disc\">\r\n\t\t<li>airlines, accommodation, major cruise and tour operators paid directly or through travel agencies<sup>$$</sup></li>\r\n\t\t<li>Spend in foreign currency when overseas and online<sup>$$</sup></li>\r\n\t</ul>\r\n\t</li>\r\n\t<li>1 point per dollar spent* on eligible Card purchases at:\r\n\t<ul type=\"disc\">\r\n\t\t<li>All other providers, excluding those below</li>\r\n\t\t<li>Insurances offered by American Express</li>\r\n\t</ul>\r\n\t</li>\r\n\t<li>0.5 point per dollar spent* on eligible Card purchases at:\r\n\t<ul type=\"disc\">\r\n\t\t<li>Utilities which are gas, water and electricity providers</li>\r\n\t\t<li>Insurance offered by companies other than American Express</li>\r\n\t\t<li>Telecommunication providers which includes goods and services purchased from phone, mobile and internet service providers</li>\r\n\t\t<li>Government bodies including the Australian Taxation Office, Australia Post, federal/state and local government bodies</li>\r\n\t</ul>\r\n\t</li>\r\n</ul>\r\n","other-features":"<ul class=\"noindent\">\r\n\t<li>Interest rate of 20.74% p.a.<sup>#</sup></li>\r\n\t<li>Annual card fee of $450</li>\r\n\t<li>$0 annual Card fee per Supplementary Card<sup>~</sup></li>\r\n\t<li>Up to 44 days interest free on purchases when the closing balance (including any balance transfer or promotional amount) is paid in full by the statement due date<sup>$</sup></li>\r\n\t<li>5,000 points after first Card spend<sup>***</sup> on eligible purchases</li>\r\n\t<li>2,500 points after first Card spend<sup>***</sup> on selected Qantas products &amp; services in Australia<sup>+</sup></li>\r\n\t<li>42,500 points after $500 spend within the first three months<sup>***</sup></li>\r\n\t<li>Introductory offer available to new<sup>***</sup> American Express Cardmembers only</li>\r\n\t<li>1 Complimentary domestic return flight<sup>+++</sup> between selected Australian Capital cities after your first Card spend every year in Australia on selected Qantas products and services<sup>+</sup></li>\r\n\t<li>Complimentary Domestic and Overseas Travel Insurance<sup>##</sup> when you pay for travel with your Card</li>\r\n</ul>\r\n"},
                    {"card-name":"Test Offering - Sarah",
                    "annual-fee":"0",
                    "opt-in-fee":"0",
                    "supplementary-card-fee":"0",
                    "interest-rate":"0",
                    "cash-advance-rate":"0"}
                ]
}]

我想在 jQuery 的客户端处理这个字符串,因为我是新手,我不知道任何人请建议最好的主意。这基本上是一个像这样的卡片信息'N'卡片信息将在服务器回复中得到我必须在页面中显示卡片列表如果我们点击一​​张特定卡片将显示卡片的所有详细信息并且必须拖动和放下并比较卡片。

所以我被困在列表中。 有谁能尽快帮忙。

提前致谢。

【问题讨论】:

    标签: java jquery json


    【解决方案1】:

    您可以使用 jQuery 的函数 parseJSON 将 JSON 字符串解析为 JavaScript 对象 函数文档和搜索示例可以指导您解决问题的方法。

    【讨论】:

    • 感谢 Oskars Pakers 我现在正在尝试这个,如果成功就会发布!
    【解决方案2】:

    在使用 JSON.parse(yourString) 或 jQuery $.parseJSON(yourString) 解析 JSON 字符串后,您可以将其用作对象。

    例如,如果你这样做:

    //yourString is the String provided in the question
    var jsonObject = JSON.parse(yourString); 
    alert(jsonObject.partner);
    

    它会提醒:

    美国运通

    【讨论】:

    • 谢谢 Dropout 我会测试并发布结果!
    • 好的,告诉我!干杯
    • var obj = jQuery.parseJSON('{ "name": "John", "age": "28"}' );警报(obj.age ===“28”); - 这显示为真,但如果我把方括号显示为假,如下所示 - var obj = jQuery.parseJSON('[{ "name": "John", "age": "28"}]' );警报(obj.age ===“28”);你能告诉我为什么它不起作用
    • 因为你有一个集合/数组..这需要你选择第一个元素..obj[0].age
    • 您好,有个小疑问,1. 我们不能像上面“合作伙伴代码”那样使用连字符 (-) 解析对象键,它似乎不起作用,2. 如果字符串值是空的,应该像 [] 或 ""
    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多