java - 如何使用 org.json.simple 包在 Java 中读取 JSON 文件

标签 java json

我要获取 Facebook read_books 该文件的格式如下:

{
   "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"
         }
      }
   ],

我需要书名及其 URL。我运行下面的代码,但在线程“main”java.lang.ClassCastException 中出现异常:org.json.simple.JSONObject 无法转换为 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);
}}

但这不是真的

最佳答案

问题出在以下行:

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

JSON 中的 data 字段不是字符串,而是嵌套的 JSON 对象。

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

这解释了您的 ClassCastException。

相反,你应该这样做:

JSONObject data = (JSONObject) indatafirst.get("data");
JSONObject book = (JSONObject) data.get("book");
String bookTitle = book.get("title");

关于java - 如何使用 org.json.simple 包在 Java 中读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21888492/

相关文章:

java - Android 应用程序在从 java.lang.NullPointerException 读取对象时启动时崩溃

java - Android:在 switch.setOnCheckedChangeListener 中调用 setOnTouchListener

json - 如何在没有 key 的情况下解析 JSON 文件?

javascript - 如何在不使用 Javascript 排序的情况下使 JSON 数组唯一

mysql - 如何使用 json 将元组数组传递给 sql 语句

java - 来自十六进制颜色值的光强度

java - 获取 "IllegalArguementException : Value for parameter ' id' 超出了 poi apache 库的范围?

java - 在 Java 中读取安全 session cookie

javascript - 蒙戈 GeoJSON : location object expected

java - 父类的GSON序列化