0

我有一个 JSON 文件,在我正在阅读的文件中是这样的

{
  "created_at": "Sun Jan 27 06:00:11 +0000 2013",
  "id": 295410858505420800,
  "id_str": "295410858505420800",
  "text": "RT @Iouis_tomlinson: gay marriage has been accepted in new york? cmon @harry_styles we're moving.",
  "source": "\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e",
  "truncated": false,
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_id_str": null,
  "in_reply_to_screen_name": null,
  "user": {
    "id": 469129489,
    "id_str": "469129489",
    "name": "claudia",
    "screen_name": "wenqianmeow",
    "location": "wild and carefree",
    "url": null,
    "description": "Hey i'm the cool cat meow - KEY ",
    "protected": false,
    "followers_count": 62,
    "friends_count": 39,
    "listed_count": 0,
    "created_at": "Fri Jan 20 08:16:21 +0000 2012",
    "favourites_count": 18,
    "utc_offset": -32400,
    "time_zone": "Alaska",
    "geo_enabled": false,
    "verified": false,
    "statuses_count": 10153,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "profile_background_color": "C0DEED",
    "profile_background_image_url": "http:\/\/a0.twimg.com\/profile_background_images\/663495104\/3ym43s5y51p6si2drotg.jpeg",
    "profile_background_image_url_https": "https:\/\/si0.twimg.com\/profile_background_images\/663495104\/3ym43s5y51p6si2drotg.jpeg",
    "profile_background_tile": true,
    "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3131226365\/652a4d1995335bc686b813580b68ba52_normal.jpeg",
    "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3131226365\/652a4d1995335bc686b813580b68ba52_normal.jpeg",
    "profile_banner_url": "https:\/\/si0.twimg.com\/profile_banners\/469129489\/1358749624",
    "profile_link_color": "FF0389",
    "profile_sidebar_border_color": "000000",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "default_profile": false,
    "default_profile_image": false,
    "following": null,
    "follow_request_sent": null,
    "notifications": null
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "retweeted_status": {
    "created_at": "Sun Jun 26 20:21:20 +0000 2011",
    "id": 85080221124001792,
    "id_str": "85080221124001792",
    "text": "gay marriage has been accepted in new york? cmon @harry_styles we're moving.",
    "source": "web",
    "truncated": false,
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
      "id": 217118451,
      "id_str": "217118451",
      "name": "Louis Tomlinson",
      "screen_name": "Iouis_tomlinson",
      "location": "Doncaster",
      "url": null,
      "description": "This is NOT the real Louis Tomlinson yeah okay cool. This is just a for a laugh so don't hate appreciate, bitches. 1\/2 of a larry bromance.",
      "protected": false,
      "followers_count": 68985,
      "friends_count": 115,
      "listed_count": 66,
      "created_at": "Thu Nov 18 16:48:11 +0000 2010",
      "favourites_count": 5,
      "utc_offset": null,
      "time_zone": null,
      "geo_enabled": false,
      "verified": false,
      "statuses_count": 1105,
      "lang": "en",
      "contributors_enabled": false,
      "is_translator": false,
      "profile_background_color": "C0DEED",
      "profile_background_image_url": "http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_image_url_https": "https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_tile": false,
      "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2604923677\/image_normal.jpg",
      "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2604923677\/image_normal.jpg",
      "profile_link_color": "0084B4",
      "profile_sidebar_border_color": "C0DEED",
      "profile_sidebar_fill_color": "DDEEF6",
      "profile_text_color": "333333",
      "profile_use_background_image": true,
      "default_profile": true,
      "default_profile_image": false,
      "following": null,
      "follow_request_sent": null,
      "notifications": null
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 21584,
    "entities": {
      "hashtags": [

      ],
      "urls": [

      ],
      "user_mentions": [
        {
          "screen_name": "Harry_Styles",
          "name": "Harry Styles",
          "id": 181561712,
          "id_str": "181561712",
          "indices": [
            49,
            62
          ]
        }
      ]
    },
    "favorited": false,
    "retweeted": true,
    "lang": "en"
  },
  "retweet_count": 0,
  "entities": {
    "hashtags": [

    ],
    "urls": [

    ],
    "user_mentions": [
      {
        "screen_name": "Iouis_tomlinson",
        "name": "Louis Tomlinson",
        "id": 217118451,
        "id_str": "217118451",
        "indices": [
          3,
          19
        ]
      },
      {
        "screen_name": "Harry_Styles",
        "name": "Harry Styles",
        "id": 181561712,
        "id_str": "181561712",
        "indices": [
          70,
          83
        ]
      }
    ]
  },
  "favorited": false,
  "retweeted": false,
  "lang": "en"
}

我想解析它并将其转换为地图,以便我可以提取我需要的名称、值对。我正在使用以下代码:

    val data = scala.io.Source.fromFile("search100.json").getLines.toList.map(x => x.split("\n").toList)
    print(data)
    val simple = [Map[String,Any]](data.mkString(""" """))

但是这似乎不起作用,有没有办法在不使用外部库的情况下将 JSON 转换为 Map?

4

2 回答 2

0

我认为这条线是错误的:

val data = scala.io.Source.fromFile("search100.json").getLines.toList.map(x => x.split("\n").toList)

应该是:

val data = scala.io.Source.fromFile("search100.json").getLines

getLines 已经剥离\r|\n

于 2013-01-29T06:34:42.443 回答
0

听起来底层的 JSON 没有解析;即您提供的输入不是 JSON。很可能是一段文字,以字母“L”开头;例如,比如说,键入签名或其他东西。

于 2013-01-29T06:18:16.627 回答