api文档工具-将json对象转行表
2017-04-25 13:51
330 查看
最近在学习写API文档。发现要对json进行解析说明,所以写了一个JSON转化为表的工具。直接上代码,没有什么好介绍的。哈哈
效果如下:
good luck
/** * 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 |
相关文章推荐
- fastJson API 最快的Json和对象转换工具
- Sandcastle - 生成.NET API文档的工具
- Sandcastle 生成.NET API文档的工具
- 告别手写 API文档生成工具推荐
- json工具性能比较:json-lib和jackson进行Java对象到json字符串序列化[转]
- 告别手写 API文档生成工具推荐
- 一个生成.net api文档的工具sandcastle
- 2013年8月20日、关键字this|操作数组(对数组工具对象建立,静态中使用,文档注释)|单例设计模式
- WebApi Serialize Entity Framework objects into JSON(webapi下将实体对象序列化为Json)
- json与对象转换的工具集合类
- GhostDoc:生成.NET API文档的工具 (帮忙文档)
- Flex AsDoc 文档API生成工具(方法2)
- TC官方文档翻译09----内存池对象API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- TC官方文档翻译06----Ordered Tree对象API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- 告别手写 API文档生成工具推荐
- Flex AsDoc 文档API生成工具 可以生成和Adobe的ActionScript3帮助一样的文档
- TC官方文档翻译05----Hash Map对象API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- Flex文档API生成工具AsDoc
- 告别手写 API文档生成工具推荐
- MongoDB mongoimport 工具 批量 导入 json 文档,小问题