英文:
Get a Json Element from a changing array
问题
以下是您要翻译的代码部分:
ok so I have the following json response that contains an array of details.
{
"top":{
"middle":[
{
"name": "name1",
"values":[
{
"value": 123
}
]
},
{
"name": "name2",
"values":[
{
"value": 456
}
]
}
]
}
}
I can get the data using:
JObject data = JObject.Parse(JSONDATA);
var Name1 = (string)data["top"]["middle"][0]["name"];
var Value1 = (float)data["top"]["middle"][0]["values"][0]["value"];
However, the order in which the items appear in the array are random.
so I cant use the [0] and [1] to specify the first or second item.
i know i could still use this method and then work out if the name1 or name 2 was a match meaning i would know which value goes with each name. but it seems very long winded and i know there must be an easier way.
i have tried a few things like,
JToken mydata = data.SelectToken("$.top.middle[?(@.name == 'name1')]");
and i tried to get the index of the array based on the name value.
but i just cant get it right. any help would be greatly appreciated.
英文:
ok so I have the following json response that contains an array of details.
{
"top":{
"middle":[
{
"name": "name1".
"values":[
{
"value": 123
}
]
},
{
"name": "name2".
"values":[
{
"value": 456
}
]
}
]
}
}
I can get the data using:
JObject data = JObject.Parse(JSONDATA);
var Name1 = (string)data["top"]["middle"][0]["name"];
var Value1 = (float)data["top"]["middle"][0]["values"][0]["value"];
However, the order in which the items appear in the array are random.
so I cant use the [0] and [1] to specify the first or second item.
i know i could still use this method and then work out if the name1 or name 2 was a match meaning i would know which value goes with each name. but it seems very long winded and i know there must be an easier way.
i have tried a few things like,
JToken mydata = data.SelectToken("$.top.middle[?(@.name == 'name1')]");
and i tried to get the index of the array based on the name value.
but i just cant get it right. any help would be greatly appreciated.
答案1
得分: 0
要获取与特定名称对应的值,您可以遍历数组并检查每个对象的名称属性,直到找到您要查找的对象。以下是一个示例实现:
JObject data = JObject.Parse(JSONDATA);
string nameToFind = "name1";
float value = -1;
JArray middleArray = (JArray)data["top"]["middle"];
foreach (JObject obj in middleArray)
{
string name = (string)obj["name"];
if (name == nameToFind)
{
value = (float)obj["values"][0]["value"];
break;
}
}
if (value != -1)
{
Console.WriteLine($"找到{nameToFind}的值为{value}");
}
else
{
Console.WriteLine($"未找到名称为{nameToFind}的对象。");
}
该实现使用循环来遍历中间数组中的每个对象,并检查名称属性是否与我们要查找的名称匹配。如果匹配成功,它将获取值属性并退出循环。如果未找到匹配的对象,它将输出一条消息指示未找到对象。
请注意,该实现假定中间数组中只有一个具有指定名称的对象。如果可能存在多个具有相同名称的对象,您可能需要修改实现以处理这种情况。
英文:
To get the value corresponding to a specific name, you can loop through the array and check each object's name property until you find the one you're looking for. Here's an example implementation:
JObject data = JObject.Parse(JSONDATA);
string nameToFind = "name1";
float value = -1;
JArray middleArray = (JArray)data["top"]["middle"];
foreach (JObject obj in middleArray)
{
string name = (string)obj["name"];
if (name == nameToFind)
{
value = (float)obj["values"][0]["value"];
break;
}
}
if (value != -1)
{
Console.WriteLine($"The value for {nameToFind} is {value}");
}
else
{
Console.WriteLine($"No object with name {nameToFind} was found.");
}
This implementation uses a loop to iterate through each object in the middle array and checks if the name property matches the one we're looking for. If it does, it retrieves the value property and breaks out of the loop. If it doesn't find a matching object, it outputs a message indicating that no object was found.
Note that this implementation assumes that there is only one object with the specified name in the middle array. If there could be multiple objects with the same name, you may need to modify the implementation to handle that scenario.
答案2
得分: 0
我宁愿使用 Linq
var jObj = JObject.Parse(json);
int value = GetValueByName("name1", jObj) // 123
public int GetValueByName(string name, JObject jObj)
{
return jObj["top"]["middle"]
.Where(x => (string)x["name"] == name)
.Select(x => (int)x["values"][0]["value"])
.FirstOrDefault();
}
英文:
I would rather to use Linq
var jObj = JObject.Parse(json);
int value = GetValueByName("name1", jObj) // 123
public int GetValueByName(string name, JObject jObj)
{
return jObj["top"]["middle"]
.Where(x => (string)x["name"] == name)
.Select(x => (int)x["values"][0]["value"])
.FirstOrDefault();
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论