示例#1
0
        private Dictionary <string, object> JsonToDictioanary(JToken jtoken)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            LikeActionButWithParameter processJObject = (JObject jobject) =>
            {
                foreach (var s in JObject.Parse(jobject.ToString()))
                {
                    string name  = s.Key;
                    JToken value = s.Value;

                    var r = value.Type;

                    if (value.HasValues)
                    {
                        if (value.Type is Newtonsoft.Json.Linq.JTokenType.Array)
                        {
                            dictionary.Add($"{name}Array", JsonToDictioanary(value));
                        }
                        else
                        {
                            dictionary.Add(name, JsonToDictioanary(value));
                        }
                    }

                    else
                    {
                        if (ReplaceJsonTypeByCsharpType(value.Type.ToString(), out string CsharpType))
                        {
                            dictionary.Add($"{name}_{CsharpType}", value.ToString());
                        }
                        else
                        {
                            dictionary.Add($"{name}_{value.Type}", value.ToString());
                        }
                    }
                }
            };

            if (jtoken is JObject)
            {
                processJObject((JObject)jtoken);
            }
            if (jtoken is JArray)
            {
                foreach (JObject child in jtoken.Children <JObject>())
                {
                    processJObject(child);
                }
            }
            return(dictionary);
        }
        private Dictionary <string, object> JsonToDictioanary(JToken jtoken)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            LikeActionButWithParameter processJObject = (JToken jobject) =>
            {
                foreach (var s in JObject.Parse(jobject.ToString()))
                {
                    string name  = s.Key;
                    JToken value = s.Value;

                    try
                    {
                        if (value.HasValues)
                        {
                            if (value.Type is Newtonsoft.Json.Linq.JTokenType.Array)
                            {
                                if (!value.Children().FirstOrDefault().HasValues)
                                {
                                    if (ReplaceJsonTypeByCsharpType(value.Children().FirstOrDefault().Type.ToString(), out string CsharpType))
                                    {
                                        dictionary.Add($"{name}_{CsharpType}Array__{_iterationsCount}", name);
                                    }
                                    else
                                    {
                                        dictionary.Add($"{name}_{value.Children().FirstOrDefault().Type}Array__{_iterationsCount}", name);
                                    }
                                }
                                else
                                {
                                    if (ReplaceJsonTypeByCsharpType(value.Children().FirstOrDefault().Type.ToString(), out string CsharpType))
                                    {
                                        dictionary.Add($"{name}_{CsharpType}Array__{_iterationsCount}", JsonToDictioanary(value));
                                    }
                                    else
                                    {
                                        dictionary.Add($"{name}Array__{_iterationsCount}", JsonToDictioanary(value));
                                    }
                                }
                            }
                            else
                            {
                                dictionary.Add($"{name}__{_iterationsCount}", JsonToDictioanary(value));
                            }
                        }

                        else
                        {
                            if (ReplaceJsonTypeByCsharpType(value.Type.ToString(), out string CsharpType))
                            {
                                dictionary.Add($"{name}_{CsharpType}__{_iterationsCount}", value.ToString());
                            }
                            else
                            {
                                dictionary.Add($"{name}_{value.Type}__{_iterationsCount}", value.ToString());
                            }
                        }
                        _iterationsCount++;


                        _upperLevelClassesCount++;
                    }
                    catch { }
                }
            };


            if (jtoken is JObject)
            {
                processJObject(jtoken);
            }
            if (jtoken is JArray)
            {
                foreach (JObject child in jtoken.Children <JObject>())
                {
                    processJObject(child);
                }
            }
            return(dictionary);
        }