具有 JSONResponse 如下:
def add_to_list(n):
n.append(len(n))
print(n)
a = [0, 1]
add_to_list(a)
print(a)
如何迭代品牌属性并与输入匹配(例如:英国)。
检查英国是否存在品牌,然后打印“语言”的价值?
我正在尝试通过JAVA使用上述API响应进行自动化,当品牌值匹配时,需要根据请求将语言值传递给其他API。
答案 0 :(得分:0)
您可以使用数组的indexOf方法获取要搜索的内容的索引。
for(var x in data){
if(data[x].brand.indexOf("UK") > -1){
console.log(data[x].Language);
}
}
答案 1 :(得分:0)
更好地创建一个结构。它使过滤对象变得容易
Option Explicit
Sub ReturnResults()
Dim r As Range
Application.ScreenUpdating = True
With Worksheets("Sheet1") ' reference results sheet
If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header" ' if A1 is empty, put a "dummy" header to make AutoFilter work properly
.AutoFilterMode = False
With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) ' reference referenced sheet column A range from row 1 down to column B last not empty cell
.SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
.AutoFilter Field:=1, Criteria1:="=TI"
On Error Resume Next
Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not r Is Nothing Then r.Copy Worksheets("Search Results").Range("B7")
.Parent.AutoFilterMode = False
.SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
End With
End With
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
您需要遍历 JSONArray 并在每个项目上使用 getJSONArray()方法检索品牌阵列,然后检查您的输入是否与任何字符串相同在该列表中使用 getString()和 compareTo(),然后打印该项目的“语言”。
String input = "UK"
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject jsonObject = jsonResponse.getJSONObject(i);
JSONArray brands = jsonObject.getJSONArray("brand");
for (int j = 0; j < brands.length(); j++) {
String brand = brands.getString(j);
if(brand.compareTo(input) == 0){
System.out.println(jsonObject.getString("Language"));
break;
}
}
}