在编程的过程中,数据的传输格式如何,会影响开发的效率和后期代码的维护, 并且现在许多的js中支持了JSON格式的数据,
比如angular、nodejs。本篇文章主要讲解Array(数组)形式数据的解析。其它格式的数据会在后续的文章中进行书写。
(1)定义一个解析Array的类JSONArray。
public class JSONArray {
@SuppressWarnings("unchecked")
public static String toJSONString(List paramList){
//if param is null or "", return
if( null==paramList || 0==paramList.size() ){
return "null";
}
StringBuffer sb = new StringBuffer();
boolean isFirstEle = true;//list参数是不是首个参数
Iterator iterator = paramList.iterator();
sb.append("[");
while(iterator.hasNext()){
//check is first element
if(isFirstEle){
isFirstEle = false;
}else{
sb.append(",");
}
Object element = iterator.next();
if( null==element){
sb.append("null");
}else{
sb.append(JSONValue.toJSONString(element));
}
}
sb.append("]");
return sb.toString();
}
}
(2) 定义一个解析各种类型数据的类JSONValue。
package com.ityongman.base;
import java.util.List;
import java.util.Map;
public class JSONValue {
public static String toJSONString(Object paramObject) {
if( null==paramObject ){
return "null";
}
if( paramObject instanceof String ){
return "\"" + escape( (String)paramObject ) + "\"";
}
if( paramObject instanceof Double){
if( ((Double)paramObject).isInfinite() || ((Double)paramObject).isNaN() ){
return "null";
}
return paramObject.toString();
}
if( paramObject instanceof Float ){
if( ((Float)paramObject).isInfinite() || ((Float)paramObject).isNaN() ){
return "null";
}
return paramObject.toString();
}
if( paramObject instanceof Number ){
return paramObject.toString();
}
if( paramObject instanceof JSONAware ){
return ((JSONAware)paramObject).toJSONString();
}
if( paramObject instanceof Map){
return JSONMap.toString( (Map)paramObject );
}
if( paramObject instanceof List){
return JSONArray.toJSONString((List)(paramObject));
}
return paramObject.toString();
}
private static String escape(String paramString) {
if( null == paramString ){
return null;
}
StringBuffer sb = new StringBuffer();
escape(paramString,sb);
return sb.toString();
}
static void escape(String paramString, StringBuffer sb_param) {
// TODO Auto-generated method stub
for(int i=0;i<paramString.length();i++){
int asciindex = paramString.charAt(i);
switch(asciindex){
case 8: // \b
sb_param.append("\\b");
break;
case 9: // \t--水平制表符
sb_param.append("\\t");
break;
case 12: // \f
sb_param.append("\\f");
break;
case 10: // "
sb_param.append("\\n");
break;
case 13: // "
sb_param.append("\\r");
break;
case 34: // "--文件分隔符
sb_param.append("\\\"");
break;
case 47: // ‘--闭单引号
sb_param.append("\\/");
break;
case 92: // \--反斜线
sb_param.append("\\\\");
break;
default:
if (((asciindex >= 0) && (asciindex <= 31)) || ((asciindex >= 127) && (asciindex <= 159)) || ((asciindex >= 8192) && (asciindex <= 8447))){
String str = Integer.toHexString(asciindex);
sb_param.append("\\u");
for (int k = 0; k < 4 - str.length(); k++){
sb_param.append(‘0‘);
}
sb_param.append(str.toUpperCase());
}else{
sb_param.append(asciindex);
}
break;
}
}
}
}
(3)常见的数据类型有List, Map,String,Double, Float, Number; 上面的代码没有对Map进行处理,所以下面提供对Map的处理类JSONMap。
package com.ityongman.base;
import java.util.Iterator;
import java.util.Map;
public class JSONMap {
public static String toString(Map paramMap) {
// TODO
if( null==paramMap || 0==paramMap.size() ){
return "null";
}
StringBuffer sb = new StringBuffer();
boolean isFirstEle = true;
Iterator iterator = paramMap.entrySet().iterator();
sb.append("{");
while( iterator.hasNext() ){
if( isFirstEle ){
isFirstEle = false;
}else{
sb.append(",");
}
Map.Entry entity = (Map.Entry)iterator.next();
toJSONString(String.valueOf(entity.getKey()), entity.getValue(),sb);
}
sb.append("}");
return sb.toString();
}
private static String toJSONString(String paramString, Object paramValue,
StringBuffer sb_param) {
sb_param.append("\"");
if( null==paramString || 0==paramString.length() ){
sb_param.append("null");
}else{
JSONValue.escape(paramString,sb_param);
}
sb_param.append("\"").append(":");
sb_param.append(JSONValue.toJSONString(paramValue));
return sb_param.toString();
}
}
(4)上面涉及到一个JSONAware类,它是接口,很多其它类对实现(implements)它,下面是具体的代码。
package com.ityongman.base;
public abstract interface JSONAware {
public abstract String toJSONString();
}
总结:数据格式的转换是经常遇到的问题,尤其是JSON格式的数据,这会给我们带来许多的便利,所以提供者这个类的帮助类。