本文介绍了如何从json字符串中获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从json数据中获取特定值但是获取null或异常 以下是我的json数据
I have tried to get specific value from json data but getting null or exception The below is my json data
{ "Name": "Kumar", "policyName": "Lic", "targetResource": "3Months", "attributes": { "MajorIssue": { "attributeId": 1, "attributeKey": "check", "attributeValue": "7776000000" } }, "enabled": true }需要从上面的json数据中得到这个值
need to get this value from the above json data
"attributeValue": "7776000000"我有什么尝试过: 以下是我试图获取具体值的代码
What I have tried: The below is my code that i have tried to fetch the specific value
dynamic Result = JsonConvert.DeserializeObject(jsonResultFromAPI); foreach (var item in Result ) { retentionPeriod = item.attributes.attributeValue;//API Value; }推荐答案
我建议创建模型来展示你的JSON数据: I suggest to create models to present your JSON data: public class MajorIssue { public int attributeId { get; set; } public string attributeKey { get; set; } public string attributeValue { get; set; } } public class Attributes { public MajorIssue MajorIssue { get; set; } } public class RootObject { public string Name { get; set; } public string policyName { get; set; } public string targetResource { get; set; } public Attributes attributes { get; set; } public bool enabled { get; set; } }
因此您可以轻松地反序列化对象:
so you can deserialize objects easily:
dynamic Result = JsonConvert.DeserializeObject<RootObject>(jsonResultFromAPI); var value = Result.Attributes.MajorIssue.attributeValue;
更多推荐
如何从json字符串中获取特定值
发布评论