从JSON转换的Facebook通知 [英] Converting Facebook notifications from json

查看:128
本文介绍了从JSON转换的Facebook通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我从Facebook使用Facebook的SDK为Android接收通知。但是, response.toString返回的数据()方法是JSON格式。

我需要知道我可以把它转换简单的人类可读的格式,这样我可以使用它们来查看一个文本框?

这是我走出图形explorer.Required字符串为标题

  {
  ID:10000115768434,
  名:瑙曼阿斯拉姆
  通知:{
    数据:
      {
        ID:notif_10000357346818423_6543370
        从:{
          名:穆罕默德阿萨德伊克巴尔
          ID:56342467433
        },
        至: {
          名:瑙曼阿斯拉姆
          ID:324545436818423
        },
        CREATED_TIME:2014-03-28T20:09:02 + 0000,
        updated_time:2014-03-28T20:16:05 + 0000,
        头衔:穆罕默德阿萨德伊克巴尔和斯迈特卡兹米也谈到穆罕默德阿萨德伊克巴尔的照片。
        链接:http://www.facebook.com/photo.php?fbid=4040070417111&se
        应用程序:{
          名:照片,
          ID:54635732
        },
        未读:1
      },


解决方案

您要么需要找到上线JSONparser库和使用它们,或者如果你愿意,你可以写自己的JSONparser类,并分析在Java对象中的数据(顺便说一句JSON响应通常是人类可读它只是它作为一个对象或数组或对象)。
如果你想编写自己的JSONparser类应该不会那么难,我们不能劝你从哪里开始,直到发布JSONresponse,我们看到了响应的格式。

件事情之一我想说的是首先检查你的JSON响应的格式。它知道格式是非常重要的,因为如果JSONresponse进来的对象数组对象的格式必须使用去code将其的JSONObject - > JSONArray - >> JSONObject的,否则你的程序会崩溃。 点击此处并复制粘贴文本框它会告诉你它的格式的JSON响应。

如果你想开始使用JSON解码的基础知识<一href=\"http://stackoverflow.com/questions/11804718/cannot-retrieve-data-from-mysql-table-which-matches-specific-uid/22597115#22597115\">Click这里和检查我的答案上的计算器非常基本的,但如果你才开始处理JSONresponses它可能是一个良好的开端。

修改

根据您的JSONresponse从大括号,这意味着响应一个JSONObject的,但我没有看到整个JSONresponse和一些括号丢失,所以我的答案可能没有什么意义,但是你可以按照这些步骤,如果你了解它启动

您首先要做的就是保存'字符串'对象响应,并创建一个JSONObject并通过将响应初始化。那么你想从中提取了通知的对象,所以你重新创建第二个的JSONObject这个对象将只从字符串中提取通知对象

那么你想要的JSONArray数据是从你的JSONObject提取,所以你定义一个JSONArray和getJSONArray(数据)。
与兵卫再次,你需要的JSONObject只提取称号的对象。

总结

 创建的JSONObject和响应字符串传递给它
    从它创建第二的JSONObject
    您创建JSONArray和使用getJSONArray(___)方法的JSONObject
    终于里面创造最后的JSONObject for循环,如果你有一个以上的通知
    并使用的getString(题)方法,对象

您的JSON响应是不完整的,所以我可能是错的,但你可以给它一个尝试。

In my app I'm receiving notifications from Facebook using facebook sdk for android. But the data returned by response.toString() method is in json format.

I need to know how can I convert it in simple human readable format so that I can use them to view in a textfield?

This is what I get out of graph explorer.Required string is "title"

{
  "id": "10000115768434",
  "name": "Nauman Aslam",
  "notifications": {
    "data": [
      {
        "id": "notif_10000357346818423_6543370",
        "from": {
          "name": "Muhammad Asad Iqbal",
          "id": "56342467433"
        },
        "to": {
          "name": "Nauman Aslam",
          "id": "324545436818423"
        },
        "created_time": "2014-03-28T20:09:02+0000",
        "updated_time": "2014-03-28T20:16:05+0000",
        "title": "Muhammad Asad Iqbal and Smat Kazmi also commented on Muhammad Asad Iqbal's photo.",
        "link": "http://www.facebook.com/photo.php?fbid=4040070417111&se",
        "application": {
          "name": "Photos",
          "id": "54635732"
        },
        "unread": 1
      },

解决方案

You will either need to find JSONparser libraries on-line and use them or if you want you can write your own JSONparser class and parse the data in java objects (By the way JSON response is usually human readable its just that it comes as an object or arrays or objects). if you want to write your own JSONparser class it shouldn't be that hard we can not advise you on where to start until you post the JSONresponse and we see the format of the response.

one things I would say is first check the format of your JSON response. It is very important to know the format because if JSONresponse comes in format of "object of arrays of objects" you must decode it using "JSONObject --> JSONArray ->> JSONObject" otherwise your program will crash. Click here and copy paste your json response on the textbox it will show you the format of it.

If you want to start with basics of JSON decoding Click here and check my answer on stackoverflow its very basic but if you have only started to deal with JSONresponses it might be a good start.

Edit

As per your JSONresponse it starts from curly brackets which means the response is one JSONObject but I do not see the whole JSONresponse and some brackets are missing so my answer might not make sense but you can follow the steps if you understand it

first thing you do is save the response in 'String' object and create a JSONObject and initialise it by passing the response. then you want the the "notifications" object extracted from it so you again create second JSONObject this object will only extract "notifications" object from the string

then you want the JSONArray "data" to be extracted so you define a JSONArray and getJSONArray("data") from you JSONObject. and thei again you will need JSONObject to only extract "title" object.

to summarise

    Create JSONObject and pass the response String to it
    Create second JSONObject from it
    Create JSONArray and use getJSONArray("___") method on you JSONObject
    and finally create last JSONObject inside a for loop if you have more than one notification 
    and use getString("title") method on that object

Your JSON response is not complete so I might be wrong but you can give it a try.

这篇关于从JSON转换的Facebook通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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