目录
- 效果展示
转载:
http://vvcat.tcode.top:78/details/22
使用工具
IDEA2018.2 JDK1.8
使用说明
JSONObject内部是用Hashmap来存储的,所以输出是按key的ASCII码排序来的,如果要让JSONObject按固定顺序(put的顺序)排列,可以修改JSONObject的定义HashMap改为LinkedHashMap。
下面代码不需要固定顺序,所以采用HashMap
Website.java
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/12/8 23:44
* @Version 1.0
**/
public class Website {
private String name;
private String href;
private String desc;
private String logo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public Website(String name, String href, String desc, String logo) {
this.name = name;
this.href = href;
this.desc = desc;
this.logo = logo;
}
public Website() {
}
}
JsonFormatTool.java
import net.sf.json.JSONArray;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/12/8 13:38
* @Version 1.0
**/
public class JsonFormatTool {
/**
* 单位缩进字符串。
*/
private static String SPACE = " ";
/**
* 返回格式化JSON字符串。
*
* @param json 未格式化的JSON字符串。
* @return 格式化的JSON字符串。
*/
public static String formatJson(String json) {
StringBuffer result = new StringBuffer();
int length = json.length();
int number = 0;
char key = 0;
// 遍历输入字符串。
for (int i = 0; i < length; i++) {
// 1、获取当前字符。
key = json.charAt(i);
// 2、如果当前字符是前方括号、前花括号做如下处理:
if ((key == '[') || (key == '{')) {
// (1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) {
result.append('\n');
result.append(indent(number));
}
// (2)打印:当前字符。
result.append(key);
// (3)前方括号、前花括号,的后面必须换行。打印:换行。
result.append('\n');
// (4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。
number++;
result.append(indent(number));
// (5)进行下一次循环。
continue;
}
// 3、如果当前字符是后方括号、后花括号做如下处理:
if ((key == ']') || (key == '}')) {
// (1)后方括号、后花括号,的前面必须换行。打印:换行。
result.append('\n');
// (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
number--;
result.append(indent(number));
// (3)打印:当前字符。
result.append(key);
// (4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。
if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
result.append('\n');
}
// (5)继续下一次循环。
continue;
}
// 4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
/*if ((key == ',')) {
result.append(key);
result.append('\n');
result.append(indent(number));
continue;
}*/
// 5、打印:当前字符。
result.append(key);
}
return result.toString();
}
/**
* 返回指定次数的缩进字符串。每一次缩进三个空格,即SPACE。
*
* @param number 缩进次数。
* @return 指定缩进次数的字符串。
*/
private static String indent(int number) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < number; i++) {
result.append(SPACE);
}
return result.toString();
}
}
CreateFileUtil.java
mport java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/12/8 13:35
* @Version 1.0
**/
public class CreateFileUtil {
/**
* 生成.json格式文件
*/
public static boolean createJsonFile(String jsonString, String filePath, String fileName) {
// 标记文件生成是否成功
boolean flag = true;
// 拼接文件完整路径
String fullPath = filePath + File.separator + fileName + ".json";
// 生成json格式文件
try {
// 保证创建一个新文件
File file = new File(fullPath);
if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
file.getParentFile().mkdirs();
}
if (file.exists()) { // 如果已存在,删除旧文件
file.delete();
}
file.createNewFile();
if(jsonString.indexOf("'")!=-1){
//将单引号转义一下,因为JSON串中的字符串类型可以单引号引起来的
jsonString = jsonString.replaceAll("'", "\\'");
}
if(jsonString.indexOf("\"")!=-1){
//将双引号转义一下,因为JSON串中的字符串类型可以单引号引起来的
jsonString = jsonString.replaceAll("\"", "\\\"");
}
if(jsonString.indexOf("\r\n")!=-1){
//将回车换行转换一下,因为JSON串中字符串不能出现显式的回车换行
jsonString = jsonString.replaceAll("\r\n", "\\u000d\\u000a");
}
if(jsonString.indexOf("\n")!=-1){
//将换行转换一下,因为JSON串中字符串不能出现显式的换行
jsonString = jsonString.replaceAll("\n", "\\u000a");
}
// 格式化json字符串
jsonString = JsonFormatTool.formatJson(jsonString);
// 将格式化后的字符串写入文件
Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
write.write(jsonString);
write.flush();
write.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
// 返回是否成功的标记
return flag;
}
}
Main.java
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Website> agencyList1 = new ArrayList<Website>();
agencyList1.add(new Website("vvcat博客","http://vvcat.tcode.top:78/","世界很小,请带着梦想一起奔跑","http://vvcat.tcode.top:78/default/images/favicon.ico"));
agencyList1.add(new Website("vvcat博客","http://vvcat.tcode.top:78/","世界很小,请带着梦想一起奔跑","http://vvcat.tcode.top:78/default/images/favicon.ico"));
agencyList1.add(new Website("vvcat博客","http://vvcat.tcode.top:78/","世界很小,请带着梦想一起奔跑","http://vvcat.tcode.top:78/default/images/favicon.ico"));
agencyList1.add(new Website("vvcat博客","http://vvcat.tcode.top:78/","世界很小,请带着梦想一起奔跑","http://vvcat.tcode.top:78/default/images/favicon.ico"));
agencyList1.add(new Website("vvcat博客","http://vvcat.tcode.top:78/","世界很小,请带着梦想一起奔跑","http://vvcat.tcode.top:78/default/images/favicon.ico"));
List<Object> agencyList = new ArrayList<Object>();
Map<String, Object> agencyMap = new HashMap<>();
agencyMap.put("这是标题1", agencyList1);
agencyMap.put("这是标题2", agencyList1);
agencyMap.put("这是标题3", agencyList1);
agencyMap.put("这是标题4", "这是标题四的内容");
agencyMap.put("这是标题5", "这是标题五的内容");
agencyList.add(agencyMap);
JSONArray jsonObject = JSONArray.fromObject(agencyList);
String jsonString1 = jsonObject.toString();// 将以上HashMap中的内容转成字符串
CreateFileUtil.createJsonFile(jsonString1, "/JAVA/Java基础/vvcat/src/main/resources", "json");
}
}
CreateFileUtil类的createJsonFile方法
第一个参数为要写入文件的内容,第二个参数为路径,第三个参数为文件名
“/JAVA/Java基础/vvcat/src/main/resources” 这里是绝对路径,原路径"E:/JAVA/Java基础/vvcat/src/main/resources"由于是项目在E盘下的某个文件夹里创建,所以生成时自动生成在E盘下。在这里我省略了E:
“json” 为文件名,生成文件后,会自动生成.json后缀格式的文件
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net</groupId>
<artifactId>vvcat</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>A Camel Route</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier><!-- jdk版本 -->
</dependency>
</dependencies>
<!-- 如果只是单独 拿这些代码测试,不要忘记加以下代码,否则会报错-->
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
效果展示
[
{
"这是标题5":"这是标题五的内容","这是标题4":"这是标题四的内容","这是标题3":
[
{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
}
],"这是标题2":
[
{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
}
],"这是标题1":
[
{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
},{
"name":"vvcat博客","logo":"http://vvcat.tcode.top:78/default/images/favicon.ico","href":"http://vvcat.tcode.top:78/","desc":"世界很小,请带着梦想一起奔跑"
}
]
}
]