对象的JSON.stringify只返回{}

时间:2015-06-11 10:13:49

标签: javascript json

我试图将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';
    });

4 个答案:

答案 0 :(得分:0)

在执行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));

答案 1 :(得分:0)

这是因为你的对象中有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"
}; 

答案 2 :(得分:0)

我认为问题是,您正在尝试在从服务器(It's an asynchronous request)接收对象之前打印该对象。尝试在success消息的末尾打印它,或使用Promise / Deferred concepts

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));
    });

答案 3 :(得分:-1)

JSON.stringify调用可能会产生一个错误,这个错误会以某种方式被捕获,这就是结果为空对象的原因。但如果没有真实的代码和/或使用它的上下文,很难说。