编写一个名为“ jsonFilter”的函数,该函数采用JSON格式的字符串作为对象数组格式的参数,其中每个对象都有键“质量”,“密度”,“温度”和“速度”,每个键映射到浮点数。此函数应以相同格式将输入作为JSON字符串返回,但仅包含速度大于38.11的对象。
Function FunWithDates(InputDate As Double) As Date
Dim dow As Integer
Dim dateOnly As Long
Dim timeOnly As Double
dateOnly = int (InputDate)
remainder = InputDate - dateOnly
dow = Weekday(InputDate)
If dow = 7 Then ' Saturday
FunWithDates = dateOnly - 0.5
ElseIf dow = 1 Then ' Sunday
FunWithDates = dateOnly - 1.5
ElseIf dow = 2 Then ' Monday
FunWithDates = dateOnly - 2.5
ElseIf remainder > 0.5 Then
FunWithDates = dateOnly - 0.5
ElseIf remainder < 0.5 Then
FunWithDates = dateOnly - 1 + (10 / 24)
End If
End Function
我得到列表索引必须是整数或切片,而不是str。我在做什么错了?
答案 0 :(得分:0)
虽然没有看到实际的输入,这有点困难,但是可能的问题是,正如您所说的,您正在接收JSON数组并尝试通过键访问它。即,如果JSON参数是json对象的json数组,则它将作为字典列表加载。列表只能通过整数访问。在这种情况下,为了能够访问任何给定对象的“速度”,您首先需要使用整数索引到该对象。
答案 1 :(得分:0)
IIUC,做:
Export-Csv
现在:
import json
def jsonFilter(JSON):
load = json.loads(JSON)
return json.dumps([i for i in load if i['velocity']>38.11])
是:
print(jsonFilter(your_json))
答案 2 :(得分:0)
import json
def jsonFilter(JSON):
load = json.loads(JSON)
array = []
for i in load:
if i['velocity'] > 38.11:
array.append(i)
return json.dumps(array)