0

I am going to get facebook read_books The file is in this format:

{
   "data": [
      {
         "id": "270170479804513",
         "from": {
            "name": "L I",
            "id": "1000022"
         },
         "start_time": "2014-01-22T09:31:00+0000",
         "publish_time": "2014-01-22T09:31:00+0000",
         "application": {
            "name": "Books",
            "id": "174275722710475"
         },
         "data": {
            "book": {
               "id": "133556360140246",
               "url": "https://www.facebook.com/pages/Pride-and-Prejudice/133556360140246",
               "type": "books.book",
               "title": "Pride and Prejudice"
            }
         },
         "type": "books.reads",
         "no_feed_story": false,
         "likes": {
            "count": 0,
            "can_like": true,
            "user_likes": false
         },
         "comments": {
            "count": 0,
            "can_comment": true,
            "comment_order": "chronological"
         }
      },
      {
         "id": "270170328",
         "from": {
            "name": "h",
            "id": "100004346894022"
         },
         "start_time": "2014-01-22T09:29:42+0000",
         "publish_time": "2014-01-22T09:29:42+0000",
         "application": {
            "name": "Books",
            "id": "174275722710475"
         },
         "data": {
            "book": {
               "id": "104081659627680",
               "url": "https://www.facebook.com/pages/Gulistan-of-Sadi/104081659627680",
               "type": "books.book",
               "title": "Gulistan of Sa'di"
            }
         },
         "type": "books.reads",
         "no_feed_story": false,
         "likes": {
            "count": 0,
            "can_like": true,
            "user_likes": false
         },
         "comments": {
            "count": 0,
            "can_comment": true,
            "comment_order": "chronological"
         }
      }
   ],

I need books titles and their URL. I run the below code but I get Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.lang.String

 while ((inputLine = in.readLine()) != null)
    {
     s = s + inputLine + "\r\n";
     if (s == null) {
    break;
     }
    t = t + inputLine + "\r\n";
    }
    in.close();
    t = t.substring(0, t.length() - 2);
   System.out.println(t);
   Object dataObj =JSONValue.parse(t);

 System.out.println(dataObj);
JSONObject dataJson = (JSONObject) dataObj;
JSONArray data = (JSONArray) dataJson.get("data");
for (Object o: data)
{
    JSONObject indata= (JSONObject) o;
    Object indatafirst=(JSONObjec`enter code here`t).get("0");
    String inndata=(String) indatafirst.get("data");
    System.out.println("inndata"+inndata);
}}

but it is not true

1
  • There isn't a read_books field in the JSON...what do you mean? Commented Feb 19, 2014 at 18:01

1 Answer 1

1

The problem is with the following line:

String inndata=(String) indatafirst.get("data");

The data field in the JSON is not a String, it's a nested JSON object.

"data": {
            "book": {
               "id": "104081659627680",
               "url": "https://www.facebook.com/pages/Gulistan-of-Sadi/104081659627680",
               "type": "books.book",
               "title": "Gulistan of Sa'di"
            }
 }

This explains your ClassCastException.

Instead you should do something like:

JSONObject data = (JSONObject) indatafirst.get("data");
JSONObject book = (JSONObject) data.get("book");
String bookTitle = book.get("title");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.