Unable to get a specific value on a JSON object

寵の児 提交于 2019-12-11 18:34:39

问题


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 && !response.error) { var result = JSON.stringify(response); document.getElementById("msgs").innerHTML="<pre>"+result+"</pre>"; } } ); } And what I end up getting is something like this:


{  
   "data":[  
      {  
         "id":"15986833470221166",
         "to":{  
            "data":[  
               {  
                  "id":"7888047207869023",
                  "name":"My Name"
               },
               {  
                  "id":"101530783476778608",
                  "name":"Friend Name"
               }
            ]
         },
         "updated_time":"2015-02-08T03:09:02+0000",
         "unread":5,
         "unseen":0,
         "comments":{  
            "data":[  
               {  
                  "id":"159268645347021166_1421118060",
                  "from":{  
                     "id":"7888047207869023",
                     "name":"My Name"
                  },
                  "message":"blah blah blah",
                  "created_time":"2015-01-13T03:01:00+0000"
               },
               {  
                  "id":"159268645347021166_1421118060",
                  "from":{  
                     "id":"7888047207869023",
                     "name":"My Name"
                  },
                  "message":"blah blah blah",
                  "created_time":"2015-01-13T03:01:16+0000"
               },
               {  
                  "id":"159268645347021166_1421118060",
                  "from":{  
                     "id":"101530783476778608",
                     "name":"Friend Name"
                  },
                  "message":"blah blah blah",
                  "created_time":"2015-01-13T03:01:23+0000"
               },
               {  
                  "id":"159268645347021166_1421118060",
                  "from":{  
                     "id":"101530783476778608",
                     "name":"Friend Name"
                  },
                  "message":"blah blah blah",
                  "created_time":"2015-01-13T03:01:30+0000"
               },
               {  
                  "id":"159268645347021166_1421118060",
                  "from":{  
                     "id":"101530783476778608",
                     "name":"Friend Name"
                  },
                  "message":"blah blah blah",
                  "created_time":"2015-02-08T03:09:02+0000"
               }
            ],
            "paging":{  
               "previous":"https://graph.facebook.com/v2.2/15986453474543021166/comments?access_token=TOKEN&limit=25&since=1423364314942&__paging_token=TOKEN&__previous=1",
               "next":"https://graph.facebook.com/v2.2/15986453474543021166/comments?access_token=TOKEN&limit=25&until=142111148324060&__paging_token=TOEKN"
            }
         }
      }

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


回答1:


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



回答2:


Based on the json you provided, you can do something like this:

// loop through the comments array
for ( var i = 0 ; i < json.data[0].comments.data.length; i++ )
{
    var d = json.data[0].comments.data[i];
    // codes here. Message is d.message

}

SAMPLE



来源:https://stackoverflow.com/questions/28445991/unable-to-get-a-specific-value-on-a-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!