Ejemplo n.º 1
0
        public static string ReadTag(string s, string tag)
        {
            var startIndex = s.IndexOfAny(new[] { "\"" + tag + "\"", "'" + tag + "'" }, StringComparison.Ordinal);

            if (startIndex < 0)
            {
                return(null);
            }
            var res = s.Substring(startIndex + 3 + tag.Length).Trim().TrimStart(':').TrimStart();

            if (res.StartsWith('"'))
            { // text
                res = Json.ConvertJsonSpecialCharacters(res);
                res = res.Replace("\\\"", "@__1");
                int endIndex     = res.IndexOf("\"}", StringComparison.Ordinal);
                int endAlternate = res.IndexOf("\",", StringComparison.Ordinal);
                if (endIndex < 0)
                {
                    endIndex = endAlternate;
                }
                else if (endAlternate > 0 && endAlternate < endIndex)
                {
                    endIndex = endAlternate;
                }
                if (endIndex < 0)
                {
                    return(null);
                }
                if (res.Length > 1)
                {
                    return(res.Substring(1, endIndex - 1).Replace("@__1", "\\\""));
                }
                return(string.Empty);
            }
            else
            { // number
                var endIndex = res.IndexOfAny(new[] { ',', '}' });
                if (endIndex < 0)
                {
                    return(null);
                }
                return(res.Substring(0, endIndex));
            }
        }