gpt4 book ai didi

python - Flask 和有效的 json - ValueError : dictionary update sequence element #0 has length 3; 2 is required

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:01 25 4
gpt4 key购买 nike

这是我使用 Restful 的 Flask 代码。

def get(self):
data = [{"id": "2", "lags": [{"id": "GBPQ1"}, {"id": "GBPQ2"},{"id": "GBPQ3"}, {"id": "BPQ4"}], "name": "GBP"}]
return jsonify(data)

这是我的错误:为什么?每个 jsoinlint 的有效 json。怎么解决?

curl http://0.0.0.0:5000/test/model/exposure/currencies/

curl http://0.0.0.0:5000/test/model/exposure/currencies/111111
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ValueError: dictionary update sequence element #0 has length 3; 2 is required // Werkzeug Debugger</title>
<link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css" type="text/css">
<!-- We need to make sure this has a favicon so that the debugger does not by
accident trigger a request to /favicon.ico which might change the application
state. -->
<link rel="shortcut icon" href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
<script type="text/javascript" src="?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js"></script>
<script type="text/javascript" src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
<script type="text/javascript">
var TRACEBACK = 140493007176848,
CONSOLE_MODE = false,
EVALEX = true,
SECRET = "hIZ55a7qO5d1PpF7ILd6";
</script>
</head>
<body>
<div class="debugger">
<h1>ValueError</h1>
<div class="detail">
<p class="errormsg">ValueError: dictionary update sequence element #0 has length 3; 2 is required</p>
</div>
<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
<div class="traceback">

<ul><li><div class="frame" id="frame-140493007176784">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1836</em>,
in <code class="function">__call__</code></h4>
<pre>return self.wsgi_app(environ, start_response)</pre>
</div>

<li><div class="frame" id="frame-140493007177104">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1820</em>,
in <code class="function">wsgi_app</code></h4>
<pre>response = self.make_response(self.handle_exception(e))</pre>
</div>

<li><div class="frame" id="frame-140493007177168">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">271</em>,
in <code class="function">error_router</code></h4>
<pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-140493007176912">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">271</em>,
in <code class="function">error_router</code></h4>
<pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-140493007176976">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py"</cite>,
line <em class="line">188</em>,
in <code class="function">wrapped_function</code></h4>
<pre>return cors_after_request(app.make_response(f(*args, **kwargs)))</pre>
</div>

<li><div class="frame" id="frame-140493007177296">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1403</em>,
in <code class="function">handle_exception</code></h4>
<pre>reraise(exc_type, exc_value, tb)</pre>
</div>

<li><div class="frame" id="frame-140493007177424">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">268</em>,
in <code class="function">error_router</code></h4>
<pre>return self.handle_error(e)</pre>
</div>

<li><div class="frame" id="frame-140493007177360">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1817</em>,
in <code class="function">wsgi_app</code></h4>
<pre>response = self.full_dispatch_request()</pre>
</div>

<li><div class="frame" id="frame-140493007177552">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1477</em>,
in <code class="function">full_dispatch_request</code></h4>
<pre>rv = self.handle_user_exception(e)</pre>
</div>

<li><div class="frame" id="frame-140493007177616">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">271</em>,
in <code class="function">error_router</code></h4>
<pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-140493007177232">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">271</em>,
in <code class="function">error_router</code></h4>
<pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-140493007177488">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py"</cite>,
line <em class="line">188</em>,
in <code class="function">wrapped_function</code></h4>
<pre>return cors_after_request(app.make_response(f(*args, **kwargs)))</pre>
</div>

<li><div class="frame" id="frame-140493007177744">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1381</em>,
in <code class="function">handle_user_exception</code></h4>
<pre>reraise(exc_type, exc_value, tb)</pre>
</div>

<li><div class="frame" id="frame-140493007177872">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">268</em>,
in <code class="function">error_router</code></h4>
<pre>return self.handle_error(e)</pre>
</div>

<li><div class="frame" id="frame-140493007177808">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1475</em>,
in <code class="function">full_dispatch_request</code></h4>
<pre>rv = self.dispatch_request()</pre>
</div>

<li><div class="frame" id="frame-140493007178000">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/app.py"</cite>,
line <em class="line">1461</em>,
in <code class="function">dispatch_request</code></h4>
<pre>return self.view_functions[rule.endpoint](**req.view_args)</pre>
</div>

<li><div class="frame" id="frame-140493007178064">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">477</em>,
in <code class="function">wrapper</code></h4>
<pre>resp = resource(*args, **kwargs)</pre>
</div>

<li><div class="frame" id="frame-140493007177680">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/views.py"</cite>,
line <em class="line">84</em>,
in <code class="function">view</code></h4>
<pre>return self.dispatch_request(*args, **kwargs)</pre>
</div>

<li><div class="frame" id="frame-140493007177936">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py"</cite>,
line <em class="line">587</em>,
in <code class="function">dispatch_request</code></h4>
<pre>resp = meth(*args, **kwargs)</pre>
</div>

<li><div class="frame" id="frame-140493007178128">
<h4>File <cite class="filename">"/home/ubuntu/workspace/qtaapi/qtaapi/resources/model_exposure_currencies.py"</cite>,
line <em class="line">198</em>,
in <code class="function">get</code></h4>
<pre>return jsonify(data)</pre>
</div>

<li><div class="frame" id="frame-140493007178192">
<h4>File <cite class="filename">"/usr/local/lib/python2.7/dist-packages/flask/json.py"</cite>,
line <em class="line">237</em>,
in <code class="function">jsonify</code></h4>
<pre>return current_app.response_class(dumps(dict(*args, **kwargs),</pre>
</div>
</ul>
<blockquote>ValueError: dictionary update sequence element #0 has length 3; 2 is required</blockquote>
</div>

<div class="plain">
<form action="/?__debugger__=yes&amp;cmd=paste" method="post">
<p>
<input type="hidden" name="language" value="pytb">
This is the Copy/Paste friendly version of the traceback. <span
class="pastemessage">You can also paste this traceback into
a <a href="https://gist.github.com/">gist</a>:
<input type="submit" value="create paste"></span>
</p>
<textarea cols="50" rows="10" name="code" readonly>Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/ubuntu/workspace/qtaapi/qtaapi/resources/model_exposure_currencies.py", line 198, in get
return jsonify(data)
File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 237, in jsonify
return current_app.response_class(dumps(dict(*args, **kwargs),
ValueError: dictionary update sequence element #0 has length 3; 2 is required</textarea>
</form>
</div>
<div class="explanation">
The debugger caught an exception in your WSGI application. You can now
look at the traceback which led to the error. <span class="nojavascript">
If you enable JavaScript you can also use additional features such as code
execution (if the evalex feature is enabled), automatic pasting of the
exceptions and much more.</span>
</div>
<div class="footer">
Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
friendly Werkzeug powered traceback interpreter.
</div>
</div>
</body>
</html>

<!--

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/ubuntu/workspace/qtaapi/qtaapi/resources/model_exposure_currencies.py", line 198, in get
return jsonify(data)
File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 237, in jsonify
return current_app.response_class(dumps(dict(*args, **kwargs),
ValueError: dictionary update sequence element #0 has length 3; 2 is required

-->
ubuntu@ubuntu:~/.ssh$

最佳答案

尝试给 jsonify 提供一个字典:

def get(self):
data = [{"id": "2", "lags": [{"id": "GBPQ1"}, {"id": "GBPQ2"},{"id": "GBPQ3"}, {"id": "BPQ4"}], "name": "GBP"}]
return jsonify({'data': data})

或者如果您愿意,您也可以这样做:return jsonify(data=data)

关于python - Flask 和有效的 json - ValueError : dictionary update sequence element #0 has length 3; 2 is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com