代码之家  ›  专栏  ›  技术社区  ›  Vasiliy Stepulo

从带有分隔符的字符串中的JSON文件获取值

  •  1
  • Vasiliy Stepulo  · 技术社区  · 6 年前

    我有一个json文件,格式如下:

    {
    "total_count":  57,
    "incomplete_results":  false,
    "items":  [
                  {
                      "id":  123456,
                      "node_id":  "dfghdfjfgjftgjhtfyjh",
                      "name":  "Firstrepo",
                      "full_name":  "MyOrganization/Firstrepo",
    [SKIP]
                  },
                  {
                      "id":  4756758,
                      "node_id":  "dtghtfjgjyuj",
                      "name":  "Secondrepo",
                      "full_name":  "MyOrganization/Secondrepo",
    [SKIP]
                  },
                  {
                      "id":  568578567,
                      "node_id":  "dsgdfghftghtfyjtyj",
                      "name":  "Anotherrepo",
                      "full_name":  "MyOrganization/Anotherrepo",
    [SKIP]
                  },
                  {
                      "id":  58567856,
                      "node_id":  "sjdhfbgsdjfgjsdfjjs",
                      "name":  "Somerepo",
                      "full_name":  "MyOrganization/Somerepo",
    [SKIP]
                  },
    

    如何获取存储库的值并将其以csv格式写入变量或文件中。像:

    Firstrepo,Secondrepo,Anotherrepo,Somerepo
    

    获取值的脚本: 配置从Github获取回购列表

    $GithubAPIURL = "https://api.github.com"
    $Suffix = "/search/repositories"
    $Additions = "?q=org:MYORG:SEARCH&page=1&per_page=100" 
    $GithubAPIToken = "&access_token=MYTOKEN" 
    

    过程

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $variable = ((Invoke-Webrequest $GithubAPIURL$Suffix$Additions$GithubAPIToken).content | ConvertFrom-Json)
    $jsonfile = "c:\a\test.json"
    $variable | ConvertTo-Json | Out-File $jsonfile -Encoding UTF8
    

    获取名称列表

    $Reponames = (Get-Content $jsonfile -Encoding UTF8 | ConvertFrom-Json)   
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Paxz Quiron    6 年前

    (Get-Content $jsonfile -Encoding UTF8 | ConvertFrom-Json).items.name
    

    (Get-Content $jsonfile -Encoding UTF8 | ConvertFrom-Json).items | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Out-File $csvFilePath