/// <summary>Produce a comma delimited text row from a JSONArray.</summary> /// <remarks> /// Produce a comma delimited text row from a JSONArray. Values containing /// the comma character will be quoted. Troublesome characters may be /// removed. /// </remarks> /// <param name="ja">A JSONArray of strings.</param> /// <returns>A string ending in NEWLINE.</returns> public static string RowToString(org.json.JSONArray ja) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < ja.Length(); i += 1) { if (i > 0) { sb.Append(','); } object @object = ja.Opt(i); if (@object != null) { string @string = @object.ToString(); if (@string.Length > 0 && (@string.IndexOf(',') >= 0 || @string.IndexOf('\n') >= 0 || @string.IndexOf('\r') >= 0 || @string.IndexOf(0) >= 0 || @string[0] == '"')) { sb.Append('"'); int length = @string.Length; for (int j = 0; j < length; j += 1) { char c = @string[j]; if (c >= ' ' && c != '"') { sb.Append(c); } } sb.Append('"'); } else { sb.Append(@string); } } } sb.Append('\n'); return sb.ToString(); }
/// <summary>Reverse the JSONML transformation, making an XML text from a JSONArray.</summary> /// <param name="ja">A JSONArray.</param> /// <returns>An XML string.</returns> /// <exception cref="JSONException"/> /// <exception cref="org.json.JSONException"/> public static string ToString(org.json.JSONArray ja) { int i; org.json.JSONObject jo; string key; System.Collections.Generic.IEnumerator<string> keys; int length; object @object; System.Text.StringBuilder sb = new System.Text.StringBuilder(); string tagName; string value; // Emit <tagName tagName = ja.GetString(0); org.json.XML.NoSpace(tagName); tagName = org.json.XML.Escape(tagName); sb.Append('<'); sb.Append(tagName); @object = ja.Opt(1); if (@object is org.json.JSONObject) { i = 2; jo = (org.json.JSONObject)@object; // Emit the attributes keys = jo.Keys(); while (keys.HasNext()) { key = keys.Next(); org.json.XML.NoSpace(key); value = jo.OptString(key); if (value != null) { sb.Append(' '); sb.Append(org.json.XML.Escape(key)); sb.Append('='); sb.Append('"'); sb.Append(org.json.XML.Escape(value)); sb.Append('"'); } } } else { i = 1; } // Emit content in body length = ja.Length(); if (i >= length) { sb.Append('/'); sb.Append('>'); } else { sb.Append('>'); do { @object = ja.Get(i); i += 1; if (@object != null) { if (@object is string) { sb.Append(org.json.XML.Escape(@object.ToString())); } else { if (@object is org.json.JSONObject) { sb.Append(ToString((org.json.JSONObject)@object)); } else { if (@object is org.json.JSONArray) { sb.Append(ToString((org.json.JSONArray)@object)); } else { sb.Append(@object.ToString()); } } } } } while (i < length); sb.Append('<'); sb.Append('/'); sb.Append(tagName); sb.Append('>'); } return sb.ToString(); }
/// <summary>Construct a JSONObject from a subset of another JSONObject.</summary> /// <remarks> /// 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. /// </remarks> /// <param name="jo">A JSONObject.</param> /// <param name="names">An array of strings.</param> /// <exception cref="JSONException"/> /// <exception> /// JSONException /// If a value is a non-finite number or if a name is /// duplicated. /// </exception> public JSONObject(org.json.JSONObject jo, string[] names) : this() { for (int i = 0; i < names.Length; i += 1) { try { this.PutOnce(names[i], jo.Opt(names[i])); } catch (System.Exception) { } } }