当属性具有特殊字符时,使用JSON.Net引用对象属性

本文关键字:属性 JSON Net 对象 使用 引用 特殊字符 | 更新日期: 2023-09-27 18:25:11

我正在使用JSON.Net更新XML文件中的对象。这太棒了!然而,我需要更新一个包含一些特殊字符的属性,并且我找不到任何关于如何使用JSON.Net.执行此操作的信息

XML

<ProfileSettings>{
  "Name": "Default",
  "Status": "New",
  "DeploymentVariants": {
    "SPSite": {
      "Signature": "SPSite",
      "Type": "SPSite",
      "Label": "SharePoint Site",
      "Source": "Solution",
      "DefaultValue": "http://google.com",
      "Value": "http://google.com"
    },
    "Process/Participant[@Name='"Manager'"]&gt;User": {
      "Signature": "Process/Participant[@Name='"Manager'"]&gt;User",
      "Type": "SPUser",
      "Label": "User for swim lane Manager",
      "Source": "Swimlane",
      "DefaultValue": "John Smith",
      "Value": "John Smith"
    }
  },
  "DeploymentScripts": {},
  "SPList": "mySPList"
}</DeploymentProfile>

为了更新SPSite对象中的DefaultValue,我可以像这样使用JSON.Net:

dynamic fromSolution = JsonConvert.DeserializeObject(profileObject);
fromSolution.DeploymentVariants.SPSite.DefaultValue = txtSPSite.Text;

但是,如果我试图访问进程/参与者[@Name=''"Manager''"]>用户对象。如果一处房产有像这样的特殊字符,我该如何访问它?

fromSolution.DeploymentVariants.Process.DefaultValue不起作用,显然在其中包含特殊的字符只会导致运行时错误。

当属性具有特殊字符时,使用JSON.Net引用对象属性

JObject实现IDictionary<string, object>。您可以使用字典语法:

fromSolution.DeploymentVariants["Process/Participant[@Name='"Manager'"]>User"].DefaultValue