简体   繁体   English

如何反序列化字典并获取特定键的值

[英]How to Deserialize the dictionary and fetch the value of a particular key

How should I get the value of status key?我应该如何获取状态键的值?

string a = "\"[{\\\"Status\\\":\\\"Passed\\\"}]\""

tried with JObject.Parse and JArray.Parse to deserialize it and fetch the value.尝试使用JObject.ParseJArray.Parse 对其进行反序列化并获取值。 getting error all the time, even if the key is present.一直出错,即使密钥存在。

PS: this value is coming directly from the DB, where it is stored as a string. PS:这个值直接来自数据库,它存储为一个字符串。

Maybe there is a better solution.也许有更好的解决方案。 But you can remove the / character and split by "但是您可以remove the /字符并按split by "

string a = "\"[{\\\"Status\\\":\\\"Passed\\\"}]\"";
var val=a.Replace("\\", "").Split(new string[] { "\"" },
StringSplitOptions.None)[4];
using System.Text.Json;

string a = "\"[{\\\"Status\\\":\\\"Passed\\\"}]\"";
var json = JsonDocument.Parse(a).RootElement.GetString();
var jDoc = JsonDocument.Parse(json);
var dic = jDoc.RootElement[0].Deserialize<Dictionary<string, string>>();
var status = dic["Status"];

Here is a method can get the value, hope it can give you some help.这里提供一个获取值的方法,希望能给大家一些帮助。

using Newtonsoft.Json;

string a = "\"[{\\\"Status\\\":\\\"Passed\\\"}]\"";
var b = JsonConvert.DeserializeObject(a);
var c = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(b.ToString());

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Json反序列化词典 <key,value> 返回null - Json deserialize Dictionary<key,value> return as null 如何使用自定义键将JSON反序列化为字典 - How to Deserialize JSON into dictionary with custom key Newtonsoft Json将字典从DataContractJsonSerializer反序列化为键/值列表 - Newtonsoft Json Deserialize Dictionary as Key/Value list from DataContractJsonSerializer Json.net 反序列化字典并将字典键分配给字典值的成员变量 - Json.net deserialize dictionary and assign dictionary key to dictionary value's member variable 当键是Xml节点名称时,如何将xml反序列化为Dictionary? - How to deserialize xml into Dictionary when key is the Xml node name? MVC - 如何在不将括号 [0] 添加到键的情况下反序列化字典? - MVC - How to deserialize a Dictionary without having brackets [0] added to the key? 如何将 json 反序列化为字典 c# 并定义密钥? - how to deserialize json into dictionary c# and define the key? Dictionary如何从大量数据中搜索特定的键值? - How does Dictionary search a particular key value out of huge amount of data? 如何对列表进行分组<dictionary<string,string> &gt; 基于特定键并获得最大值</dictionary<string,string> - How to group a List<Dictionary<string,string>> based on particular key and get the max value 如何反序列化firebase fetch - How to deserialize firebase fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM