コード例 #1
0
        private static string format(SimpleJson.JsonObject json, string strTab = "", bool bAddComma = false)
        {
            string strOut            = strTab + "{" + Environment.NewLine;
            string strOriginalTab    = strTab;
            bool   bOriginalAddComma = bAddComma;

            strTab += "  ";

            KeyValuePair <string, object>[] rgItems = json.ToArray();

            for (int i = 0; i < rgItems.Length; i++)
            {
                string strKey = rgItems[i].Key;
                SimpleJson.JsonArray children = rgItems[i].Value as SimpleJson.JsonArray;

                strOut += strTab + "\"" + strKey + "\":";

                if (children != null)
                {
                    if (children.Count > 1)
                    {
                        strOut += "[";
                    }

                    strOut += Environment.NewLine;

                    bAddComma = true;
                    int nIdx = 0;
                    foreach (object objChild in children)
                    {
                        if (nIdx >= children.Count - 1)
                        {
                            bAddComma = false;
                        }

                        SimpleJson.JsonObject child = objChild as SimpleJson.JsonObject;
                        if (child != null)
                        {
                            strOut += format(child, strTab + "  ", bAddComma);
                        }
                        else
                        {
                            strOut += strTab + "  ";

                            if (objChild == null)
                            {
                                strOut += "null";
                            }
                            else
                            {
                                strOut += "\"" + objChild.ToString() + "\"";
                            }

                            strOut += (bAddComma ? "," : "") + Environment.NewLine;
                        }
                        nIdx++;
                    }

                    if (children.Count > 1)
                    {
                        strOut += strTab + "]" + Environment.NewLine;
                    }
                }
                else
                {
                    SimpleJson.JsonObject child = rgItems[i].Value as SimpleJson.JsonObject;
                    if (child != null)
                    {
                        strOut += format(child, strTab + "  ", bAddComma);
                    }
                    else
                    {
                        if (rgItems[i].Value == null)
                        {
                            strOut += "null";
                        }
                        else
                        {
                            strOut += "\"" + rgItems[i].Value.ToString() + "\"";
                        }

                        if (bAddComma)
                        {
                            strOut += ",";
                        }

                        strOut += Environment.NewLine;
                    }
                }
            }

            strOut += strOriginalTab + "}" + (bOriginalAddComma ? "," : "") + Environment.NewLine;
            return(strOut);
        }
コード例 #2
0
        private static void extract(SimpleJson.JsonObject json, JsonData root)
        {
            KeyValuePair <string, object>[] rgItems = json.ToArray();

            for (int i = 0; i < rgItems.Length; i++)
            {
                string strKey = rgItems[i].Key;
                SimpleJson.JsonArray children = rgItems[i].Value as SimpleJson.JsonArray;

                JsonData data = new JsonData(strKey);

                if (children != null)
                {
                    int nIdx = 0;
                    foreach (object objChild in children)
                    {
                        JsonData child1 = new JsonData(nIdx.ToString());

                        SimpleJson.JsonObject child = objChild as SimpleJson.JsonObject;
                        if (child != null)
                        {
                            extract(child, child1);
                        }
                        else
                        {
                            if (objChild == null)
                            {
                                data.Values.Add("null");
                            }
                            else
                            {
                                data.Values.Add(objChild.ToString());
                            }
                        }
                        nIdx++;

                        data.Children.Add(child1);
                    }
                }
                else
                {
                    SimpleJson.JsonObject child = rgItems[i].Value as SimpleJson.JsonObject;

                    if (child != null)
                    {
                        extract(child, data);
                    }
                    else
                    {
                        if (rgItems[i].Value == null)
                        {
                            data.Values.Add("null");
                        }
                        else
                        {
                            data.Values.Add(rgItems[i].Value.ToString());
                        }
                    }
                }

                root.Children.Add(data);
            }
        }