1.1.15 • Published 2 years ago

waft-json v1.1.15

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

Install

npm install waft-json

Usage

JSON.parse string -> JSON.JSONObject

const jsonStr = '{"姓名":"小王","年龄":18,"身高":178.2}'
const obj2 = JSON.parseObject(jsonStr);
log('----> jsonStr parse:' + obj2.toString())
if(!obj2.has('体重')){
  log('----> 体重不存在')
}
const name = obj2.getString('姓名').toString();
const age = obj2.getInteger('年龄').intValue();
const height = obj2.getFloat('身高').floatValue();
log('----> jsonStr parse split:' + name + ' ' + age.toString() + ' ' + height.toString());

JSON.JSONArray -> string

const arr = new JSONArray();
arr.push(obj);
arr.push('一串普通的字符串');
arr.push(3.2);
arr.push(3);
log('----> arr to string:' + arr.toString())

JSON.JSONObject -> string

const obj = new JSONObject();
obj.set('name', '精灵');
obj.set('desc', '一串描述');
obj.set('age', 3);
obj.set('height', 155.3);

log('----> obj to string:' + obj.toString())
// 放字符串
const case1 = ["123", "456"];
expect(JSON.stringify(case1) == '["123","456"]');
// 放数字
const case2 = [123, 456];
expect(JSON.stringify(case2) == '[123,456]');
// 放对象
const stu:Student = {name:'小明'};
const stus = [stu];
expect(JSON.stringify(stus) == '[{}]');
// 放JSONValue
const obj =  new JSONObject();
obj.set("name","小明");
expect(JSON.stringify(obj) == '{"name":"小明"}');
// Array放JSONValue
const arr = [obj];
expect(JSON.stringify(arr) == '[{"name":"小明"}]');
// JSONArray放JSONValue
const json_arr = new JSONArray();
json_arr.push(obj);
expect(JSON.stringify(json_arr) == '[{"name":"小明"}]');