我有一个URL,该URL在调用时输出JSON数据。我必须检查该JSON输出中的特定单词。下面的示例-
r1 = session.get(authurl, headers=headers, timeout=6)
resphead = r1.headers.get('content-type')
if 'application/json' in resphead:
json_data = r1.json()
overallstatus1 = str((json_data['status']))
overallstatus2 = str((json_data['status']['code']))
有时,必须使用['status']['code']
调用输出,有时 code 部分不会出现在输出中,即['status']
。同样,我将检查许多其他状态,以相同的方式进行区别。
即使键发生更改,在这里也可以读取输出。
请澄清。
答案 0 :(得分:0)
当您在响应对象上调用$(document).ready(function() {
var socket = io();
var senderName = $('#sender-name').val();
var receiverName = $('#receiverName').val();
socket.on('connect', function() {
var params = {
senderName: senderName,
receiverName: receiverName
};
socket.emit('joinRequest', params, function() {
console.log('joined from request page');
});
socket.on('refresh', function(data) {
$('#reload').load(location.href + ' #reload');
});
});
$('#add_friend').on('submit', function(e) {
e.preventDefault();
var receiverName = $('#receiverName').val();
console.log('typeof receiver:'+typeof(receiverName))
$.ajax({
url: '/request',
type: 'POST',
data: {
receiverName: receiverName
},
success: function() {
socket.emit(
'friendRequest',
{
receiverName: receiverName,
senderName: senderName
},
function() {
console.log('Request sent');
}
);
}
});
socket.emit('reload', {});
});
});
时,它将返回一个Python .json()
。因此,您可以利用字典的内置函数dict
来返回键(如果它存在于字典中),否则返回一些预先指定的值(默认值为get()
)
从文档中: https://docs.python.org/2/library/stdtypes.html#dict.get
您的代码中的调用将是
None