Esempio n. 1
0
        public static bool SetShortcutKeyJsonValue(JObject jobject, string key, string value)
        {
            try
            {
                JEnumerable <JToken> jToken = jobject["Settings"].Children();

                IEnumerator enumerator = jToken.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    JToken jc = (JToken)enumerator.Current;
                    if (jc is JObject || ((JProperty)jc).Value is JObject)
                    {
                        SetShortcutKeyJsonValue((JObject)jc, key, value);
                    }
                    else
                    {
                        if (((JProperty)jc).Name == key)
                        {
                            ((JProperty)jc).Value = value;
                        }
                    }
                }
                Console.WriteLine(jobject.ToString());
                File.WriteAllText(jsonPath, jobject.ToString());
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("e.Message: " + e.Message + ";e.StackTrace: " + e.StackTrace);
                return(false);
            }
        }
Esempio n. 2
0
        public static void UpdateTabPagename(string action, JObject jobject, string key, string oldvalue, string newvalue, int isFirst)
        {
            JEnumerable <JToken> jToken = new JEnumerable <JToken>();
            string apptag = "";

            if (isFirst == 0)
            {
                jToken = jobject["AppListModel"].Children();
            }
            else
            {
                jToken = jobject["AppInfo"].Children();
                apptag = GetJsonValue(jToken, "apptag");
            }

            IEnumerator enumerator = jToken.GetEnumerator();

            while (enumerator.MoveNext())
            {
                JToken jc = (JToken)enumerator.Current;
                if (jc is JObject || ((JProperty)jc).Value is JObject)
                {
                    if (jc.Count() > 0)
                    {
                        UpdateTabPagename(action, (JObject)jc, key, oldvalue, newvalue, 1);
                    }
                }
                else
                {
                    if (apptag == oldvalue)
                    {
                        if (action == "edit")
                        {
                            if (((JProperty)jc).Name == key)
                            {
                                ((JProperty)jc).Value = newvalue;
                            }
                        }
                        else
                        {
                            jc.Parent.Parent.Remove();
                            break;
                        }
                    }
                }
            }

            Console.WriteLine(jobject.ToString().Replace(",\r\n    {}", "").Replace(",\r\n      {}", ""));
            File.WriteAllText(jsonPath, jobject.ToString().Replace(",\r\n    {}", "").Replace(",\r\n      {}", ""));
        }
Esempio n. 3
0
        public static bool SetTabPageJsonValue(JObject jobject, string id, string key, string value, int isFirst)
        {
            try
            {
                JEnumerable <JToken> jToken = new JEnumerable <JToken>();
                string tabpageid            = "";
                if (isFirst == 0)
                {
                    jToken = jobject["Settings"]["tabControl_Main"].Children();
                }
                else
                {
                    jToken    = jobject["TabPages"].Children();
                    tabpageid = GetJsonValue(jToken, "tabpageid");
                }

                IEnumerator enumerator = jToken.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    JToken jc = (JToken)enumerator.Current;
                    if (jc is JObject || ((JProperty)jc).Value is JObject)
                    {
                        SetTabPageJsonValue((JObject)jc, id, key, value, 1);
                    }
                    else
                    {
                        if (tabpageid == id)
                        {
                            if (((JProperty)jc).Name == key)
                            {
                                ((JProperty)jc).Value = value;
                            }
                        }
                    }
                }

                Console.WriteLine(jobject.ToString().Replace(",\r\n      {}", ""));
                File.WriteAllText(jsonPath, jobject.ToString().Replace(",\r\n      {}", ""));
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("e.Message: " + e.Message + ";e.StackTrace: " + e.StackTrace);
                return(false);
            }
        }
Esempio n. 4
0
        public static bool SetListviewJson(JObject jobject, string id, string key, string value, int isFirst)
        {
            try
            {
                JEnumerable <JToken> jToken = new JEnumerable <JToken>();
                string apptitle             = "";
                if (isFirst == 0)
                {
                    jToken = jobject["AppListModel"].Children();
                }
                else
                {
                    jToken   = jobject["AppInfo"].Children();
                    apptitle = GetJsonValue(jToken, "apptitle");
                }

                IEnumerator enumerator = jToken.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    JToken jc = (JToken)enumerator.Current;
                    if (jc is JObject || ((JProperty)jc).Value is JObject)
                    {
                        SetListviewJson((JObject)jc, id, key, value, 1);
                    }
                    else
                    {
                        if (apptitle == id)
                        {
                            if (((JProperty)jc).Name == key)
                            {
                                ((JProperty)jc).Value = value;
                            }
                        }
                    }
                }

                Console.WriteLine(jobject.ToString());
                File.WriteAllText(jsonPath, jobject.ToString());
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("e.Message: " + e.Message + ";e.StackTrace: " + e.StackTrace);
                return(false);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 多层嵌套 string jsonData =
        /// "{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":\"guangzhou\",\"province\":\"guangdong\"}}";
        /// JObject jsonObj = JObject.Parse(jsonData);
        /// Response.Write(GetJsonValue(jsonObj.Children(), "province"));
        /// </summary>
        /// <param name="jToken"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static dynamic GetJsonValue(this JEnumerable <JToken> jToken, string key)
        {
            IEnumerator enumerator = jToken.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var jc = (JToken)enumerator.Current;
                if (jc is JObject || ((JProperty)jc).Value is JObject)
                {
                    return(GetJsonValue(jc.Children(), key));
                }

                if (((JProperty)jc).Name == key)
                {
                    return(((JProperty)jc).Value);
                }
            }

            return(null);
        }
Esempio n. 6
0
 public string GetJsonValue(JEnumerable <JToken> jToken, string key)
 {
     System.Collections.IEnumerator enumerator = jToken.GetEnumerator();
     while (enumerator.MoveNext())
     {
         JToken jc = (JToken)enumerator.Current;
         if (jc is JObject || ((JProperty)jc).Value is JObject)
         {
             return(GetJsonValue(jc.Children(), key));
         }
         else
         {
             if (((JProperty)jc).Name == key)
             {
                 return(((JProperty)jc).Value.ToString());
             }
         }
     }
     return(null);
 }
Esempio n. 7
0
        /// <summary>
        /// 迭代获取eky对应的值
        /// </summary>
        /// <param name="jToken"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public List <JsonResult> GetNestJsonValue(JEnumerable <JToken> jToken)
        {
            IEnumerator       enumerator  = jToken.GetEnumerator();
            List <JsonResult> jsonResults = new List <JsonResult>();

            while (enumerator.MoveNext())
            {
                JToken jc     = (JToken)enumerator.Current;
                var    jValue = (JObject)((JProperty)jc).Value;
                foreach (JProperty jProperty in jValue.Properties())
                {
                    string name    = jProperty.Name;
                    var    jPValue = jProperty.Value.ToString();
                    jsonResults.Add(new JsonResult()
                    {
                        Name  = name,
                        Vaule = jPValue
                    });
                }
            }
            return(jsonResults);
        }
Esempio n. 8
0
        private static void FillDictionaryFromJToken(Dictionary <string, object> dict, JToken token, string prefix)
        {
            JTokenType type      = token.Type;
            int        tokenType = (int)type;

            if (tokenType != 1)
            {
                if (tokenType == 2)
                {
                    int num = 0;
                    JEnumerable <JToken> jenumerable = token.Children();
                    using (IEnumerator <JToken> enumerator = jenumerable.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            JToken current = enumerator.Current;
                            FillDictionaryFromJToken(dict, current, Join(prefix, num.ToString()));
                            ++num;
                        }
                    }
                }
                else
                {
                    dict.Add(prefix, ((JValue)token).Value);
                }
            }
            else
            {
                JEnumerable <JProperty> jenumerable = token.Children <JProperty>();
                using (IEnumerator <JProperty> enumerator = jenumerable.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        JProperty current = enumerator.Current;
                        FillDictionaryFromJToken(dict, current.Value, Join(prefix, current.Name));
                    }
                }
            }
        }
Esempio n. 9
0
 public string GetJsonValue(JEnumerable<JToken> jToken, string key)
 {
     IEnumerator enumerator = jToken.GetEnumerator();
     while (enumerator.MoveNext())
     {
         JToken jc = (JToken)enumerator.Current;
         if (jc is JObject || ((JProperty)jc).Value is JObject)
         {
             return GetJsonValue(jc.Children(), key);
         }
         else
         {
             if (((JProperty)jc).Name == key)
             {
                 return ((JProperty)jc).Value.ToString();
             }
         }
     }
     return null;
 }