如何从json字符串中获取特定值

编程入门 行业动态 更新时间:2024-10-26 12:22:42
本文介绍了如何从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字符串中获取特定值

本文发布于:2023-10-12 00:04:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483223.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   定值   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!