TypeError:字符串索引必须是django中的整数

时间:2019-04-18 04:58:14

标签: python json django python-3.x

我正在尝试将json存储到bigchainDB中。但是问题是,当我在对json对象进行硬编码并在其他节点之间进行广播时,它会获得成功。但是,当我从邮递员发送相同的json对象时,出现错误string indices must be integers

这是我的功能

 def index(request):
    root = settings.BIGCHAINDB
    bdb = BigchainDB(root)
    alice, bob = generate_keypair(), generate_keypair()
    insertDB =  json.dumps(request.body.decode("utf-8"))
    jsonDict =  json.loads(insertDB)
    prepared_token_tx = bdb.transactions.prepare(
        operation='CREATE',
        signers=alice.public_key,
        recipients=[([bob.public_key], 10)],
        asset=jsonDict)
    fulfilled_token_tx = bdb.transactions.fulfill(
        prepared_token_tx,
        private_keys=alice.private_key)
    bdb.transactions.send_commit(fulfilled_token_tx)
    txid = fulfilled_token_tx['id']
    return HttpResponse(txid)

JSON对象:

  

{“数据”:{     “ cphNumber”:“ 321”,     “ farmName”:“ 313”,     “ addressLine1”:“ 13”,     “ addressLine2”:“ 13”,     “区域”:“ 13”,     “ postalCode”:“ 13”,     “ corrName”:“ 13”,     “ corrAddressLine1”:“ 131”,     “ corrAddressLine2”:“ 31”,     “ corrRegion”:“ 31”,     “ corrCountry”:“ 321”,     “ corrPostal”:“ 31”,     “ corrMobile”:“ 321”,     “ corrEmail”:“ 31”,     “ agentName”:“ 31”,     “ agentAddressLine1”:“ 313”,     “ agentAddressLine2”:“ 132”,     “ agentRegion”:“ 13”,     “ agentCountry”:“ 132”,     “ agentPostal”:“ 132”,     “ brn”:“ 13”,     “ animalSpecies”:“ 132”   }

Django Version: 2.1.7
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'api']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/home/user/Desktop/blockchain/api/views.py" in index
  48. 		asset=jsonDict)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/driver.py" in prepare
  254.             inputs=inputs,

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in prepare_transaction
  132.         inputs=inputs,

File "/home/user/miniconda3/lib/python3.7/functools.py" in wrapper
  824.         return dispatch(args[0].__class__)(*args, **kw)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in _prepare_create_transaction_dispatcher
  45.     return prepare_create_transaction(**kwargs)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in prepare_create_transaction
  196.         asset=asset['data'] if asset else None,

Exception Type: TypeError at /api/
Exception Value: string indices must be integers

任何建议都是最欢迎的。预先感谢。

1 个答案:

答案 0 :(得分:1)

这里的问题是您正在=IFERROR(INDEX($A1:$E1,,MATCH(TRUE,INDEX($A1:$E1>0,0),0)+COLUMN()-1),"") 上使用json.dumps(),它已经是一个字符串。因此,在此上使用request.body时,您的数据不会解析为json.loads()

dict

docs