JObject 以变量为值解析数据 [英] JObject Parse Data with a variable as the value
问题描述
当我需要通过变量设置其中一个值时,我试图破译使用 JObject Parse 的正确语法.这是为了使用 Algolia 将新对象推送到我的搜索索引.
I am trying to decipher the correct syntax for using JObject Parse when I need to have one of the values set by a variable. This is for using Algolia to push a new object to my search index.
songIndexHelper.PartialUpdateObject(JObject.Parse(@"{""ApprovalFL"":"true",
""objectID"":"'+Accepted.Value+'"}"));
我从我的函数参数中收到 Accepted.Value
.例如,Accepted.Value 可以等于 98.此外,true
应该被格式化为布尔值而不是字符串.以上是我的尝试.我应该如何修正我的语法?
I receive Accepted.Value
from my function argument. For example, Accepted.Value could equal something like 98. Also, true
should be formatted as boolean instead of a string. The above is my attempt. How should I fix my syntax?
我正在关注 Algolia 的这个文档:https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/
I'm following this documentation from Algolia: https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/
有关更多上下文,这是函数中的上述行:
For more context, here is the above line in the function:
public ActionResult Index(int? Accepted, int? Denied)
{
var accountInfo = EntityDataAccess.GetAccountInfoByUserID(User.Identity.GetUserId());
if(accountInfo == null || accountInfo.AdminFL == false || accountInfo.LabelFL == true)
{
return RedirectToAction("Index", "Home");
}
else
{
if(Accepted != null)
{
EntityDataAccess.AcceptSong(Accepted.Value);
var songIndexHelper = HttpContext.Application.Get("SongIndexHelper") as IndexHelper<SongAlgoliaModel>;
songIndexHelper.PartialUpdateObject(JObject.Parse(@"{""ApprovalFL"":""true"",
""objectID"":""Accepted.Value""}"));
}
推荐答案
songIndexHelper.PartialUpdateObject(JObject.Parse(@"{""ApprovalFL"":""true"",
""objectID"":""Accepted.Value""}"));
应该是:
songIndexHelper.PartialUpdateObject(JObject.Parse(@"{""ApprovalFL"":true,
""objectID"":" +Accepted.Value+ "}"));
关键是用+
把Accepted
的值串联起来,而不是用引号把true
包裹起来.
The key is to use +
to concatenate in the value of Accepted
, and not wrap true
in quotes.
我建议的另一种方法是根本不使用字符串.考虑以下方法:
Another approach I would suggest is not using strings at all. Consider an approach like:
var bob = new { ApprovalFL = true, objectID = Accepted.Value};
var obj = JObject.FromObject(bob);
songIndexHelper.PartialUpdateObject(obj);
这篇关于JObject 以变量为值解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!