【发布时间】:2021-07-03 00:43:10
【问题描述】:
我一直在尝试覆盖我的 JObject 中的值(我从网络应用程序中接收它,所以它可能会有所不同)。我尝试使用 for 循环,但我不知道如何使用 JObject 进行迭代。 Foreach 没用,因为我无法编辑 JObject 中的值。
这是我的尝试:
public JObject TranslateJson(string jsonString, LanguageCharset languageCharset)
{
JObject json = JObject.Parse(jsonString);
for ( int i = 0; i > json.Count; i++)
{
json[i].Value = "test"; //Compiler Error CS1656
}
return json;
}
#Update1
public JObject TranslateJson(string jsonString, LanguageCharset languageCharset)
{
JObject json = JObject.Parse(jsonString);
foreach (var x in json)
{
json[x.Key] = "test";
}
return json;
}
#Update2
部分json:
{
"common": {
"appName": "[App name] Web",
"other": "other",
"others": "others",
"dateRange": {
"start": "Date from",
"end": "Date to"
},
"sidebar": {
"dashboard": "[App name] dashboard",
"violations": {
"list": "Infringement list",
"timeline": "Activities and infringements"
},
"drivers": "Driver list",
"profile": "Profile",
"settings": "Settings",
"logout": "Sign out"
},
"tariff": {
"name": {
"CZ": "Czech tariff",
"RO": "Romanian tariff",
"DE": "German tariff",
"GB": "British tariff",
"LT": "Lithuanian tariff",
"DK": "Danish tariff",
"PL": "Polish tariff",
"BE": "Belgian tariff",
"CH": "Swiss tariff",
"FR": "French tariff",
"LU": "Luxembourgish tariff",
"SE": "Swedish tariff",
"HU": "Magyar tariff"
[..] (more nestings)
#更新 3
示例输入 json:
{
"weapons":
{
"axe": "axe",
"sword": "sword" },
"tools":
{
"saw": "saw"
}
}
示例输出:
{
"weapons":
{
"axe": "translated",
"sword": "translated" },
"tools":
{
"saw": "translated"
}
}
为什么只翻译?因为当我可以覆盖该 json 结构的任何嵌套中的值时,我就可以调用我的 transtalion 函数。
【问题讨论】:
-
完全没有,检查我的更新:。
-
我被那条评论弄糊涂了。要么文档是错误的,要么没有重现您的错误。正如您在此演示中看到的那样dotnetfiddle.net/2zl09h。或者我们缺少minimal reproducible example
-
请详细描述您的问题,并给出您需要更换的对象,您可以提供您的
json
标签: c# json .net loops asp.net-core