代码之家  ›  专栏  ›  技术社区  ›  Manjunath Rao

在JSON中提供完整的文件路径

  •  0
  • Manjunath Rao  · 技术社区  · 5 年前

    我想使用JSON文件来存储powershell脚本的参数。很少有JSON属性将完整的文件路径作为值。

    这个 https://jsonlint.com/ 显示JSON是有效的。然而, ConvertFrom-Json cmdlet引发并出错。

    enter image description here

    请帮助我修复此JSON解析。

    JSON格式:

        {
      "region": "US",
      "proxy_address_exception_list": "1.1.1.1; 2.2.2.2",
      "dsvm_ip_address": "10.1.1.1",
      "svm_ip_address": "10.2.2.2",
      "url_without_xto": "abc.com",
      "url_with_xto": "def-xto.com",
      "web_server_farm_name": "dss",
      "full_path_to_requestrouter_amd64_msi": "c:\\script_downloads\\requestrouter.msi",
      "full_path_to_rewrite_amd64_en_US_msi": "c:\\script_downloads\\rewrite.msi",
      "full_path_to_webfarm_v1_1_amd64_en_US_msi" : "c:\\script_downloads\\webfarm.msi",
      "full_path_to_application_host_config": "c:\\script_downloads\\applicationHost.config"
    }
    

    PowerShell代码:

    # Get parameter_list from file 
    $parameter_list_json = Get-Content -Path "C:\Users\manjug\Desktop\Visualization_Automation\parameters.json" -Raw
    $parameter_list = ConvertFrom-Json $parameter_list_json
    

    错误:

    PS C:\Users\manjug\Desktop> # Get parameter_list from file 
    $parameter_list_json = Get-Content -Path "C:\Users\manjug\Desktop\Visualization_Automation\parameters.json" -Raw
    $parameter_list = ConvertFrom-Json $parameter_list_json
    ConvertFrom-Json : Unrecognized escape sequence. (293): {
      "region": "US",
      "proxy_address_exception_list": "1.1.1.1; 2.2.2.2",
      "dsvm_ip_address": "10.1.1.1.",
      "svm_ip_address": "10.2.2.2",
      "url_without_xto": "abc.com",
      "url_with_xto": "def-xto.com",
      "web_server_farm_name": "dss",
      "full_path_to_requestrouter_amd64_msi": "c:\script_downloads\requestrouter_amd64_msi",
      "full_path_to_rewrite_amd64_en_US_msi": "c:\script_downloads\rewrite_amd64_en_US_msi",
      "full_path_to_webfarm_v1_1_amd64_en_US_msi" : "c:\script_downloads\webfarm_v1_1_amd64_en_US_msi",
      "full_path_to_application_host_config": "c:\script_downloads\applicationHost.config"
    }
    At line:3 char:19
    + $parameter_list = ConvertFrom-Json $parameter_list_json
    +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   David Brabant    5 年前

    字符串中缺少转义符:

    "full_path_to_requestrouter_amd64_msi": "c:\script_downloads\\requestrouter.msi",
    

    刚刚 c: . 这应该是 c:\\script . 一旦你解决了这个问题,它就会起作用:

    $json = @"
    {
      "region": "US",
      "proxy_address_exception_list": "1.1.1.1; 2.2.2.2",
      "dsvm_ip_address": "10.1.1.1",
      "svm_ip_address": "10.2.2.2",
      "url_without_xto": "abc.com",
      "url_with_xto": "def-xto.com",
      "web_server_farm_name": "dss",
      "full_path_to_requestrouter_amd64_msi": "c:\\script_downloads\\requestrouter.msi",
      "full_path_to_rewrite_amd64_en_US_msi": "c:\\script_downloads\\rewrite.msi",
      "full_path_to_webfarm_v1_1_amd64_en_US_msi" : "c:\\script_downloads\\webfarm.msi",
      "full_path_to_application_host_config": "c:\\script_downloads\\applicationHost.config"
    }
    "@
    
    $x = $json | ConvertFrom-Json
    
    $x
    
    region                                    : US
    proxy_address_exception_list              : 1.1.1.1; 2.2.2.2
    dsvm_ip_address                           : 10.1.1.1
    svm_ip_address                            : 10.2.2.2
    url_without_xto                           : abc.com
    url_with_xto                              : def-xto.com
    web_server_farm_name                      : dss
    full_path_to_requestrouter_amd64_msi      : c:\script_downloads\requestrouter.msi
    full_path_to_rewrite_amd64_en_US_msi      : c:\script_downloads\rewrite.msi
    full_path_to_webfarm_v1_1_amd64_en_US_msi : c:\script_downloads\webfarm.msi
    full_path_to_application_host_config      : c:\script_downloads\applicationHost.config