获取具有一定值的json对象的特定部分 [英] Get specific part of json object which has a certain value
问题描述
我正在尝试获取Json的特定部分,但不知道如何做.
I'm trying to get a specific part of Json, but couldn't figure out how.
我的json看起来像:
My json looks like:
[
{
"ResourceId":"1",
"ResourceText":"Hello",
"LanguageId":"1"
},
{
"ResourceId":"2",
"ResourceText":"World",
"LanguageId":"1"
}
.
.
.
]
因此,例如,我需要零件(根据pResourceId参数进行更改,因此我每次都尝试更改)
So for example I want the part (changes according to pResourceId parameter, so what I'm trying to get changes everytime)
{
"ResourceId":"1",
"ResourceText":"Hello",
"LanguageId":"1"
}
但是我听不懂.到目前为止,我尝试过的事情:
But I couldn't get it. What I tried so far:
JObject data = JObject.Parse(jsonStringResources);
return data.Values().Where(x => x.Contains("ResourceId\" : \"" +pResourceId +"\"") ) as clsResource;
推荐答案
我将为您的JSON格式建立一个模型:
I would make a model for the format of your JSON:
class Model
{
public string ResourceId { get; set; }
public string ResourceText { get; set; }
public string LanguageId { get; set; }
}
然后您可以执行以下操作:
And then you can do:
var models = JsonConvert.DeserializeObject<List<Model>>(jsonStringResources);
var matchingModel = models.Where(model => model.ResourceId == pResourceId).ToList().FirstOrDefault();
return JsonConvert.SerializeObject(matchingModel);
我们将JSON反序列化为我们的模型列表.然后,我们得到ResourceId
与pResourceId
匹配的模型.最后,我们再次序列化模型以获取JSON字符串.
We deserialize the JSON into a list of our models. We then get the model where the ResourceId
matches the pResourceId
. Finally, we serialize the model again to get the JSON string.
pResourceId == 1
时,结果为:
{
"ResourceId": "1",
"ResourceText": "Hello",
"LanguageId": "1"
}
这篇关于获取具有一定值的json对象的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!