简体   繁体   English

解析包含不同大小写的相同键的 JSON 字典

[英]Parsing a JSON dictionary that contains the same key with different casing

I have a problem;我有个问题; I would to know if there is a method to parse json file without having a unique format.我想知道是否有一种方法可以在没有唯一格式的情况下解析 json 文件。 So it may have different attributes but all of them contain the attribute Status but it can be in double.所以它可能有不同的属性,但它们都包含属性状态,但它可以是双重的。

  {
  "requestid": "1111",
  "message": "db",
  "status": "OK",
  "data": [
    {
      "Status": "OK", // this one I would to test first to read the other attributes
      "fand": "",
      "nalDate": "",
      "price": 1230000,
      "status": 2
    }
  ]
}

The defacto standard Json serializer for .NET is Newtonsoft.Json ( How to install ). .NET 的事实上的标准 Json 序列化程序是Newtonsoft.Json如何安装)。 You can parse the Json into an object graph and work on that in any order you like:您可以将 Json 解析为对象图并按您喜欢的任何顺序进行处理:

namespace ConsoleApp3
{
    using System;
    using Newtonsoft.Json.Linq;

    class Program
    {
        static void Main()
        {
            var text = @"{
                'requestid': '1111',
                'message': 'db',
                'status': 'OK',
                'data': [
                {
                    'Status': 'OK', // this one I would to test first to read the other attributes
                    'fand': '',
                    'nalDate': '',
                    'price': 1230000,
                    'status': 2
                }
                ]
            }";

            var json = JObject.Parse(text);

            Console.WriteLine(json.SelectToken("data[0].Status").Value<string>());
            Console.ReadLine();
        }
    }
}

With https://www.newtonsoft.com/json随着https://www.newtonsoft.com/json

Data data = JsonConvert.DeserializeObject<Data>(json);

And create the class Data with the interesting data inside the json并在 json 中使用有趣的数据创建类 Data

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

相关问题 通过与其他类型进行比较来检查字典是否包含密钥 - Check if Dictionary contains key by comparing with different type 如果使用LINQ包含相同键的项,如何更新字典? - How to update Dictionary if it contains item with same key using LINQ? 将JSON文件反序列化为包含具有动态键的字典的类 - deserializing JSON file to Class that contains dictionary with dynamic key 在Windows Phone 8中解析复杂的JSON对象(字典键/值对) - Parsing Complicated JSON object in windows phone 8 (Dictionary Key/Value pairs) 检查字典键包含 - Check dictionary Key Contains 我可以保存字典吗 <string, string> 在Bot Framework中存储ConversationData时的关键框? - Can I preserve Dictionary<string, string> key casing when storing in Bot Framework ConversationData? 使用JSON解析C#字典 - Parsing C# Dictionary with JSON 根据该键的属性确定字典是否包含键? - Determine if dictionary contains key based on a property of that key? 关键字名称为json的字典 - dictionary to json with key names 已解决“已添加项目”的解决方法。 键入字典“每次使用相同的键但具有不同的值 - Workaround to “Item has already been added. Key in dictionary” using the same key everytime but with a different value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM