Ejemplo n.º 1
0
    /// <summary>
    /// Parses the string json into a value
    /// </summary>
    /// <param name="json">A JSON string.</param>
    /// <returns>An ArrayList, a Hashtable, a double, a string, null, true, or false</returns>
    public static object jsonDecode(string json)
    {
        // save the string for debug information
        MiniJSON2.lastDecode = json;

        if (json != null)
        {
            char[] charArray = json.ToCharArray();
            int    index     = 0;
            bool   success   = true;
            object value     = MiniJSON2.parseValue(charArray, ref index, ref success);

            if (success)
            {
                MiniJSON2.lastErrorIndex = -1;
            }
            else
            {
                MiniJSON2.lastErrorIndex = index;
            }

            return(value);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Converts a Hashtable / ArrayList / Dictionary(string,string) object into a JSON string
    /// </summary>
    /// <param name="json">A Hashtable / ArrayList</param>
    /// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
    public static string jsonEncode(object json)
    {
        var builder = new StringBuilder(BUILDER_CAPACITY);
        var success = MiniJSON2.serializeValue(json, builder);

        return(success ? builder.ToString() : null);
    }
Ejemplo n.º 3
0
    public override IEnumerator doReauest()
    {
        WWWForm wf = new WWWForm();

//        wf.AddField("userid", Game.UserMgr.PlayerId);
        wf.AddField("identifier", GetRandomIdentifier());
        wf.AddField("checksum", checkSum);
        wf.AddField("platform", GlobalConfig.GetPlatformId.ToString());
        wf.AddField("version", GlobalConfig.GetVersion);

        if (null != args)
        {
            foreach (string key in args.Keys)
            {
                wf.AddField(key, args[key]);
            }
        }

        WWW sdw = new WWW(GlobalConfig.URL_ROOT + uri, wf);

        yield return(sdw);

        if (sdw.error != null)
        {
            Debug.LogWarning("uri:" + uri + "; msg:" + sdw.error);
            if (null != OnError)
            {
                OnError(NetCode.Unknown, sdw.error);
            }

            yield break;
        }
        if (sdw.error == null)
        {
            try
            {
                string result = sdw.text;
                Debug.Log("Return Json:" + result);
                Hashtable sdd = MiniJSON2.jsonDecode(result) as Hashtable;

                int     code    = sdd["code"].GetJsonConverter().toInt();
                NetCode netCode = (NetCode)code;

                if (NetCode.Success == netCode)
                {
                    if (sdd.ContainsKey("data"))
                    {
                        object    ob   = sdd["data"];
                        Hashtable data = ob as Hashtable;
                        if (null != OnComplete)
                        {
                            OnComplete(data);
                        }
                    }
                    else
                    {
                        if (null != OnComplete)
                        {
                            OnComplete(sdd);
                        }
                    }
                }
                else
                {
                    string msg = "";
                    if (sdd.ContainsKey("msg"))
                    {
                        msg = sdd["msg"].GetJsonConverter().toStr();
                    }

                    Debug.LogWarning("Return Error:" + netCode.ToString() + "; msg:" + msg);
                    if (null != OnError)
                    {
                        OnError(netCode, msg);
                    }
                }
            }
            catch (System.Exception e)
            {
                Debug.LogWarning("Return Error:" + NetCode.JsonError.ToString() + "; msg:" + e.Message);
                if (null != OnError)
                {
                    OnError(NetCode.JsonError, e.Message);
                }
            }
        }
    }
Ejemplo n.º 4
0
    public override IEnumerator doReauest()
    {
        bool hasLoadingPage = Game.UIMgr.IsSceneActive(UIPage.LoadingPage);

        if (useMask && !hasLoadingPage)
        {
            Game.LoadingPage.Show(LoadPageType.OnlyMask);
        }

        string url = GlobalConfig.URL_ROOT + uri;

        if (args == null)
        {
            args = new Dictionary <string, string>();
        }

        CheckCommonParam();

        WWWForm wwwform = getForm(url);

        WWW www = new WWW(url, wwwform.data, GetHeader(wwwform));

        yield return(www);

        while (!www.isDone)
        {
            yield return(0);
        }

        if (www.error == null)
        {
            NetCode   netCode = NetCode.Unknown;
            Hashtable sdd     = null;
            string    msg     = "";
            try
            {
                string jsons = www.text;
                // 解密
#if UNITY_IOS || UNITY_IPHONE
                Debug.Log(url + ": net jsons == " + jsons);
#elif UNITY_ANDROID
                jsons = GetJsonFormXXTea(jsons, url);
                Debug.Log(url + ": net jsons == " + jsons);
#endif

                sdd = MiniJSON2.jsonDecode(jsons) as Hashtable;

                int code = sdd["error_code"].GetJsonConverter().toInt();
                netCode = (NetCode)code;
            }
            catch (System.Exception e)
            {
                netCode = NetCode.JsonError;
                msg     = e.Message;
            }

            if (NetCode.Success == netCode)
            {
                Hashtable data = null;
                if (sdd.ContainsKey("data"))
                {
                    object ob = sdd["data"];
                    data = ob as Hashtable;
                }

                if (null != OnComplete)
                {
                    OnComplete(data);
                }
            }
            else
            {
                if (netCode != NetCode.JsonError && null != sdd && sdd.ContainsKey("msg"))
                {
                    msg = sdd["msg"].GetJsonConverter().toStr();
                }

                Debug.LogWarning("Return Error:" + netCode.ToString() + "; msg:" + msg);

                if (null != OnError)
                {
                    OnError(netCode, msg);
                }
            }

            if (useMask && !hasLoadingPage)
            {
                Game.LoadingPage.Hide();
            }
        }
    }