Playtomic.JSON.JsonDecode C# (CSharp) Method

JsonDecode() public static method

Parses the string json into a value
public static JsonDecode ( string json ) : object
json string A JSON string.
return object
        public static object JsonDecode(string json)
        {
            // save the string for debug information
            JSON.instance.lastDecode = json;

            if (json != null) {
                char[] charArray = json.ToCharArray();
                int index = 0;
                bool success = true;
                object value = JSON.instance.ParseValue(charArray, ref index, ref success);
                if (success) {
                    JSON.instance.lastErrorIndex = -1;
                } else {
                    JSON.instance.lastErrorIndex = index;
                }
                return value;
            } else {
                return null;
            }
        }

Usage Example

Ejemplo n.º 1
0
        public static void GetResponse(string section, string action, Hashtable postdata, Action <PResponse> callback)
        {
            if (postdata == null)
            {
                postdata = new Hashtable();
            }
            else
            {
                postdata.Remove("publickey");
                postdata.Remove("section");
                postdata.Remove("action");
            }

            postdata.Add("publickey", PUBLICKEY);
            postdata.Add("section", section);
            postdata.Add("action", action);

            var json   = JSON.JsonEncode(postdata);
            var buffer = Encoding.UTF8.GetBytes("data=" + Encode.Base64(json) + "&hash=" + Encode.Md5(json + PRIVATEKEY));
            var task   = MakeAsyncRequest(buffer);

            if (string.IsNullOrEmpty(task.Result) || task.Exception != null || task.IsCanceled || task.IsFaulted)
            {
                callback(PResponse.GeneralError(1));
                return;
            }

            var results = (Hashtable)JSON.JsonDecode(task.Result);

            if (!results.ContainsKey("success") || !results.ContainsKey("errorcode"))
            {
                callback(PResponse.GeneralError(1));
                return;
            }

            callback(new PResponse
            {
                success   = (bool)results["success"],
                errorcode = (int)(double)results["errorcode"],
                json      = results
            });
        }