热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

控制台打印Json出来

2019独角兽企业重金招聘Python工程师标准packagecom.qxzl.util;importorg.json.JSONArray;importorg.json.J

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.qxzl.util;import org.json.JSONArray;
import org.json.JSONObject;import java.util.Iterator;/*** @Description 控制台打印Json出来* @Author Created by .* @Email hccpw@sina.com* @Date ${DATE}*/
public class JsonLog {private static final char TOP_LEFT_CORNER = '╔';private static final char BOTTOM_LEFT_CORNER = '╚';private static final String DOUBLE_DIVIDER = "═════════════════════════════════════════════════════════════════";public static void println(String content) {outp(" ", 0);if (content == null || content.isEmpty()) {return;}try {outp(TOP_LEFT_CORNER + DOUBLE_DIVIDER, 0);if (content.startsWith("{")) {setJSONObjectLog(new JSONObject(content), 1);} else if (content.startsWith("{")) {setJSONArrayLog(new JSONArray(content), 1);} else {outp(content, 1);}outp(BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER, 0);} catch (Exception e) {}outp(" ", 0);}/*** 对JSONObject的解析*/private static void setJSONObjectLog(JSONObject jsonObject, int count) throws Exception {if (jsonObject == null || jsonObject.length() == 0) {outp("{}", count);return;}outp("{", count);count = count + 1;Iterator msgIterator = jsonObject.keys();while (msgIterator.hasNext()) {String key = msgIterator.next();Object value = jsonObject.get(key);if (value.toString().startsWith("{")) {JSONObject object = new JSONObject(value.toString());if (object == null || object.length() == 0) {outp(key + ": {}", count);} else {outp(key + ": ", count);setJSONObjectLog(new JSONObject(value.toString()), count + 1);}} else if (value.toString().startsWith("[")) {JSONArray array = new JSONArray(value.toString());if (array == null || array.length() == 0) {outp(key + ": []", count);} else {outp(key + ": ", count);setJSONArrayLog(new JSONArray(value.toString()), count + 1);}} else {if (value instanceof String) {outp(key + ": " + "\"" + value + "\"" + " ,", count);} else {outp(key + ": " + value + " ,", count);}}}outp("}", count - 1);}/*** 对JSONArray的解析*/private static void setJSONArrayLog(JSONArray jsonArray, int count) throws Exception {if (jsonArray == null || jsonArray.length() == 0) {outp("[]", count);return;}outp("[", count);count = count + 1;for (int i = 0; i // Log.d("HTTPDEBUG", builder.toString() + s);}
}

 


转:https://my.oschina.net/openoschina/blog/1637701



推荐阅读
author-avatar
似懂非懂2502904027
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有