AddChildAttribute() public method

public AddChildAttribute ( JSONObject child, string Raw ) : void
child JSONObject
Raw string
return void
示例#1
0
        public void AddChildAttribute(JSONObject child, string Raw)
        {
            child.Parent = this;
            child.Raw = Raw;

            int split = Raw.IndexOf(':');
            if (split == -1) { return; }
            string[] items = new string[2];
            items[0] = Raw.Substring(0, split);
            items[1] = Raw.Substring(split + 1);
            child.Name = items[0].Replace("\"", "").Trim();

            if (items[1].Trim().StartsWith("\""))
            {
                child.ValueType = JSONType.String;
                string v = items[1].Replace("\\\"", "|||||");
                v = v.Replace("\"", "");
                v = v.Replace("|||||", "\\\"");
                child.Value = v.Trim();
            }
            else if (items[1].Trim().StartsWith("'"))
            {
                child.ValueType = JSONType.String;
                child.Value = items[1].Replace("'", "").Trim();
            }
            else if (items[1].Trim().CanConvert(typeof(System.Int32)))
            {
                child.ValueType = JSONType.Number;
                child.Value = Convert.ToInt32(items[1]);
            }
            else if (items[1].Trim().CanConvert(typeof(bool)))
            {
                child.ValueType = JSONType.Boolean;
                child.Value = Convert.ToBoolean(items[1].Trim());
            }
            else if (items[1].FirstVisibleCharacter() == '[' && items[1].LastVisibleCharacter() == ']')
            {
                items[1] = items[1].RemoveIfFirst('[').RemoveIfLast(']');
                char next = items[1].FirstVisibleCharacter();
                if (next == '{') //We are dealing with Objects here.
                {
                    child.ValueType = JSONType.ObjectArray;
                    child.Deserialize(items[1]);
                }
                else  // Raw text
                {
                    child.ValueType = JSONType.Raw;
                    child.Value = items[1];
                }
            }
            else if (items[1].FirstVisibleCharacter() == '{')
            {
                child.ValueType = JSONType.Object;
                items[1] = items[1].RemoveIfFirst('{').RemoveIfLast('}');
                List<JSONObject> props = SplitObjects(items[1]);
                foreach (JSONObject prop in props)
                {
                    child.AddChildAttribute(prop, prop.Raw);
                }
            }
            else
            {
                child.ValueType = JSONType.Raw;
                child.Value = items[1].Trim();
            }

            this.ChildObjects.Add(child);
        }