示例#1
0
        /// <summary>
        /// Converts json string to dynamic object collections datatable.
        /// </summary>
        /// <param name="json">Json string to extend.</param>
        /// <param name="type">The type.</param>
        /// <param name="selectedFields">Selected fields.</param>
        /// <param name="selectFieldsOnly">Whether to use select fields only.</param>
        /// <returns>DataTable object</returns>
        public static DataTable ToDynamicObjects(this string json, Type type, List <string> selectedFields, bool selectFieldsOnly)
        {
            var    data         = new DataTable();
            JArray jarr         = JArray.Parse(json);
            bool   columnsAdded = false;

            foreach (JObject jobject in jarr.Children <JObject>())
            {
                var settings = new JsonSerializerSettings();
                DeserializerExceptionsContractResolver resolver = DeserializerExceptionsContractResolver.Instance;
                resolver.JsonObjectToDeserialize = jobject;
                settings.ContractResolver        = resolver;
                object tempObject = JsonConvert.DeserializeObject(jobject.ToString(), type, settings);

                var propertyDescriptors = new List <PropertyDescriptor>();
                if (selectFieldsOnly)
                {
                    if (selectedFields != null && selectedFields.Count > 0)
                    {
                        propertyDescriptors.AddRange(
                            TypeDescriptor.GetProperties(
                                tempObject.GetType()).Cast <PropertyDescriptor>().Where(property => selectedFields.Contains(property.Name)));
                    }
                }
                else
                {
                    propertyDescriptors.AddRange(TypeDescriptor.GetProperties(tempObject.GetType()).Cast <PropertyDescriptor>());
                }

                if (!columnsAdded)
                {
                    for (int i = 0; i < propertyDescriptors.Count; i++)
                    {
                        PropertyDescriptor prop = propertyDescriptors[i];
                        data.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                    }

                    columnsAdded = true;
                }

                var values = new object[propertyDescriptors.Count];
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = propertyDescriptors[i].GetValue(tempObject);
                }

                data.Rows.Add(values);
            }

            return(data);
        }
 // Using an explicit static constructor enables lazy initialization.
 static DeserializerExceptionsContractResolver()
 {
     instance = new DeserializerExceptionsContractResolver();
 }