对象的 JSON.stringify 只返回 {}
JSON.stringify of an object returns just {}
我正在尝试将javascript对象转换为JSON。对象格式正确,console.log(myObject)
正确返回它。
但是,console.log(JSON.stringify(myObject));
回报只是{}
我在这里错过了什么?编辑:有问题的对象:
Object
autor: "Administrador"
descripcion: "At Google I/O 2015, everything we’ve seen and learned about is under the command of Sundar Pichai. In this exclusive interview, he walks us through his product vision.↵↵Subscribe: http://goo.gl/G5RXGs↵↵Check out our full video catalog: http://goo.gl/lfcGfq↵Visit our playlists: http://goo.gl/94XbKx↵Like The Verge on Facebook: http://goo.gl/2P1aGc↵Follow on Twitter: http://goo.gl/XTWX61↵Follow on Instagram: http://goo.gl/7ZeLvX↵Read More: http://www.theverge.com"
titulo: "The future of Google with Sundar Pichai"
url_imagen: "https://i.ytimg.com/vi/TguamcqrQjI/sddefault.jpg"
url_video: "https://www.youtube.com/embed/TguamcqrQjI"
__proto__: Object
编辑:这是我创建对象的方式:
var myObject = {};
$http.get('apiCallToYoutubeIcantShareHereCauseItContainsAPrivateKey')
.success(function(data) {
myObject.titulo = data['items'][0]["snippet"]['title'];
myObject.descripcion = data['items'][0]["snippet"]['description'];
myObject.url_video ="https://www.youtube.com/embed/"+idYoutube;
myObject.url_imagen = data['items'][0]["snippet"]['thumbnails']['standard']["url"];
myObject.autor = 'Administrador';
});
问题是,您正在尝试在从服务器(It's an asynchronous request
)接收对象之前打印对象。尝试在success
消息的末尾打印它,或使用承诺/延期概念
var myObject = {};
$http.get('apiCallToYoutubeIcantShareHereCauseItContainsAPrivateKey')
.success(function(data) {
myObject.titulo = data['items'][0]["snippet"]['title'];
myObject.descripcion = data['items'][0]["snippet"]['description'];
myObject.url_video ="https://www.youtube.com/embed/"+idYoutube;
myObject.url_imagen = data['items'][0]["snippet"]['thumbnails']['standard']["url"];
myObject.autor = 'Administrador';
console.log(JSON.stringify(myObject));
});
这是因为您的对象中;
了 2 个不属于那里的分号。
您当前的代码:
var myObject = {
"autor": "Administrador",
"descripcion": "At Google I/O 2015, everything we’ve seen and learned about is under the command of Sundar Pichai.In this exclusive interview, he walks us through his product vision.Subscribe: goo.gl/G5RXGsFollow on Twitter: goo.gl/XTWX61↵Follow on Instagram: goo.gl/7ZeLvX ",
"titulo": "The future of Google with Sundar Pichai",
"url_imagen": "i.ytimg.com/vi/TguamcqrQjI/sddefault.jpg";, // <-- Semicolon that doesn't belong here
"url_video": "youtube.com/embed/TguamcqrQjI"; // <-- Semicolon that doesn't belong here
};
跟随没有它们的对象:
JSFiddle: http://jsfiddle.net/zu2L3h5x/1/
var myObject = {
"autor": "Administrador",
"descripcion": "At Google I/O 2015, everything we’ve seen and learned about is under the command of Sundar Pichai.In this exclusive interview, he walks us through his product vision.Subscribe: goo.gl/G5RXGsFollow on Twitter: goo.gl/XTWX61↵Follow on Instagram: goo.gl/7ZeLvX ",
"titulo": "The future of Google with Sundar Pichai",
"url_imagen": "i.ytimg.com/vi/TguamcqrQjI/sddefault.jpg",
"url_video": "youtube.com/embed/TguamcqrQjI"
};
你确定myObject
在你执行console.log(JSON.stringify(myObject));
时仍在范围内
如果在控制台中执行以下语句,您将看到它工作正常:
var web = { "domain":"mammothworkwear.com","keywords":["Helly Hansen", "Snickers"]}; console.log(JSON.stringify(web));
我猜你的myObject
是空的
编辑:
您上面发布的 JSON 未验证,请将其粘贴到 http://jsonlint.com/
如果您修复了格式问题并在控制台中运行所有内容,它将按预期工作:
var myObject = {
autor: "Administrador",
descripcion: "At Google I/O 2015, everything we’ve seen and learned about is under the command of Sundar Pichai. In this exclusive interview, he walks us through his product vision.↵↵Subscribe: http://goo.gl/G5RXGs↵↵Check out our full video catalog: http://goo.gl/lfcGfq↵Visit our playlists: http://goo.gl/94XbKx↵Like The Verge on Facebook: http://goo.gl/2P1aGc↵Follow on Twitter: http://goo.gl/XTWX61↵Follow on Instagram: http://goo.gl/7ZeLvX↵Read More: http://www.theverge.com",
titulo: "The future of Google with Sundar Pichai",
url_imagen: "https://i.ytimg.com/vi/TguamcqrQjI/sddefault.jpg",
url_video: "https://www.youtube.com/embed/TguamcqrQjI"
};
console.log(myObject);
console.log(JSON.stringify(myObject));
JSON.stringify 调用可能会产生一个错误,该错误以某种方式被捕获,这就是结果为空对象的原因。但是,如果没有真正的代码和/或使用它的上下文,很难说。
- 节点导出返回一个空对象
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 控制台返回var不是't定义,但它是
- 从函数返回角度承诺
- Javascript返回值只在循环中返回一次
- 如何避免JSON.stringify在特殊情况下返回undefined,从而为JSON.parse创建字符串失败
- 为什么我的 JSON 代码使用自己的输出(来自 JSON.stringify)返回“语法错误”
- JSON.stringify 在解析复杂对象时返回空对象
- 使用 JSON.stringify 时将原型函数添加回返回的 Google Maps API 对象
- JavaScript、stringify、replacer-返回未定义与不返回任何内容
- JSON.stringify返回不同的日期值
- javascript中的Stringify返回一个空值
- 为什么JSON.stringify()在传递替换函数时返回undefined ?
- JSON.stringify返回"[object object]"而不是对象的内容
- JSON.stringify(2) == "2"可能返回false
- stringify()在传递AJAX响应数据字段时意外返回undefined
- 为什么JSON.Stringify返回undefined
- 对象的 JSON.stringify 只返回 {}
- JSON stringify返回空字符串