json - 问题将 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"
}
]
}
}
解决方案
推荐阅读
- python - 如何更新我的 AWS 凭证以使用 S3?
- sql - 如何从查询 SQL 中查询
- javascript - 如何使用 React Native 获得内部手机声音?
- python - 无法使用 IIS 在 Windows 2016 上运行的 Flask 中打开 Excel 文件
- react-native-navigation - 如何使用 react-native-navigation 在 iOS 上增加滑动区域?
- python - 如何使用 pytesseract 改进结果
- javascript - 使用按钮控制 iframe 宽度
- .net - 如何使 Entity Framework Core DbSet Take 和 Skip 采用 long 类型,而不是 int(我有超过 40 亿行)
- python - Python 输出未在 Visual Studio Code 中一次显示
- .net-core - 我设置了任何 CORS 设置,但在我的 ASP.NET Core 5 Web API 中不起作用