对象的 JSON.stringify 只返回 {}

JSON.stringify of an object returns just {}

本文关键字:返回 stringify JSON 对象      更新时间:2023-09-26

我正在尝试将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 调用可能会产生一个错误,该错误以某种方式被捕获,这就是结果为空对象的原因。但是,如果没有真正的代码和/或使用它的上下文,很难说。