用JAVA给JSON进行排版
来源:互联网 发布:软件项目解决方案模板 编辑:程序博客网 时间:2024/06/05 20:14
功能:可以把一行的JSON字符串格式化后进行输出,带有缩进,看起来更加直观。
支持JSON格式的字符串和文件格式化输出。
package json;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import vote.Vote;
/**
* JSON打印器
* @author jiujie
* @version $Id: JSONPrinter.java, v 0.1 2016年7月28日 下午5:08:41 jiujie Exp $
*/
public class JSONPrinter {
private Object jsonObj;
/**
* JSON文件打印器
* constructor
* @author jiujie
* 2016年7月28日 下午5:08:59
* @param jsonFile
*/
public JSONPrinter(File jsonFile) {
this.jsonObj = jsonFile;
}
/**
* JSON字符串打印器
* constructor
* @author jiujie
* 2016年7月28日 下午5:08:59
* @param jsonString
*/
public JSONPrinter(String jsonString) {
this.jsonObj = jsonString;
}
public void print() {
if (jsonObj instanceof File) {
try {
print(new FileInputStream((File) jsonObj));
} catch (Exception e) {
e.printStackTrace();
}
} else if (jsonObj instanceof String) {
try {
print(new ByteArrayInputStream(((String) jsonObj).getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void print(InputStream inputStream) throws IOException {
InputStreamReader in = new InputStreamReader(inputStream);
int read = 0;
int indent = 0;
while ((read = in.read()) > 0) {
char ch = (char) read;
switch (ch) {
case '{': {
indent = printAndRightMove(indent, ch);
break;
}
case '}': {
indent = printAndLeftMove(indent, ch);
break;
}
case '[': {
indent = printAndRightMove(indent, ch);
break;
}
case ']': {
indent = printAndLeftMove(indent, ch);
break;
}
case ',': {
System.out.println(ch);
System.out.print(getBlankString(indent));
break;
}
default: {
System.out.print(ch);
break;
}
}
}
in.close();
}
private int printAndLeftMove(int indent, char ch) {
System.out.println();
indent -= 2;
System.out.print(getBlankString(indent) + ch);
return indent;
}
private int printAndRightMove(int indent, char ch) {
System.out.println();
System.out.println(getBlankString(indent) + ch);
indent += 2;
System.out.print(getBlankString(indent));
return indent;
}
private String getBlankString(int length) {
if (length <= 0) {
return "";
}
String blankString = "";
for (int i = 0; i < length; i++) {
blankString += " ";
}
return blankString;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
ClassLoader classLoader = Vote.class.getClassLoader();
String path = classLoader.getResource("").toString().replace("/bin", "").replace("file:/",
"") + "src/json/json.txt";
JSONPrinter jsonPrinter = new JSONPrinter(new File(path));
jsonPrinter.print();
JSONPrinter stringJsonPrinter = new JSONPrinter("{score:100,age:30}");
stringJsonPrinter.print();
}
}
- 用JAVA给JSON进行排版
- iPhone用coreText(核心文本)给文字进行排版
- JAVA对输出进行排版
- 用空格进行字符串排版
- 用PhotoShop快速给证件照排版
- 不用表格,用样式进行排版定位
- json序列化排版
- Mac OS 下用Tex Shop 进行中文排版
- 用Java对JSON字符串进行格式化输出
- 用Java对JSON字符串进行格式化输出
- 如何将JSON对象传递给Controller进行处理
- java 后台返回json数据给前台
- java 后台返回json数据给前台
- Java通过Json传递数据给JavaScript
- 【Java】给org.json 添加 json转Bean的方法
- 使用Word2000进行公式排版
- XeCJK+XeLaTex进行中文排版
- 使用Word2003进行公式排版
- Spring Boot (九):过滤器、拦截器、监听器
- MD_behavior_nestedscroll
- Django REST FrameWork中文教程4:验证和权限
- 开发过程中遇到的一些编码问题怎么解决
- IntelliJ IDEA创建和配置Maven项目并运行
- 用JAVA给JSON进行排版
- SpringCloud(第 033 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务
- Android小技巧之json格式字符串写入csv文件
- Android系统级开发进程清理功能的一些记录和发现(一)
- 2017年最新App Store审核指南(官方)
- 折叠菜单
- 视频会议及流媒体十大开源项目
- eclipse实用快捷键
- Spring Boot (十):打包部署