将foreach循环输出存储到数组

时间:2019-07-18 17:56:15

标签: arrays powershell

具有一个具有相同键值的JSON数组,想要遍历这些键值并获取与该数组值相同的一个键并将输出存储到数组中

model.wv

这是我尝试的代码,没有给出任何输出。

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    //==========================START==================================
    def Properties versionProps = new Properties()
    def versionPropsFile = file('version.properties')
    if(versionPropsFile.exists())
        versionProps.load(new FileInputStream(versionPropsFile))
    def code = (versionProps['VERSION_CODE'] ?: "0").toInteger() + 1
    versionProps['VERSION_CODE'] = code.toString()
    versionProps.store(versionPropsFile.newWriter(), null)
    //===========================END===================================
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "0.19"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //=======================================START===============================================
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    def appName = "MyAppSampleName"
                    outputFileName = appName+"_v${variant.versionName}.${versionProps['VERSION_CODE']}.apk"
                }
            }
            //=======================================END===============================================
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以这样做:

首先将json字符串转换为对象:

$contents = '{
    "contents":[
        { 
            "name":"windows-Instance",
            "Buildid":"1234",
            "Buildtime":"1563350400238"
        },
        {
            "name":"linux-Instance",
            "Buildid":"1454",
            "Buildtime":"1563264000198"
        },
        {
            "name":"linux-Instance",
            "Buildid":"1278685",
            "Buildtime":"1563177600092"
        }
    ]
}' | ConvertFrom-Json

比检索所需的属性:

$result = $contents | Select-Object -ExpandProperty "contents" | Select-Object -ExpandProperty "Buildtime" 

答案 1 :(得分:0)

您可以遍历JSON并创建一个自定义对象,该对象将允许您在需要时进一步“操纵”数据。 否则,@ mhu的示例是一个完美的在线用户

$json_content = (Get-Content ".\sample.json") | ConvertFrom-Json

$result = foreach ($content in $json_content."contents") {
    [PSCustomObject]@{
        "Buildtime" = $content."Buildtime"
    }
}