Вопрос

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.

Другие советы

When you get a response string in json format there are libraries that allows you to turn your string in a dictionary type.

If you want a web site that helps you view a json string in readable by hummans go here Online JSON Viewer. Paste your code in the first page then go to viewer tab, you'll see the name of the keys and the values.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top