我从javascript调用api,而api正在返回json.使用以下代码调用json:
document.ready(function(){
$.getJSON(url,function (data) {
console.log(data);
$("#test").text(data);
});
}
});
但它只显示[对象] [对象]到页面.
并且在console.log中,它返回跟随json的无效包括__proto__.
Object {author: "Hay House UK",title: "Top 15 Must-Read Books For 2014",text: "When a year ends,it’s like finishing a favorite b…elf-help slant conducive to healing planet Earth.",images: Array[2],date_created: "Wed,01 Jan 2014 09:17:31 PST"…}
author: "Hay House UK"
categories: Object
business_finance: 0.039999999999999994
disaster_accident: 0.011428571428571427
education: 0.13887284818770693
entertainment_culture: 0.09647619047619045
environment: 0.08025982988254671
health_medical_pharma: 0.09809523809523808
hospitality_recreation: 0.04541137315797972
humaninterest: 0.04666666666666667
labor: 0.048559733653411985
law_crime: 0.01257142857142857
other: 0
politics: 0.12309533849221464
religion_belief: 0.03994037416667575
socialissues: 0.07857142857142856
sports: 0.05831770720339089
technology_internet: 0.014511842376414444
war_conflict: 0.053333333333333316
weather: 0.013888095736801704
__proto__: Object
cid: 1235024564
date: "Wed,1 Jan 2014 08:00:00 GMT"
date_created: "Wed,01 Jan 2014 09:17:31 PST"
html: "logo_keep_aspect_215x215.jpg">
但我的有效json如下:
{
"author": "Hay House UK","title": "Top 15 Must-Read Books For 2014","text": "When a year ends,but you also want to wrap it up so you can start the next one.\nBefore we close this chapter on 2013,we’d like to share some of oy and You Can Heal Your Life (YCHYL),Hay House is committed to publishing products that have a positive self-help slant conducive to healing planet Earth.","images": [
{
"primary": "true","caption": "Hay House","url": "http://www.healyourlife.com/img/resize_cache/1067-hay_house_logo_keep_aspect_215x215.jpg"
},{
"caption": "top books for 2014 hay house","url": "http://www.healyourlife.com/img/resize_cache/3667-Top_15_Must_Read_Books_For_2014_keep_aspect_374x215.jpg"
}
],"date_created": "Wed,01 Jan 2014 09:17:31 PST","categories": {
"entertainment_culture": 0.09647619047619045,"hospitality_recreation": 0.04541137315797972,"other": 0,"business_finance": 0.039999999999999994,"technology_internet": 0.014511842376414444,"socialissues": 0.07857142857142856,"sports": 0.05831770720339089,"humaninterest": 0.04666666666666667,"religion_belief": 0.03994037416667575,"war_conflict": 0.053333333333333316,"education": 0.13887284818770693,"health_medical_pharma": 0.09809523809523808,"labor": 0.048559733653411985,"law_crime": 0.01257142857142857,"politics": 0.12309533849221464,"environment": 0.08025982988254671,"weather": 0.013888095736801704,"disaster_accident": 0.011428571428571427
},"html": "
我需要那个纯粹的json,并且只想从javascript中将json显示在页面上,不包括proto和所有其他属性.
最佳答案
看一下文档:http://api.jquery.com/jquery.getjson/
它说:
The success callback is passed the returned data,which is typically a
JavaScript object or array as defined by the JSON structure and parsed
using the $.parseJSON() method. It is also passed the text status of
the response.
这意味着生成的JSON字符串将自动解析为JavaScript对象.
如果希望结果是JSON格式,可以在结果上使用JSON.stringify():
function (data) {
jsonData = JSON.stringify(data);
console.log(jsonData);
$("#test").text(jsonData);
});
但是,现在您将解析并字符串化不必要的JSON值.如果您只对字符串表示感兴趣,可以执行以下操作:
$.ajax({
dataType: "text",url: url,success: function (data) {
console.log(data);
$("#test").text(data);
}
});
请记住,$.getJSON只是一个快捷方式:
$.ajax({
dataType: "json",data: data,success: success
});
因此,通过指定dataType:“json”,您告诉jQuery,您期望一个JSON格式的字符串,然后将自动解析.如果指定dataType:“text”,您应该只获取文本表示.