flask-restful
에서 JSON의 레코드 목록을 반환하는 간단한 get
을 만들었습니다. 반환 된 JSON이 목록 대신 유니 코드로 이해되는 이유는 무엇입니까?
resource_fields = {
'record_date': fields.String,
'rating': fields.Integer,
'notes': fields.String,
'last_updated': fields.DateTime,
}
class Records(Resource):
def get(self, email, password, last_sync_date):
user, em_login_provider = Rest_auth.authenticate(email, password)
resource_fields = {
'record_date': fields.String,
'rating': fields.Integer,
'notes': fields.String,
'last_updated': fields.DateTime,
}
m_records = []
if user:
try:
date = parser.parse(last_sync_date)
except:
#Never synced before - get all
recordsdb = Record.query(Record.user == user.key)
for record in recordsdb:
m_record = marshal(record, resource_fields);
m_records.append(m_record);
return json.dumps(m_records)
return {'data': 'none'}
이제 단위 테스트에서, JSON 파서에 수신 된 문자열을로드 한 후, 난 여전히 유니 코드를 얻을.
이 같이
:
[
{
"rating": 1,
"notes": null,
"last_updated": "Mon, 14 Oct 2013 20:56:09 -0000",
"record_date": "2013-10-14"
},
{
"rating": 2,
"notes": null,
"last_updated": "Mon, 14 Oct 2013 20:56:09 -0000",
"record_date": "2013-09-14"
}
]
단위 테스트 :
rv = self.app.get('/rest/records/{0}/{1}/{2}'.format(email, password, sync_date))
resp = json.loads(rv.data)
eq_(len(resp),2)
그러나 그것 때문에 200 분에 문자와 두 개체하지 목록이있는 유니 코드는 단위 테스트가 실패합니다.
내가 무엇을 놓치고 있는지 알려주세요.
print repr(resp)
출력이 :
str: u'[{"rating": 1, "notes": null, "last_updated": "Mon, 14 Oct 2013 21:33:07 -0000", "record_date": "2013-10-14"}, {"rating": 2, "notes": null, "last_updated": "Mon, 14 Oct 2013 21:33:07 -0000", "record_date": "2013-09-14"}]'
희망이 이미 당신을 위해 JSON으로 데이터를 인코딩한다
* 그러나 그것 때문에 200 분에 문자와 두 개체하지 목록이있는 유니 코드, 단위 테스트 *이 실패합니다. 'print repr (resp)'가 무엇인지를 보여줄 수 있습니까? 그게 정확한 소리하지 않습니다; 메서드는 목록을 나타내는 JSON 문자열을 반환해야합니다. –
물론 방금 추가했습니다. 감사합니다 – Houman
그건 내가 당신에게 제공하도록 요청한 것이 아니며 그것은 당신의 시험 실패입니다. 'print repr (resp)'는 무엇을 인쇄합니까? –