/// <summary> /// 输出json /// </summary> /// <param name="type">编码类型</param> /// <returns>json字符串</returns> public string GetJSON(JSONEncodingType type) { if (data == null) { return string.Empty; } if (data is GObject) { return GObjectToJSON(type); } if (data is GObjectList) { return GObjectListToJSON(type); } return string.Empty; }
/// <summary> /// 输出数据对象的json字符串 /// </summary> /// <param name="type">加密类型</param> /// <returns>json字符串</returns> private string GObjectToJSON(JSONEncodingType type) { jsonContent = new StringBuilder("{"); GObject obj = this.data as GObject; foreach (string attribute in obj) { jsonContent.Append(string.Format("\"{0}\":{1},", attribute, JSONUtility.ValueToString(obj.Get(attribute), type))); } return jsonContent.Replace(',', '}', jsonContent.Length - 1, 1).ToString(); }
/// <summary> /// 输出数据对象集合的json字符串 /// </summary> /// <param name="type">编码类型</param> /// <returns>json字符串</returns> private string GObjectListToJSON(JSONEncodingType type) { jsonContent = new StringBuilder("["); GObjectList list = this.data as GObjectList; for (int i = 0; i < list.Count; i++) { jsonContent.Append(list[i].GetJSON(type)); jsonContent.Append(","); } jsonContent.Replace(',', ']', jsonContent.Length - 1, 1); return jsonContent.ToString(); }