简体   繁体   English

将 JSON 对象/字符串转换为 C# 动态对象并有权访问属性/值?

[英]Convert a JSON object/string to a C# dynamic object and have access to the properties/values?

I am trying to take a huge JSON file (could be a string) and without knowing the actual structure of the data I want to read and process it as a class in C#.我正在尝试获取一个巨大的 JSON 文件(可能是一个字符串),并且不知道我想要读取的数据的实际结构,并将其作为 C# 中的类进行处理。 I tried using JSON to deserialize it but I wasn't totally sure about where to go after that.我尝试使用 JSON 来反序列化它,但我不完全确定在那之后去哪里。 I was thinking of using Reflections but not sure what data I need.我正在考虑使用反射,但不确定我需要什么数据。

I have tried to deserialize the object as the code shows.我试图反序列化对象,如代码所示。 But I want to test if it is the right object type incase it isn't I would hope it fails but I can't seem to get past this part.但是我想测试它是否是正确的对象类型,因为它不是我希望它失败,但我似乎无法通过这部分。 I also am not sure what to do with reflections inside of the check.我也不知道如何处理支票内部的反射。 I know I should iterate but not sure which property values inside of the object will contain what I need.我知道我应该迭代但不确定对象内的哪些属性值将包含我需要的内容。

string jsonData = sr.ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(jsonData);
if (data is List<dynamic>)
{
    data.GetType().GetProperties();
}

I want an object that has all the access to the data from a JSON file/string.我想要一个可以访问来自 JSON 文件/字符串的数据的对象。

I think one of your problems is to use JArray instead of List and also you need to cast your item to JObject. 我认为您的问题之一是使用JArray而不是List,并且还需要将项目强制转换为JObject。 Use Newtonsoft.Json and Newtonsoft.Json.Linq then you can read your Json as an example: 使用Newtonsoft.Json和Newtonsoft.Json.Linq,然后您可以阅读您的Json作为示例:

string __content = "[ {\"name\": \"person1\" , \"age\": 33} , {\"name\": \"person2\" , \"age\" : 23} ]";

        dynamic data = JsonConvert.DeserializeObject(__content);
        // make sure you have an array of object
        if (data is Newtonsoft.Json.Linq.JArray)
        {
            int i = 0;
            foreach (dynamic item in data)
            {
                // get the property of the object 
                JObject currentitem = item as JObject;
                if (currentitem != null)
                {
                    // access to value of each property
                    foreach (JProperty p in currentitem.Properties())
                    {
                        Console.WriteLine("[" + i + "] : " + p.Name + ":" + p.Value.ToString());
                    }
                    i++;
                }
            }
        }

You can use the DeserializeObject method with a generic type. 您可以将DeserializeObject方法与通用类型一起使用。 In order to assure the Type you want to cast to. 为了确保您要转换为Type。 Which mean your code will be as following: 这意味着您的代码将如下所示:

var data = JsonConvert.DeserializeObject<T>(jsonData);

Where T is the class you want to parse to. T是您要解析的类。 This method will throw an exception in case the Json string can't be parsed to your class. 如果无法将Json字符串解析到您的类,则此方法将引发异常。 I think that's what you want. 我想这就是你想要的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM