JObject 以变量为值解析数据

编程入门 行业动态 更新时间:2024-10-08 08:29:05
本文介绍了JObject 以变量为值解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我需要通过变量设置其中一个值时,我试图破译使用 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 的这个文档:www.algolia/doc/api-reference/api-methods/partial-update-objects/

I'm following this documentation from Algolia: www.algolia/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 以变量为值解析数据

本文发布于:2023-11-28 20:24:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:量为   数据   JObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!