/// <summary>
/// 序列化JSONObject对象
/// </summary>
/// <param name="jsonObject"></param>
/// <returns></returns>
public static string SerializeObject(JSONObject jsonObject)
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
foreach (KeyValuePair<string, object> kvp in jsonObject)
{
if (kvp.Value is JSONObject)
{
sb.Append(string.Format("/"{0}/":{1},", kvp.Key, SerializeObject((JSONObject)kvp.Value)));
}
else if (kvp.Value is JSONArray)
{
sb.Append(string.Format("/"{0}/":{1},", kvp.Key, SerializeArray((JSONArray)kvp.Value)));
}
else if (kvp.Value is String)
{
sb.Append(string.Format("/"{0}/":/"{1}/",", kvp.Key, kvp.Value));
}
else
{
sb.Append(string.Format("/"{0}/":/"{1}/",", kvp.Key, ""));
}
}
if (sb.Length > 1)
sb.Remove(sb.Length - 1, 1);
sb.Append("}");
return sb.ToString();
}
/// <summary>
/// 序列化JSONArray对象
/// </summary>
/// <param name="jsonArray"></param>
/// <returns></returns>
public static string SerializeArray(JSONArray jsonArray)
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i = 0; i < jsonArray.Count; i++)
{
if (jsonArray[i] is JSONObject)
{
sb.Append(string.Format("{0},", SerializeObject((JSONObject)jsonArray[i])));
}
else if (jsonArray[i] is JSONArray)
{
sb.Append(string.Format("{0},", SerializeArray((JSONArray)jsonArray[i])));
}
else if (jsonArray[i] is String)
{
sb.Append(string.Format("/"{0}/",", jsonArray[i]));
}
else
{
sb.Append(string.Format("/"{0}/",", ""));
}
}
if (sb.Length > 1)
sb.Remove(sb.Length - 1, 1);
sb.Append("]");
return sb.ToString();
}
#endregion
}
以上是在C#中对JSONObject和JSONArray 的序列化(原文见http://www.codesky.net/article/doc/201004/20100417061035.htm),是不是比在Java中简单多了!但Java程序员也想用这种方法,该怎么办,笔者做了一些尝试,现在把代码贴出,请大家不吝指正!!
/**
* 序列化JsonObject
* @param jsonObject
* @return
*/
public static String SerializeObject(JSONObject jsonObject){
Iterator<String> it=jsonObject.keys();
Collection<Object> col=jsonObject.values();
Iterator<Object> colit=col.iterator();
Map<String,Object> map=new HashMap<String,Object>();
//这里的键值是一一对应的
int csize=col.size();
if(csize>0){
//把jsonObject封装成HashMap
while(it.hasNext()){
String tmpKey=it.next();
Object tmpValue=colit.next();
map.put(tmpKey, tmpValue);
}
}else{
return "";
}
StringBuilder sb = new StringBuilder();
sb.append("{");
for(Map.Entry<String, Object> entry:map.entrySet()){
System.out.println(entry.getValue().getClass().toString());
//判断是否是JsonObject
if(entry.getValue().getClass().toString().equals(JSONObject.class.toString())){
sb.append(String.format("/"{0}/":{1},", entry.getKey(), SerializeObject((JSONObject)entry.getValue())));
//判断是否是JsonArray
}else if(entry.getValue().getClass().toString().equals(JSONArray.class.toString())){
sb.append(String.format("/"{0}/":{1},", entry.getKey(), SerializeArray((JSONArray)entry.getValue())));
//判断是否是String
}else if(entry.getValue().getClass().toString().equals(String.class.toString())){
sb.append(String.format("/"{0}/":/"{1}/",", entry.getKey(), entry.getValue()));
}else{
sb.append(String.format("/"{0}/":/"{1}/",", entry.getKey(), ""));
}
}
if (sb.length() > 1)
sb.deleteCharAt(sb.length() - 1);
sb.append("}");
return sb.toString();
}
/**
* 序列化JsonArray
* @param jsonArray
* @return
*/
public static String SerializeArray(JSONArray jsonArray){
StringBuilder sb = new StringBuilder();
sb.append("[");
int size=jsonArray.size();
for (int i = 0; i < size; i++){
if(jsonArray.get(i).getClass().toString().equals(JSONObject.class.toString())){
sb.append(String.format("{0},", SerializeObject((JSONObject)jsonArray.get(i))));
}else if(jsonArray.get(i).getClass().toString().equals(JSONArray.class.toString())){
sb.append(String.format("{0},", SerializeArray((JSONArray)jsonArray.get(i))));
}else if(jsonArray.get(i).getClass().equals(String.class.toString())){
sb.append(String.format("/"{0}/",", jsonArray.get(i)));
}else{
sb.append(String.format("/"{0}/",", ""));
}
}
if (sb.length() > 1)
sb.deleteCharAt(sb.length()- 1);
sb.append("]");
return sb.toString();
}