从 json c# 中提取键的值

Extract value for key from json c#

我有以下Json(是项目附带的文件):

{
  "Errors": {
    "NoCountry": {
      "MSG": "The CountryCode field is required.",
      "Description": "Error encountered when Country parameter is missing from the request"
    },
    "NoLOI": {
      "MSG": "Validation failed: \r\n -- 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:\users\banu_\source\repos\TestJsonLib\TestJsonLib\Json\ErrorMSG.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 所说的那样,以便能够在 [=24= 这样的断言中使用它](例如,MyParam);

我想你问的是这个?

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: \r\n -- 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

如果愿意,您还可以使用 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}");
}

您可以使用 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
}