api文档工具-将json对象转行表
来源:互联网 发布:自学网站美工 编辑:程序博客网 时间:2024/06/09 14:44
最近在学习写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); }}
效果如下:
good luck
0 0
- api文档工具-将json对象转行表
- 将list对象转化为json数据存入文档
- fastJson API 最快的Json和对象转换工具
- 将对象转变成JSON
- 将json对象排序
- Json对象工具类
- 之前写的一个XML工具类,包括将JSON对象转换为XML对象
- 使用第三方工具,将JavaBean对象/List或Set或Map对象转成JSON
- php API文档生成工具
- RESTFul API文档生成工具
- API文档自动生成工具
- javadoc工具文档API生成
- API文档生成工具推荐
- RESTful API文档生成工具
- API文档管理系统,文档生成工具
- c#.net如何将API返回json对象转换成自己想要的List<T>
- JavaScript对象的API文档
- 将字符串转成json对象
- Oracle trunc()函数的用法
- 计算机书籍汇总
- 在云服务器上从零搭建nginx+uwsgi+django服务器
- spring mvc 返回json乱码
- C语言中#define使用方法
- api文档工具-将json对象转行表
- 归并排序之数组和链表
- 微信小程序开发常见问题FAQ之七
- calcHist的使用中严重的错误和误区!!!!!!!为什么所有的文章都没有指出??
- 秒杀系统架构分析与实战
- input 输入法禁用
- 1、Activiti 创建表
- Java内存模型及GC原理
- led子系统实现led驱动