无法在JSON对象上获取特定值 [英] Unable to get a specific value on a JSON object

查看:108
本文介绍了无法在JSON对象上获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Facebook Javascript SDK获取收件箱消息,到目前为止,我能够获取它们并将其转换为JSON对象

I'm using the Facebook Javascript SDK to get inbox messages and so far I was able to get them and the transform it to a JSON object

function getMsgs(){
FB.api(
/ me / inbox,
function(response){
if(response&&错误){
var result = JSON.stringify(response);
document.getElementById(msgs)。innerHTML =< pre>+ result +< / pre>;
}
}
);
}

我最终得到的是这样的:

function getMsgs() { FB.api( "/me/inbox", function (response) { if (response && !response.error) { var result = JSON.stringify(response); document.getElementById("msgs").innerHTML="<pre>"+result+"</pre>"; } } ); } And what I end up getting is something like this:

我想要访问'message'值。我想创建一个数组,存储这个JSON结构中的每一个消息值。

I want to access the 'message' value. I want to create an array that stores every single message value in this JSON structure.

推荐答案

如果只需要获取消息你可以使用

If you only need to get the messages, you can do with

  if (response && !response.error) {
      var m=null
          , result=JSON.stringify(response)
          , re=/"message":"([^"]+)"/g
          , messages=[];
      while( m=re.exec(result) ) {
          messages.push(m[1]);
      }
      // messages array should now hold all the values by key "message"
      //console.log(messages);
  }

这篇关于无法在JSON对象上获取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆