C# 嗨,如果我有一个特定json值的键,我如何访问它的长度呢。I';我和C一起工作# 使用(StreamReader r=newstreamreader(filePath)) { 字符串json=r.ReadToEnd(); JObject data=JObject.Parse(json); foreach(数据中的变量x) { 如果(x.Key.Contains(产品)) { Console.WriteLine(x.Key.Contains(product)); //因为(int m=0;m
有点不清楚你到底想要什么,但让我来帮你。 让我们先了解您的JSON模式。它有C# 嗨,如果我有一个特定json值的键,我如何访问它的长度呢。I';我和C一起工作# 使用(StreamReader r=newstreamreader(filePath)) { 字符串json=r.ReadToEnd(); JObject data=JObject.Parse(json); foreach(数据中的变量x) { 如果(x.Key.Contains(产品)) { Console.WriteLine(x.Key.Contains(product)); //因为(int m=0;m,c#,json,asp.net-mvc,C#,Json,Asp.net Mvc,有点不清楚你到底想要什么,但让我来帮你。 让我们先了解您的JSON模式。它有productCategory(“书籍”、“计算机”、“游戏”等),每个productCategory都有属性数组(对于“书籍”->[“格式”、“页面”、“维度”…] 因此,现在如果您想遍历productCategory并计算其长度,可以执行以下操作: using (StreamReader r = new StreamReader(filePath)) {
productCategory
(“书籍”、“计算机”、“游戏”等),每个productCategory
都有属性数组(对于“书籍”->[“格式”、“页面”、“维度”…]
因此,现在如果您想遍历productCategory并计算其长度,可以执行以下操作:
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var x in data)
{
if (x.Key.Contains(product))
{
Console.WriteLine(x.Key.Contains(product));
// for (int m = 0; m <= x.Value.ToString().Length; m++)
}
}
Console.WriteLine(tempList.Count);
Console.WriteLine("");
}
}
else
{
Console.WriteLine("ERROR: Data not returned by Database...");
return View();
}
}
else
{
Console.WriteLine("ERROR: No Value recieved by client...");
return View();
}
return View();
}
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var productCategory in data)
{
// counts the number of properties like for eg. For "Book" it has 8 properties like "Format","Page" etc.
var count = productCategory.Value.Children().ToList().Count;
Console.WriteLine($"{productCategory.Key} Count: {count}");
}
}
输出:
如果还希望遍历每个productCategory的属性,可以执行以下操作:
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var x in data)
{
if (x.Key.Contains(product))
{
Console.WriteLine(x.Key.Contains(product));
// for (int m = 0; m <= x.Value.ToString().Length; m++)
}
}
Console.WriteLine(tempList.Count);
Console.WriteLine("");
}
}
else
{
Console.WriteLine("ERROR: Data not returned by Database...");
return View();
}
}
else
{
Console.WriteLine("ERROR: No Value recieved by client...");
return View();
}
return View();
}
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var productCategory in data)
{
// counts the number of properties like for eg. For "Book" it has 8 properties like "Format","Page" etc.
var count = productCategory.Value.Children().ToList().Count;
Console.WriteLine($"{productCategory.Key} Count: {count}");
}
}
输出:
非常感谢你帮助我。
非常感谢
我尝试了OhmnioX给出的想法,并对代码进行了处理,最后创建了以下代码段
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
JObject data = JObject.Parse(json);
foreach (var productCategory in data)
{
// counts the number of properties like for eg. For "Book" it has 8 properties like "Format","Page" etc.
var count = productCategory.Value.Children().ToList().Count;
Console.WriteLine($"{productCategory.Key} Count: {count}");
foreach (var properties in productCategory.Value)
{
//Here you will have properties like "Format","Page" etc..
Console.WriteLine(properties);
}
Console.WriteLine("");
}
}
使用(StreamReader r=newstreamreader(filePath))
{
字符串json=r.ReadToEnd();
JObject data=JObject.Parse(json);
foreach(数据中的变量x)
{
如果(x.Key.ToString()包含(产品))
{
var cnt=x.Value.Children().ToList().Count;
对于(int m=0;请访问并检查Hi OhmnioX,非常感谢您提供的解决方案,现在它对我有意义了。