最近在学习写API文档。发现要对json进行解析说明,所以写了一个JSON转化为表的工具。直接上代码,没有什么好介绍的。哈哈
/**
* Created by OlaWang on 2017/4/21.
* 将json对象转化为表格
*/
public class JSONToForm {
public final static String form_head = "|名称|类型|描述|";
public final static String form_broad = "|-----|-----|-----|";
public final static String form_line = "|{0}|{1}|{2}|";
public static void main(String[] args) {
// String source = "{\n" +
// " \"api\": \"tuya.m.user.uid.password.login\",\n" +
// " \"result\": {\n" +
// " \"ecode\": \"000561001B400640\",\n" +
// " \"email\": \"\",\n" +
// " \"headPic\": \"\",\n" +
// " \"mobile\": \"\",\n" +
// " \"nickname\": \"\",\n" +
// " \"partnerIdentity\": \"p1000000\",\n" +
// " \"phoneCode\": \"86\",\n" +
// " \"sex\": 0,\n" +
// " \"sid\": \"00145611B8300530Y6th0rc38a78d3b0407463460b38fdefccbfd2d4\",\n" +
// " \"snsNickname\": \"\",\n" +
// " \"uid\": \"001456118300530Yth0r\",\n" +
// " \"userType\": 1,\n" +
// " \"username\": \"c0000jux88\",\n" +
// " \"domain\": {\n" +
// " \"gwApiUrl\": \"http://a.gw.tuyacn.com/gw.json\",\n" +
// " \"gwMqttUrl\": \"mq.gw.tuyacn.com\",\n" +
// " \"mobileApiUrl\": \"https://a1.tuyacn.com\",\n" +
// " \"mobileMqttUrl\": \"mq.mb.tuyacn.com\",\n" +
// " \"regionCode\": \"AY\"\n" +
// " }\n" +
// " },\n" +
// " \"status\": \"ok\",\n" +
// " \"success\": true\n" +
// "}";
// String source="{\n" +
// " \"countryCode\": \"86\",\n" +
// " \"userAccount\":\"xx@qq.com\",\n" +
// " \"devIds\":\"[\\\"002000315ccf7f1b5392\\\"]\"\n" +
// "}\n";
String source = "{\n" +
" \"name\": \"BeJson\",\n" +
" \"url\": \"http://www.bejson.com\",\n" +
" \"page\": 88,\n" +
" \"isNonProfit\": true,\n" +
" \"address\": {\n" +
" \"street\": \"科技园路.\",\n" +
" \"city\": \"江苏苏州\",\n" +
" \"country\": \"中国\"\n" +
" },\n" +
" \"links\": [\n" +
" {\n" +
" \"name\": \"Google\",\n" +
" \"url\": \"http://www.google.com\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Baidu\",\n" +
" \"url\": \"http://www.baidu.com\"\n" +
" },\n" +
" {\n" +
" \"name\": \"SoSo\",\n" +
" \"url\": \"http://www.SoSo.com\"\n" +
" }\n" +
" ]\n" +
"}";
System.out.println(form_head);
System.out.println(form_broad);
jsonToForm("", source, false);
}
/**
* 将json转行为表单
*
* @param belong 上级的名字
* @param jsonSource 对象的字符
* @param jsonArray 字符是否为数组类型
*/
public static void jsonToForm(String belong, String jsonSource, boolean jsonArray) {
try {
JSONObject json = null;
if (jsonArray) {
JSONArray jsArray = JSON.parseArray(jsonSource);
if (jsArray.size() == 0) {
return;
}
json = (JSONObject) jsArray.get(0);
} else {
json = JSON.parseObject(jsonSource);
}
Iterator<String> keys = json.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
//1.String;2.JSONObject;3.JSONArray;4.boolean;5.int
Object value = json.get(key);
if (value instanceof String) {
format(belong, key, "String");
} else if (value instanceof JSONObject) {
format(belong, key, "JSONObject");
jsonToForm(key, ((JSONObject) value).toJSONString(), false);
} else if (value instanceof JSONArray) {
format(belong, key, "JSONArray");
jsonToForm(key, ((JSONArray) value).toJSONString(), true);
} else if (value instanceof Boolean) {
format(belong, key, "boolean");
} else if (value instanceof Integer) {
format(belong, key, "int");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 打印出一条数据
*
* @param belong 属性归属
* @param key 名字
* @param type 类型
*/
private static void format(String belong, String key, String type) {
String f = form_line;
if (belong.length() == 0) {
f = f.replace("{0}", key);
} else {
f = f.replace("{0}", belong + "." + key);
}
f = f.replace("{1}", type);
f = f.replace("{2}", "");
System.out.println(f);
}
}
效果如下:
名称 | 类型 | 描述 |
---|---|---|
address | JSONObject | |
address.country | String | |
address.city | String | |
address.street | String | |
isNonProfit | boolean | |
name | String | |
links | JSONArray | |
links.name | String | |
links.url | String | |
page | int | |
url | String |
good luck