从 json c# 中提取键的值

  • 本文关键字:提取 json c# json assert
  • 更新时间 :
  • 英文 :


我有以下Json(是附加到项目的文件(:

{
"Errors": {
"NoCountry": {
"MSG": "The CountryCode field is required.",
"Description": "Error encountered when Country parameter is missing from the request"
},
"NoLOI": {
"MSG": "Validation failed: rn -- LengthOfInterview cannot be empty"
"Description": "Error encountered when LOI parameter is missing from the request"
}
}
}

我需要提取例如 Errors.NoCompletes 的值.MSG以便在断言中使用它以将其与我从 API 获得的输出进行比较。 到目前为止,我尝试创建一个看起来像这样的字典:

public class ErrorsDictionary
{
public string MSG;
public string Description;
}
public class DicRoot
{
public Dictionary<string, ErrorsDictionary> Errors { set; get; }
}

并像这样使用它:

DicRoot Json = JsonConvert.DeserializeObject<DicRoot>(File.ReadAllText(@"c:usersbanu_sourcereposTestJsonLibTestJsonLibJsonErrorMSG.json"));
foreach (var f in Json.Errors)
{
Console.WriteLine("Nume={0} Mesaj={1} Description={2}",f.Key, f.Value.MSG, f.Value.Description);
}

问题是我无法弄清楚,我如何提取一个特定的值,就像我上面所说的 Errors.NoLOI.MSG,以便能够在像 Assert.Equals(例如,MyParam(这样的断言中使用它;

如果您愿意,还可以使用 JsonPath、匿名类型和字符串内插:

JObject obj = JObject.Parse(json);
var errors = obj
.SelectTokens("$.Errors.*")
.ToDictionary(
e => ((JProperty)e.Parent).Name,
e => new { Msg = e["MSG"], Descr = e["Description"] });
foreach (var e in errors)
{
Console.WriteLine($"Nume={e.Key} Mesaj={e.Value.Msg} Description={e.Value.Descr}");
}

我想你要的是这个?

DicRoot dict = JsonConvert.DeserializeObject<DicRoot>(File.ReadAllText("foo.json"));
string msg = dict["NoLOI"].MSG;

我知道这看起来有点解决。但是,它正在起作用。

class Program
{
static void Main(string[] args)
{
string json = "{"Errors": {"NoCountry": {"MSG": "The CountryCode field is required.","Description": "Error encountered when Country parameter is missing from the request"},"NoLOI": {"MSG": "Validation failed: rn -- LengthOfInterview cannot be empty", "Description": "Error encountered when LOI parameter is missing from the request"},}}";
var Json = JsonConvert.DeserializeObject<ErrorsClass>(json);
var obj = JsonConvert.DeserializeObject<Dictionary<string, ErrorsDictionary>>(Json.Errors.ToString());
foreach (var f in obj)
{
Console.WriteLine("Nume={0} Mesaj={1} Description={2}", f.Key, f.Value.MSG, f.Value.Description);
}
Console.Read();
}
}
public class ErrorsDictionary
{
public string MSG { get; set; }
public string Description { get; set; }
}
public class DicRoot
{
public Dictionary<string, ErrorsDictionary> ErrorsDic { set; get; }
}
class ErrorsClass
{
public object Errors { get; set; }
}

输出:

Nume=NoCountry Mesaj=The CountryCode field is required. Description=Error encountered when Country parameter is missing from the request Nume=NoLOI Mesaj=Validation failed: -- LengthOfInterview cannot be empty Description=Error encountered when LOI parameter is missing from the request

您可以使用Newtonsoft.json NuGet。试试这个

var files = JObject.Parse(YourJson);
var recList = files.SelectTokens("$..Errors").ToList();
foreach (JProperty prop in recList.Children())
{
string key = prop.Name.ToString();
string value = prop.Value.ToString();
//Do your stuffs here
}

相关内容

  • 没有找到相关文章

最新更新