Java中仿C#中KeyValuePair序列化jsonObject

/// <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();
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值