Example #1
0
 /// <summary> Construct a JSONObject from a subset of another JSONObject.
 /// An array of strings is used to identify the keys that should be copied.
 /// Missing keys are ignored.
 /// </summary>
 /// <param name="jo">A JSONObject.
 /// </param>
 /// <param name="names">An array of strings.
 /// </param>
 /// <throws>  JSONException  </throws>
 /// <exception cref="JSONException">If a value is a non-finite number or if a name is duplicated.
 /// </exception>
 public JSONObject(JSONObject jo, System.String[] names)
     : this()
 {
     for (int i = 0; i < names.Length; i += 1)
     {
         try
         {
             putOnce(names[i], jo.opt(names[i]));
         }
         catch (System.Exception)
         {
         }
     }
 }
Example #2
0
 /// <summary> Get an array of field names from a JSONObject.
 /// 
 /// </summary>
 /// <returns> An array of field names, or null if there are no names.
 /// </returns>
 public static System.String[] getNames(JSONObject jo)
 {
     int length = jo.length();
     if (length == 0)
     {
         return null;
     }
     System.Collections.IEnumerator i = jo.keys();
     System.String[] names = new System.String[length];
     int j = 0;
     while (i.MoveNext())
     {
         names[j] = ((System.String)i.Current);
         j += 1;
     }
     return names;
 }
        public JSONObject getJSONObject(string name)
        {
            #if VERBOSE_LOGGING
            Debug.Log(MethodBase.GetCurrentMethod().Name);
            #endif
            JNIFind();
            if (_jcJsonObject == IntPtr.Zero)
            {
                Debug.LogError("_jcJsonObject is not initialized");
                return null;
            }
            if (_jmGetJsonObject == IntPtr.Zero)
            {
                Debug.LogError("_jmGetJsonObject is not initialized");
                return null;
            }

            IntPtr arg1 = AndroidJNI.NewStringUTF(name);
            IntPtr result = AndroidJNI.CallObjectMethod(_instance, _jmGetJsonObject, new jvalue[] { new jvalue() { l = arg1 } });
            AndroidJNI.DeleteLocalRef(arg1);

            if (result == IntPtr.Zero)
            {
                Debug.LogError("Failed to get JSONObject");
                return null;
            }

            JSONObject retVal = new JSONObject(result);
            return retVal;
        }
Example #4
0
        public org.json.JSONObject getJSONObject(int index)
        {
#if VERBOSE_LOGGING
            Debug.Log(MethodBase.GetCurrentMethod().Name);
#endif
            JNIFind();
            if (_jcJsonArray == IntPtr.Zero)
            {
                Debug.LogError("_jcJsonObject is not initialized");
                return null;
            }
            if (_jmGetJsonObject == IntPtr.Zero)
            {
                Debug.LogError("_jmGetJsonObject is not initialized");
                return null;
            }

            int arg1 = index;
            IntPtr result = AndroidJNI.CallObjectMethod(_instance, _jmGetJsonObject, new jvalue[] { new jvalue() { i = arg1 } });
            if (result == IntPtr.Zero)
            {
                Debug.LogError("Failed to get JSONObject");
                return null;
            }

            org.json.JSONObject retVal = new JSONObject(result);
            return retVal;
        }
Example #5
0
        public JSONObject put(string name, string value)
        {
            #if VERBOSE_LOGGING
            Debug.Log(MethodBase.GetCurrentMethod().Name);
            #endif
            JNIFind();
            if (_jcJsonObject == IntPtr.Zero)
            {
                Debug.LogError("_jcJsonObject is not initialized");
                return null;
            }
            if (_jmPut == IntPtr.Zero)
            {
                Debug.LogError("_jmPut is not initialized");
                return null;
            }

            IntPtr arg1 = AndroidJNI.NewStringUTF(name);
            IntPtr arg2 = AndroidJNI.NewStringUTF(value);
            IntPtr retVal = AndroidJNI.CallObjectMethod(_instance, _jmPut, new jvalue[] { new jvalue() { l = arg1 }, new jvalue() { l = arg2 } });
            AndroidJNI.DeleteLocalRef(arg1);
            AndroidJNI.DeleteLocalRef(arg2);

            if (retVal == IntPtr.Zero)
            {
                Debug.LogError("Put returned null object");
                return null;
            }

            JSONObject jsonObject = new JSONObject(retVal);

            return jsonObject;
        }
Example #6
0
 /// <summary> Append an object value.</summary>
 /// <param name="o">The object to append. It can be null, or a Boolean, Number,
 /// String, JSONObject, or JSONArray, or an object with a toJSONString()
 /// method.
 /// </param>
 /// <returns> this
 /// </returns>
 /// <throws>  JSONException If the value is out of sequence. </throws>
 public virtual JSONWriter value_Renamed(System.Object o)
 {
     return(this.append(JSONObject.valueToString(o)));
 }
Example #7
0
 /// <summary> Push an array or object scope.</summary>
 /// <param name="jo">The scope to open.
 /// </param>
 /// <throws>  JSONException If nesting is too deep. </throws>
 private void push(JSONObject jo)
 {
     if (this.top >= maxdepth)
     {
         throw new JSONException("Nesting too deep.");
     }
     this.stack[this.top] = jo;
     this.mode = jo == null?'a':'k';
     this.top += 1;
 }
Example #8
0
        public org.json.JSONArray put(int index, JSONObject jsonObject)
        {
            #if VERBOSE_LOGGING
            Debug.Log(MethodBase.GetCurrentMethod().Name);
            #endif
            JNIFind();
            if (_jcJsonArray == IntPtr.Zero)
            {
                Debug.LogError("_jcJsonObject is not initialized");
                return null;
            }
            if (_jmPut == IntPtr.Zero)
            {
                Debug.LogError("_jmPut is not initialized");
                return null;
            }

            if (null == jsonObject)
            {
                Debug.LogError("jsonObject is null");
                return null;
            }

            if (jsonObject.GetInstance() == IntPtr.Zero)
            {
                Debug.LogError("jsonObject instance is not initialized");
                return null;
            }

            int arg1 = index;
            IntPtr arg2 = jsonObject.GetInstance();
            IntPtr result = AndroidJNI.CallObjectMethod(_instance, _jmPut, new jvalue[] { new jvalue() { i = arg1 }, new jvalue() { l = arg2 } });

            return this;
        }
Example #9
0
 /// <summary> Produce a JSONObject by combining a JSONArray of names with the values
 /// of this JSONArray.
 /// </summary>
 /// <param name="names">A JSONArray containing a list of key strings. These will be
 /// paired with the values.
 /// </param>
 /// <returns> A JSONObject, or null if there are no names or if this JSONArray
 /// has no values.
 /// </returns>
 /// <throws>  JSONException If any of the names are null. </throws>
 public virtual JSONObject toJSONObject(JSONArray names)
 {
     if (names == null || names.length() == 0 || length() == 0)
     {
         return null;
     }
     JSONObject jo = new JSONObject();
     for (int i = 0; i < names.length(); i += 1)
     {
         jo.put(names.getString(i), this.opt(i));
     }
     return jo;
 }