如何编写Python脚本来接收这些JSON数据并将其转换为数据选项卡

2024-06-08 00:50:30 发布

您现在位置:Python中文网/ 问答频道 /正文

`Python脚本,用于接收此JSON数据并将其转换为数据表

`下面是JSON数据

{"inappproduct": [{"packageName": "game",
  "sku": "game_product1",
  "status": "active",
  "purchaseType": "managedUser",
  "defaultPrice": {"priceMicros": "69990000", "currency": "CAD"},
  "prices": {"DZ": {"priceMicros": "6325000000", "currency": "DZD"},
   "AU": {"priceMicros": "79990000", "currency": "AUD"},
   "CA": {"priceMicros": "69990000", "currency": "CAD"},
   "IT": {"priceMicros": "54990000", "currency": "EUR"},
   "JP": {"priceMicros": "6000000000", "currency": "JPY"},
   "RU": {"priceMicros": "3790000000", "currency": "RUB"},
   "SG": {"priceMicros": "68980000", "currency": "SGD"},
   "KR": {"priceMicros": "65000000000", "currency": "KRW"},
   "GB": {"priceMicros": "48990000", "currency": "GBP"},
   "US": {"priceMicros": "49990000", "currency": "USD"},
  "listings": {"en-US": {"title": "currency_1",
    "description": "In-game currency"}},
  "defaultLanguage": "en-US"}},
 {"packageName": "game",
  "sku": "game_bundle1",
  "status": "active",
  "purchaseType": "managedUser",
  "defaultPrice": {"priceMicros": "139990000", "currency": "CAD"},
  "prices": {"DZ": {"priceMicros": "12750000000", "currency": "DZD"},
   "AU": {"priceMicros": "159990000", "currency": "AUD"},
   "CA": {"priceMicros": "139990000", "currency": "CAD"},
   "IT": {"priceMicros": "109990000", "currency": "EUR"},
   "JP": {"priceMicros": "11800000000", "currency": "JPY"},
   "RU": {"priceMicros": "7490000000", "currency": "RUB"},
   "SG": {"priceMicros": "148980000", "currency": "SGD"},
   "KR": {"priceMicros": "130000000000", "currency": "KRW"},
   "GB": {"priceMicros": "99990000", "currency": "GBP"},
   "US": {"priceMicros": "99990000", "currency": "USD"},
  "listings": {"en-US": {"title": "bundle",
    "description": "In-game bundle"}},
  "defaultLanguage": "en-US"}}]}

The following are some considerations: ●可以从JSON键“packageName”中找到packageName列 ● The column sku can be found from the JSON key “sku” ●列countryCode可以从包含两个- letter country codes nested within the JSON key “prices” ●列currency可以从JSON键“currency”中找到 `●列价格可从JSON键“priceMicros”中找到。价格 等于“priceMicros”除以1000000。你知道吗

`the result should look like below

packageName         sku          countryCode    currency   price
game            game_product1     CA            CAD        69.99
game            game_product1     US            USD        48.99
. . . . .
. . . . .
. . . . .
game            game_bundle1      GB            GBP        99.99

Tags: gamejsoncurrencycaenpackagenamepricesus
1条回答
网友
1楼 · 发布于 2024-06-08 00:50:30

我会使用pandas和列表理解,所以安装pandas包 如果你还没有安装。你知道吗

我会给你的“JSON数据”起个名字。你知道吗

my_json = {

"inappproduct": [{"packageName": "game",
  "sku": "game_product1",
  "status": "active",
  "purchaseType": "managedUser",
  "defaultPrice": {"priceMicros": "69990000", "currency": "CAD"},
  "prices": {"DZ": {"priceMicros": "6325000000", "currency": "DZD"},
   "AU": {"priceMicros": "79990000", "currency": "AUD"},
   "CA": {"priceMicros": "69990000", "currency": "CAD"},
   "IT": {"priceMicros": "54990000", "currency": "EUR"},
   "JP": {"priceMicros": "6000000000", "currency": "JPY"},
   "RU": {"priceMicros": "3790000000", "currency": "RUB"},
   "SG": {"priceMicros": "68980000", "currency": "SGD"},
   "KR": {"priceMicros": "65000000000", "currency": "KRW"},
   "GB": {"priceMicros": "48990000", "currency": "GBP"},
   "US": {"priceMicros": "49990000", "currency": "USD"},
  "listings": {"en-US": {"title": "currency_1",
    "description": "In-game currency"}},
  "defaultLanguage": "en-US"}},
 {"packageName": "game",
  "sku": "game_bundle1",
  "status": "active",
  "purchaseType": "managedUser",
  "defaultPrice": {"priceMicros": "139990000", "currency": "CAD"},
  "prices": {"DZ": {"priceMicros": "12750000000", "currency": "DZD"},
   "AU": {"priceMicros": "159990000", "currency": "AUD"},
   "CA": {"priceMicros": "139990000", "currency": "CAD"},
   "IT": {"priceMicros": "109990000", "currency": "EUR"},
   "JP": {"priceMicros": "11800000000", "currency": "JPY"},
   "RU": {"priceMicros": "7490000000", "currency": "RUB"},
   "SG": {"priceMicros": "148980000", "currency": "SGD"},
   "KR": {"priceMicros": "130000000000", "currency": "KRW"},
   "GB": {"priceMicros": "99990000", "currency": "GBP"},
   "US": {"priceMicros": "99990000", "currency": "USD"},
  "listings": {"en-US": {"title": "bundle",
    "description": "In-game bundle"}},
  "defaultLanguage": "en-US"}}]}

然后:

>>> import pandas as pd
>>> records = [
...     {
...         "packageName": x["packageName"],
...         "sku": x["sku"],
...         "countryCode": k,
...         "currency": v["currency"],
...         "price": str(float(v["priceMicros"])/1e6)
...     }
...     for x in my_json["inappproduct"]
...     for k, v in x["prices"].items()
...     if isinstance(v, dict) and v.get("currency") is not None
... ]
>>> app_df = pd.DataFrame(records)
>>> app_df = app_df.reindex(columns=["packageName", "sku", "countryCode", "currency", "price"])
>>> app_df
   packageName            sku countryCode currency     price
0         game  game_product1          CA      CAD     69.99
1         game  game_product1          JP      JPY    6000.0
2         game  game_product1          IT      EUR     54.99
3         game  game_product1          DZ      DZD    6325.0
4         game  game_product1          GB      GBP     48.99
5         game  game_product1          RU      RUB    3790.0
6         game  game_product1          US      USD     49.99
7         game  game_product1          KR      KRW   65000.0
8         game  game_product1          AU      AUD     79.99
9         game  game_product1          SG      SGD     68.98
10        game   game_bundle1          CA      CAD    139.99
11        game   game_bundle1          JP      JPY   11800.0
12        game   game_bundle1          IT      EUR    109.99
13        game   game_bundle1          DZ      DZD   12750.0
14        game   game_bundle1          GB      GBP     99.99
15        game   game_bundle1          RU      RUB    7490.0
16        game   game_bundle1          US      USD     99.99
17        game   game_bundle1          KR      KRW  130000.0
18        game   game_bundle1          AU      AUD    159.99
19        game   game_bundle1          SG      SGD    148.98

相关问题 更多 >

    热门问题