在python中对以整数开头的json数组进行排序

时间:2018-07-28 20:02:10

标签: php python mysql database

我有一个看起来像这样的json文件:

 {
    "1": {
       "name": "item1"
       "symbol": "symbol1"
       "quotes": {
          "Amount": "45345"
          "other": "value"
         }
      }
   "734": {
     "name": "item1"
     "symbol": "symbol1"
     "quotes": {
        "Amount": "45345"
        "other": "value"
       }
    }
   "378": {
     "name": "item1"
     "symbol": "symbol1"
     "quotes": {
        "Amount": "45345"
        "other": "value"
        }
      }
   "2": {
     "name": "item1"
     "symbol": "symbol1"
     "quotes": {
        "Amount": "45345"
        "other": "value"
       }
      }
 } 

放置数字的“类型”是字符串值。如果有帮助。

通常情况下,有人不会尝试做额外的工作,而用整数值来标记“键” ...

我正在尝试将此文件和其他json放入mysql数据库中。

我的研究告诉我,这个过程看起来有点像这样:

    # !!!!!!!!!!This is a copy paste from another solution::::::!!!!!!!

    json_obj = json.loads(response.decode('utf-8'))

    #print(json_obj["products"][0]["upc"])

    for product in json_obj["products"]:
        print("upc:", product["upc"])
        print("sku:", product["sku"])
        print("salePrice:", product["salePrice"])
        print('---')
       cursor.execute("INSERT INTO bestb (sku, upc, salePrice) VALUES (%s,%s,%s)", (product["sku"], product["upc"], product["salePrice"]))  

以上说明我们将在将密钥写入cols的同时加载并读取json文件。 -很有道理...我如何用整数循环执行此过程...

我从while循环开始

i = 1
while 1 < 400:
    print(data - to mysql) 

我正在使用python提取表单API,然后将数据“规范化”以插入到mysql中,那里的php会将数据提取到一些图表中。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试一下

import json
json = {
    "1": {
       "name": "item1",
       "symbol": "symbol1",
       "quotes": {
          "Amount": "45345",
          "other": "value",
         }
      },
   "734": {
     "name": "item1",
     "symbol": "symbol1",
     "quotes": {
        "Amount": "45345",
        "other": "value",
       }
    },
   "378": {
     "name": "item1",
     "symbol": "symbol1",
     "quotes": {
        "Amount": "45345",
        "other": "value",
        }
      },
   "2": {
     "name": "item1",
     "symbol": "symbol1",
     "quotes": {
        "Amount": "45345",
        "other": "value",
       }
      }
 };


jsonSorted = sorted(json.items()) 

print(jsonSorted)