JAVA:遍历多个具有相同名称的 JSON 数组,直到满足内部 JSON 数组的子项条件 - IBM Watson

JAVA: Iterate through multiple JSON arrays with same name until inner JSON array's child's condition is met - IBM Watson

从下面的 JSON 响应中,我需要遍历每个 alternatives JSON 数组中可用的 timestamps直到满足条件(例如:时间为 20 秒和 00 毫秒)。我怎样才能做到这一点?在检查 SO 时,我发现不允许多次使用相同的名称(此处为替代项),但是 JSON 响应是有效的,当它从在线工具验证时(注意:此响应是从 ibm watson speech to文字 API).

 {
"results": [{
        "alternatives": [{
            "timestamps": [
                [
                    "this",
                    0.03,
                    0.22
                ],
                [
                    "move",
                    0.22,
                    0.53
                ],
                [
                    "planks",
                    0.53,
                    1.0
                ]
            ],
            "confidence": 0.637,
            "transcript": "this move planks "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "glue",
                    2.19,
                    2.51
                ],
                [
                    "the",
                    2.51,
                    2.65
                ],
                [
                    "sheet",
                    2.65,
                    2.96
                ],
                [
                    "to",
                    2.96,
                    3.06
                ],
                [
                    "the",
                    3.06,
                    3.15
                ],
                [
                    "doctor",
                    3.15,
                    3.59
                ],
                [
                    "background",
                    3.72,
                    4.42
                ]
            ],
            "confidence": 0.725,
            "transcript": "glue the sheet to the doctor background "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "it",
                    5.72,
                    5.86
                ],
                [
                    "is",
                    5.86,
                    5.97
                ],
                [
                    "easy",
                    5.97,
                    6.24
                ],
                [
                    "to",
                    6.24,
                    6.37
                ],
                [
                    "tell",
                    6.37,
                    6.62
                ],
                [
                    "the",
                    6.62,
                    6.73
                ],
                [
                    "death",
                    6.73,
                    7.0
                ],
                [
                    "of",
                    7.03,
                    7.2
                ],
                [
                    "a",
                    7.2,
                    7.29
                ],
                [
                    "well",
                    7.29,
                    7.66
                ]
            ],
            "confidence": 0.873,
            "transcript": "it is easy to tell the death of a well "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "these",
                    8.8,
                    9.06
                ],
                [
                    "days",
                    9.06,
                    9.29
                ],
                [
                    "a",
                    9.29,
                    9.35
                ],
                [
                    "chicken",
                    9.35,
                    9.71
                ],
                [
                    "leg",
                    9.71,
                    10.01
                ]
            ],
            "confidence": 0.628,
            "transcript": "these days a chicken leg "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "the",
                    10.45,
                    10.54
                ],
                [
                    "birch",
                    10.54,
                    11.03
                ],
                [
                    "canoe",
                    11.03,
                    11.48
                ],
                [
                    "slid",
                    11.48,
                    11.87
                ],
                [
                    "on",
                    11.87,
                    12.06
                ],
                [
                    "the",
                    12.06,
                    12.15
                ],
                [
                    "smooth",
                    12.15,
                    12.56
                ],
                [
                    "planes",
                    12.56,
                    12.88
                ]
            ],
            "confidence": 0.313,
            "transcript": "the birch canoe slid on the smooth planes "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "glue",
                    14.22,
                    14.54
                ],
                [
                    "the",
                    14.54,
                    14.69
                ],
                [
                    "sheet",
                    14.69,
                    14.99
                ],
                [
                    "to",
                    14.99,
                    15.1
                ],
                [
                    "the",
                    15.1,
                    15.18
                ],
                [
                    "doctor",
                    15.18,
                    15.61
                ],
                [
                    "the",
                    15.61,
                    15.71
                ],
                [
                    "background",
                    15.71,
                    16.45
                ]
            ],
            "confidence": 0.707,
            "transcript": "glue the sheet to the doctor the background "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "it",
                    17.75,
                    17.89
                ],
                [
                    "is",
                    17.89,
                    18.0
                ],
                [
                    "easy",
                    18.0,
                    18.27
                ],
                [
                    "to",
                    18.27,
                    18.4
                ],
                [
                    "tell",
                    18.4,
                    18.66
                ],
                [
                    "the",
                    18.66,
                    18.76
                ],
                [
                    "death",
                    18.76,
                    19.04
                ],
                [
                    "of",
                    19.07,
                    19.23
                ],
                [
                    "a",
                    19.23,
                    19.32
                ],
                [
                    "well",
                    19.32,
                    19.68
                ]
            ],
            "confidence": 0.872,
            "transcript": "it is easy to tell the death of a well "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "these",
                    20.83,
                    21.09
                ],
                [
                    "days",
                    21.09,
                    21.32
                ],
                [
                    "a",
                    21.32,
                    21.38
                ],
                [
                    "chicken",
                    21.38,
                    21.74
                ],
                [
                    "leg",
                    21.74,
                    21.96
                ],
                [
                    "is",
                    21.96,
                    22.08
                ],
                [
                    "a",
                    22.08,
                    22.16
                ],
                [
                    "very",
                    22.16,
                    22.46
                ],
                [
                    "dish",
                    22.49,
                    22.99
                ]
            ],
            "confidence": 0.652,
            "transcript": "these days a chicken leg is a very dish "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "rice",
                    24.17,
                    24.5
                ],
                [
                    "is",
                    24.5,
                    24.61
                ],
                [
                    "often",
                    24.61,
                    24.97
                ],
                [
                    "served",
                    24.97,
                    25.33
                ],
                [
                    "in",
                    25.33,
                    25.47
                ],
                [
                    "roundels",
                    25.47,
                    26.2
                ]
            ],
            "confidence": 0.705,
            "transcript": "rice is often served in roundels "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "the",
                    27.28,
                    27.42
                ],
                [
                    "Jews",
                    27.42,
                    27.71
                ],
                [
                    "of",
                    27.71,
                    27.84
                ],
                [
                    "lemons",
                    27.84,
                    28.25
                ],
                [
                    "makes",
                    28.25,
                    28.53
                ],
                [
                    "fine",
                    28.53,
                    28.85
                ]
            ],
            "confidence": 0.381,
            "transcript": "the Jews of lemons makes fine "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "much",
                    29.01,
                    29.44
                ]
            ],
            "confidence": 0.382,
            "transcript": "much "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "the",
                    30.37,
                    30.48
                ],
                [
                    "box",
                    30.48,
                    30.85
                ],
                [
                    "was",
                    30.85,
                    31.02
                ],
                [
                    "thrown",
                    31.02,
                    31.31
                ],
                [
                    "beside",
                    31.31,
                    31.67
                ],
                [
                    "the",
                    31.67,
                    31.75
                ],
                [
                    "park",
                    31.75,
                    32.01
                ],
                [
                    "trucks",
                    32.01,
                    32.43
                ]
            ],
            "confidence": 0.531,
            "transcript": "the box was thrown beside the park trucks "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "the",
                    33.69,
                    33.81
                ],
                [
                    "hawks",
                    33.81,
                    34.11
                ],
                [
                    "are",
                    34.11,
                    34.2
                ],
                [
                    "set",
                    34.2,
                    34.52
                ],
                [
                    "chopped",
                    34.52,
                    34.87
                ],
                [
                    "corn",
                    34.87,
                    35.17
                ],
                [
                    "and",
                    35.17,
                    35.28
                ],
                [
                    "garbage",
                    35.28,
                    35.86
                ]
            ],
            "confidence": 0.505,
            "transcript": "the hawks are set chopped corn and garbage "
        }],
        "final": true
    },
    {
        "alternatives": [{
            "timestamps": [
                [
                    "four",
                    36.9,
                    37.29
                ],
                [
                    "hours",
                    37.29,
                    37.57
                ],
                [
                    "of",
                    37.57,
                    37.66
                ],
                [
                    "steady",
                    37.66,
                    37.93
                ],
                [
                    "work",
                    37.93,
                    38.18
                ],
                [
                    "faced",
                    38.18,
                    38.58
                ]
            ],
            "confidence": 0.784,
            "transcript": "four hours of steady work faced "
        }],
        "final": true
    }
]

}

你可以试试下面的方法

String s = ""; //Your json string
 JSONObject json = new JSONObject(s);
        JSONArray jsonArray = json.getJSONArray("results");
        Double searchTime  = Double.valueOf(0.22);
        for(int alt=0 ;alt<jsonArray.length(); alt++){
            JSONArray alternatives = jsonArray.getJSONObject(alt).getJSONArray("alternatives");

            for(int tmp=0;tmp<alternatives.length(); tmp++){
                JSONArray timeStamps = alternatives.getJSONObject(tmp).getJSONArray("timestamps");
                for(int t=0;t<timeStamps.length();t++){
                    if(searchTime.equals(timeStamps.getJSONArray(t).getDouble(1))){
                        System.out.println(alternatives);
                        System.out.println(timeStamps.getJSONArray(t));
                    }
                }
            }
        }