c# - 将数组添加到属性
问题描述
我正在尝试找到一种将数组添加到属性的方法。目前,我正在毫无问题地添加非数组。
var root = JObject.Parse(contractJson.ToString());
//get company name node
var companyNameMatches = root.Descendants()
.OfType<JObject>()
.Where(x => x["question"] != null && x["question"].ToString() == "Name of the company");
//add answer result to company name node
foreach (JObject jo in companyNameMatches)
{
jo.Add("answer", new JObject(new JProperty("result", Request.Form["Companyname"].ToString())));
}
所以,这一行......如何将“答案”变成一个数组:
jo.Add("answer", new JObject(new JProperty("result", Request.Form["Companyname"].ToString())));
寻找这个输出:
"answer":[
{
"result": "value"
}
]
解决方案
你需要你的answer
属性是一个数组,所以你应该使用JArray
它。更改此行:
jo.Add("answer", new JObject(new JProperty("result", Request.Form["Companyname"].ToString())));
至:
// Create the object to put in the array
var result = new JObject(new JProperty("result", Request.Form["Companyname"].ToString()));
// Create the array as the value for the answer property
jo.Add("answer", new JArray { result });
推荐阅读
- c++ - 如何在反编译的 dll 上找到 Arg 的类型
- android - cursor.getCount() 在模拟器中从外部存储中搜索音频文件时返回 0
- encryption - 如何使用 Flutter 和 Dart 执行文件加密
- javascript - 通过javascript函数v.9 CE过滤查找字段?
- php - 如何在 Codeception 中编写可重用的方法/操作?
- regex - 正则表达式 - 在字符串中查找单词
- python - 不能在 csv-read 迭代器上使用 next()
- development-environment - 如何提高开发环境的速度
- python - 按日期升序和降序排序的列
- c# - 如何用 web.config 部分替换 machine.config 的“oracle.manageddataaccess.client”部分