由“ jq”打印的值与json文件中存在的值不同

时间:2019-04-28 07:44:50

标签: json python-3.x precision jq

我有a.json个文件,内容如下:

{"created_at": "Fri Feb 01 03:19:49 +0000 2019", "id": 1091174254324928512, "id_str": "1091174254324928512", "text": "\u091c\u094b \u091c\u0940\u0902\u0926 \u0915\u093e \u092e\u0942\u0921 \u0939\u0948, \u0935\u0939\u0940 \u0939\u093f\u0928\u094d\u0926 \u0915\u093e \u092e\u0942\u0921 \u0939\u0948 \u270c\ufe0f\n\n#JindByPollResults #JindByelection\n#jindbypoll #JindResults\n#fridayfeelings", "truncated": false, "entities": {"hashtags": [{"text": "JindByPollResults", "indices": [43, 61]}, {"text": "JindByelection", "indices": [62, 77]}, {"text": "jindbypoll", "indices": [78, 89]}, {"text": "JindResults", "indices": [90, 102]}, {"text": "fridayfeelings", "indices": [103, 118]}], "symbols": [], "user_mentions": [], "urls": []}, "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "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": 439154852, "id_str": "439154852", "name": "Chowkidar Kailash Vijayvargiya", "screen_name": "KailashOnline", "location": "Delhi", "description": "National General Secretary of #BJP, Active Worker & a Proud Indian who continuously strives for development.", "url": "https://twitter.com/KbWcYpNlEZ", "entities": {"url": {"urls": [{"url": "https://twitter.com/KbWcYpNlEZ", "expanded_url": "http://www.KailashOnline.in", "display_url": "KailashOnline.in", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 756260, "friends_count": 1777, "listed_count": 243, "created_at": "Sat Dec 17 12:36:56 +0000 2011", "favourites_count": 7206, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": true, "statuses_count": 17644, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": true, "profile_image_url": "http://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/439154852/1544690191", "profile_link_color": "E06412", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "333333", "profile_use_background_image": true, "has_extended_profile": true, "default_profile": false, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}, "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 663, "favorite_count": 2705, "favorited": false, "retweeted": false, "lang": "hi"}

如您所见,id字段值为= 1091174254324928512

在python3中,带有原始a.json

>>> import json
>>> f=open("a.json","r")
>>> d=json.load(f)
>>> d["id"]
1091174254324928512

当我使用命令打印相同的文件时 cat a.json | jq .
我得到以下输出

{
  "created_at": "Fri Feb 01 03:19:49 +0000 2019",
  "id": 1091174254324928500,
  "id_str": "1091174254324928512",
  "text": "जो जींद का मूड है, वही हिन्द का मूड है ✌️\n\n#JindByPollResults #JindByelection\n#jindbypoll #JindResults\n#fridayfeelings",
  "truncated": false,
  "entities": {
    "hashtags": [
      {
        "text": "JindByPollResults",
        "indices": [
          43,
          61
        ]
      },
      {
        "text": "JindByelection",
        "indices": [
          62,
          77
        ]
      },
      {
        "text": "jindbypoll",
        "indices": [
          78,
          89
        ]
      },
      {
        "text": "JindResults",
        "indices": [
          90,
          102
        ]
      },
      {
        "text": "fridayfeelings",
        "indices": [
          103,
          118
        ]
      }
    ],
    "symbols": [],
    "user_mentions": [],
    "urls": []
  },
  "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
  "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": 439154852,
    "id_str": "439154852",
    "name": "Chowkidar Kailash Vijayvargiya",
    "screen_name": "KailashOnline",
    "location": "Delhi",
    "description": "National General Secretary of #BJP, Active Worker & a Proud Indian who continuously strives for development.",
    "url": "https://twitter.com/KbWcYpNlEZ",
    "entities": {
      "url": {
        "urls": [
          {
            "url": "https://twitter.com/KbWcYpNlEZ",
            "expanded_url": "http://www.KailashOnline.in",
            "display_url": "KailashOnline.in",
            "indices": [
              0,
              23
            ]
          }
        ]
      },
      "description": {
        "urls": []
      }
    },
    "protected": false,
    "followers_count": 756260,
    "friends_count": 1777,
    "listed_count": 243,
    "created_at": "Sat Dec 17 12:36:56 +0000 2011",
    "favourites_count": 7206,
    "utc_offset": null,
    "time_zone": null,
    "geo_enabled": true,
    "verified": true,
    "statuses_count": 17644,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "is_translation_enabled": false,
    "profile_background_color": "000000",
    "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
    "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
    "profile_background_tile": true,
    "profile_image_url": "http://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg",
    "profile_banner_url": "https://pbs.twimg.com/profile_banners/439154852/1544690191",
    "profile_link_color": "E06412",
    "profile_sidebar_border_color": "000000",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "has_extended_profile": true,
    "default_profile": false,
    "default_profile_image": false,
    "following": false,
    "follow_request_sent": false,
    "notifications": false,
    "translator_type": "none"
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "is_quote_status": false,
  "retweet_count": 663,
  "favorite_count": 2705,
  "favorited": false,
  "retweeted": false,
  "lang": "hi"
}

此处打印的id字段值= 1091174254324928500,与文件中存在的id字段值不同。

在python3中,以美化的pretty_a.json

>>> import json
>>> f=open("pretty_a.json","r")
>>> d=json.load(f)
>>> d["id"]
1091174254324928500

此外,当我使用VSCode打开a.json并使用prettify_JSON扩展名时,我得到的美化输出与使用cat a.json | jq .打印的输出相同。

所以我认为问题是因为json解码器如何处理数字数据类型。
似乎jqpython3的json解码器对数据类型的处理方式不同。

我的问题是,到底发生这种情况的原因是什么,以及如何在使用json的{​​{1}}模块进行编码的同时处理这些类型的特殊性,而不管给定的json文件是否经过python3修饰。

0 个答案:

没有答案