JSON 是一種方便的serialization format. 相對於XML, 其容量相對較小, 更適合於network 中傳輸. 當要將dictionary serialize 時, 方法如下:
public static class JsonUtility { public static Dictionary<string, object> NvcToDictionary(NameValueCollection nvc, bool handleMultipleValuesPerKey) { var result = new Dictionary<string, object>(); foreach (string key in nvc.Keys) { if (handleMultipleValuesPerKey) { string[] values = nvc.GetValues(key); if (values.Length == 1) { result.Add(key, values[0]); } else { result.Add(key, values); } } else { result.Add(key, nvc[key]); } } return result; } }
Leave a Reply