Line用法 · dmo产品开发文档 · 看云
### [Line用法]
~~~
Line继承了HashMap,同时封装了JSON功能
//常用泛型:
Line<String, String> //key用string,value用string,只能保存string类型的值
Line<String, Object> //key用string,value用Object,可以放置各种不同类型的值
Line<String, Integer> //key用string,value用Integer,只能保存整数值
//构造方法
//构造默认空的Line
Line<String, Object> line = new Line<String, Object>();
//从JSON字符串构造Line
String json = "{id:1, name:'xxx', age: 123}";
Line<String, Object> line = new Line<String, Object>(json);
//从Map构造Line
Map<String, Object> map = new HashMap<String, Object>();
...
Line<String, Object> line = new Line<String, Object>(map);
//从K,V数据对构造Line
String k1 = "id";
Integer v1 = 1;
String k2 = "name";
String v2 = "xxx";
String k3 = "age";
Integer v3 = 123;
Line<String, Object> line = new Line<String, Object>(k1,v1,k2,v2,k3,v3);
//Line中元素的数量
int size = line.size();
//Line写入
Line<String, Object> line = new Line<String, Object>();
//允许value是null,但不保存
line.put("key", value);
//把v1,v2转换为List(v1, v2), 结果是:{key: List(v1,v2)}
line.put("key", v1, v2);
//把v1...v3...转换为List, 如果只有v1一个,则保存为值value
line.put("key", v1, v2, v3, ...);
//如果key是String,则把key强制转换为小写
line.putIgnoreKeyCase("key", value);
//读取key对应的value
Line<String, Object> line = new Line<String, Object>();
Object v1 = line.get("key1");
String v2 = (String)line.get("key2");
Integer v3 = (Integer)line.get("key3");
//强制key为小写
String v4 = (String)line.getIgnoreKeyCase("key4");
//有默认值的
String v5 = (String)line.get("key5", "defaultValue");
//检查Line中是否存在指定的key
if (line.has("key")) {
...
}
//返回全部key的name数组
String[] keys = line.getNames();
//在List<Line>中对Line进行排序, 此处指定key排序的次序,具体排序参见List参见List用法
line.setSortBy("name, age, id desc");
//把Line转换为JSON字符串
String json = line.toJSON();
//把Line转换为字符串
String str = line.toString();
//把Line转换为HTTP协议的querystring格式
String query = line.toQueryString();
//默认情况下,在toJSON, toString, toQueryString中,把string类型的值自动转换为数字等其他类型
//例如,自动把字符串 "123" 转换为数字 123
//也可以关闭这种转换
line.setAutoTransValue(false);
~~~