首页 > 解决方案 > 问题将 Jobject 附加到现有 Jobject ,覆盖现有 JSON 而不附加

问题描述

我从 API 获得 JSON 响应,我想将每次调用的 API 响应附加到 JSON,在初始 API 响应中,我将创建如下所示的 json 文件。从第二个响应中,我只会向现有的 json 添加几个值,如果 json 文件已经存在,我会尝试附加值,如果不想创建新文件,下面是示例代码,当我尝试附加时,其覆盖完整的json文件,带有新值而不附加它,请建议

来自 API 的响应

{"properties":{"assignedBy":"put test","policyType":"Custom","mode":"All","description":"Force resource names to begin with given 'prefix' and/or end with given 'suffix'","metadata":{"category":"Naming","createdBy":"33a3582b-5fb6-4aad-8e24-7f751bc0cbe1","createdOn":"2019-12-18T01:03:23.6075759Z","updatedBy":"33a3582b-5fb6-4aad-8e24-7f751bc0cbe1","updatedOn":"2019-12-19T00:47:52.6835216Z"},"parameters":{"ringValue":{"type":"Array","metadata":{"displayName":"put test","description":"The list of tags that can be specified when deploying resources."}}},"policyRule":{"if":{"allOf":[{"field":"type","equals":"Microsoft.KeyVault/vaults"},{"anyOf":[{"field":"tags[Ring]","in":"[parameters('ringValue')]"}]}]},"then":{"effect":"audit"}}},"id":"/providers/Microsoft.Management/managementgroups/JamamidaMGTest/providers/Microsoft.Authorization/policyDefinitions/KVPolicy.json","type":"Microsoft.Authorization/policyDefinitions","name":"KVPolicy.json"}

当前代码

                var jobject = JObject.Parse(file);

                        if (!blockBlob.Exists())
                        {
                            JObject js = new JObject(
                                new JProperty("properties",
                                new JObject(
                                    new JProperty("displayName", (object)jobject["properties"]["displayName"]),
                                    new JProperty("description", (object)jobject["properties"]["description"]),
                                    new JProperty("policyDefinations",
                                    new JArray(new JObject(
                                        new JProperty("assignedBy", "a")))));


                            modifiedfile = js.ToString();
                            await blockBlob.UploadTextAsync(modifiedfile);
                            Console.WriteLine(file);
                        }
                        else
                        {

                        var ms = new MemoryStream();
                        var policyData = blockBlob.DownloadText();

                        var currentJObject = JObject.Parse(policyData);
                        var currentArray = (JArray)currentJObject["properties"]["policyDefinations"];

                        var data = JsonConvert.DeserializeObject<PolicyJsonEntity>(policyData);

                        currentArray.AddAfterSelf(new JObject(
                               new JArray(new JObject(
                                   new JProperty("assignedBy", "jaya")))));

第一个输出:

  "properties": {
    "displayName": "put test",
    "description": "Force resource names to begin with given 'prefix' and/or end with given 'suffix'",
    "policyDefinations": [
      {
        "assignedBy": "a"
      }
    ]

  }
}

每次附加后它应该看起来像

"properties": {
    "displayName": "put test",
    "description": "Force resource names to begin with given 'prefix' and/or end with given 'suffix'",
    "policyDefinations": [
      {
        "assignedBy": "xyz@gmail.com"
      },
      {
        "assignedBy": "123@hotmail.com"
      },
      {
        "assignedBy": "sdjk@123.com"
      }
    ]
  }
}

标签: jsonjson.netjsonp

解决方案


推荐阅读