假设我们有一个包含数组的字符串:
mystring='{"name": "John", "ages": [20, 30, 40]}'
我们可以使用 jq 命令来提取 ages 数组:
ages=$(echo $mystring | jq '.ages')
然后,我们可以使用 foreach 命令来遍历数组中的每个元素:
echo $ages | jq -r '.[]' | while read age; do
  echo "Age: $age"
done
这将输出:
Age: 20
Age: 30
Age: 40
完整的示例代码:
mystring='{"name": "John", "ages": [20, 30, 40]}'
# Extract the "ages" array
ages=$(echo $mystring | jq '.ages')
# Loop over each element of the array and print it
echo $ages | jq -r '.[]' | while read age; do
  echo "Age: $age"
done
                                    

        
        
        
        
        
        
        
        
        
        
                
                
                
                
                
                
                