java生成松散格式的json字符串
fastjson挺好用的
假设你有这么一个json字符串
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("name", UUID.randomUUID().toString());
String str = jsonObject.toJSONString();
System.out.println(str);
-----------------------------------------------------------
{"name":"c7afdda0-1d8e-47a3-9588-2d116cf3ec3d","id":1}
想生成这么样的格式
{
"name":"c7afdda0-1d8e-47a3-9588-2d116cf3ec3d",
"id":1
}
又或者有这么个json字符串
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 5; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", i);
jsonObject.put("name", UUID.randomUUID().toString());
jsonArray.add(jsonObject);
}
String str = jsonArray.toJSONString();
System.out.println(str);
---------------------------------------------------------------
[{"name":"1c40f906-7e04-4e82-9220-073e631f2d08","id":0},{"name":"444273f8-0164-4da0-aca0-ece422028618","id":1},{"name":"adbb53ea-048c-450e-9ced-1a3b54103832","id":2},{"name":"b2bec740-4293-401f-b1a3-f2330d38f483","id":3},{"name":"a318b04e-5b30-4c72-8a92-bec4b50dabd0","id":4}]
想生成这么个格式
[
{
"name":"1c40f906-7e04-4e82-9220-073e631f2d08",
"id":0
},
{
"name":"444273f8-0164-4da0-aca0-ece422028618",
"id":1
},
{
"name":"adbb53ea-048c-450e-9ced-1a3b54103832",
"id":2
},
{
"name":"b2bec740-4293-401f-b1a3-f2330d38f483",
"id":3
},
{
"name":"a318b04e-5b30-4c72-8a92-bec4b50dabd0",
"id":4
}
]
这时候其实你可以打开json.cn并且关闭博客骂一句**浪费时间
或者复制这段话,和我一起做**
public class JsonArr2JsonString {
@Test
public void test(){
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 5; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", i);
jsonObject.put("name", UUID.randomUUID().toString());
jsonArray.add(jsonObject);
}
String str = jsonArray.toJSONString();
System.out.println(str);
int num = 0;
StringBuilder temp = new StringBuilder();
char c;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
c = chars[i];
if(c == '['){
temp.append(c);
print(temp.toString() , num, 0);
num++;
temp.setLength(0);
}else if(c == '{'){
if (temp.length() > 0){
print(temp.toString() , num, 0);
temp.setLength(0);
num++;
}
temp.append(c);
print(temp.toString() , num, 0);
temp.setLength(0);
}else if(c == ','){
temp.append(c);
print(temp.toString(), num, chars[i-1] == '}' ? 0 : 1);
temp.setLength(0);
}else if(c == ']'){
temp.append(c);
print(temp.toString(), num, 0);
temp.setLength(0);
}else if(c == '}'){
print(temp.toString() , num, 1);
temp.setLength(0);
temp.append(c);
if(i < chars.length-1 && chars[i+1] != ','){
print(temp.toString() , num, 0);
temp.setLength(0);
num--;
}
}else {
temp.append(c);
}
}
}
public void print(String str, int num, int offset){
for (int i = 0; i < num+offset; i++) {
System.out.print(" ");
}
System.out.println(str);
}
}
public class JsonObj2JsonString {
@Test
public void test(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("name", UUID.randomUUID().toString());
String str = jsonObject.toJSONString();
System.out.println(str);
int num = 0;
StringBuilder temp = new StringBuilder();
char c;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
c = chars[i];
if(c == '{'){
if (temp.length() > 0){
print(temp.toString() , num, 0);
temp.setLength(0);
num++;
}
temp.append(c);
print(temp.toString() , num, 0);
temp.setLength(0);
}else if(c == ','){
temp.append(c);
print(temp.toString(), num, chars[i-1] == '}' ? 0 : 1);
temp.setLength(0);
}else if(c == '}'){
print(temp.toString() , num, 1);
temp.setLength(0);
temp.append(c);
print(temp.toString() , num, 0);
temp.setLength(0);
num--;
}else {
temp.append(c);
}
}
}
public void print(String str, int num, int offset){
for (int i = 0; i < num+offset; i++) {
System.out.print(" ");
}
System.out.println(str);
}
}
没想到啊 有一天我居然又用到了这个东西
于是回来更新博客了
package com.xu.code.util;
import org.dom4j.DocumentException;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class JsonFormat {
public static void main(String[] args) throws DocumentException, IOException {
String filePath = "C:/新建文件夹/测试模型2.json";
StringBuilder fileContent = new StringBuilder();
FileInputStream fis = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
fileContent.append(line);
fileContent.append("\r\n"); // 补上换行符
}
List<String> list = toLooseFormatJson(fileContent.toString());
System.out.println(stringArrayToLooseFormatJsonString(list));
}
public static String stringArrayToLooseFormatJsonString( List<String> list){
StringBuilder sb = new StringBuilder();
for (String s : list) {
sb.append(s).append("\r\n");
}
System.out.println(sb);
return sb.toString();
}
/**
* json str 的松散格式 转换前提是一行的json字符串
*
* @param str String
*/
public static List<String> toLooseFormatJson(String str) {
List<String> result = new ArrayList<>();//重新定义什么叫result
char[] chars = str.toCharArray();
int tab = 0;
StringBuilder sb = new StringBuilder();
boolean notStringValue = true;
for (char c : chars) {
if (c == '"') notStringValue = !notStringValue;//确定一下现在是不是String str 避免误会特殊符号换行
if (notStringValue && (c == '{' || c == '[' || c == ',')) {//不是字符值 且 是 { [ , 符号时
sb.append(c);
result.add(getEmptyStr(tab) + sb);
if (c != ',') tab++;
sb = new StringBuilder();
} else if (notStringValue && (c == '}' || c == ']')) {//不是字符值 且 是 } ] 符号时
if (sb.length() > 0) result.add(getEmptyStr(tab) + sb);
tab--;
result.add(getEmptyStr(tab) + c);
sb = new StringBuilder();
} else {
sb.append(c);
}
}
return result;
}
private static final String default_empty = " ";//两个空格
private static final String long_empty = " ";//四个空格
/**
* 获取空字符串
*
* @param tab tab的数量
* @return String 数据
*/
private static String getEmptyStr(int tab) {
return getEmptyStr(tab, default_empty);
}
/**
* 获取空字符串
*
* @param tab tab的数量
* @param empty 空字符串
* @return String 数据
*/
private static String getEmptyStr(int tab, String empty) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < tab; i++) {
stringBuilder.append(empty);
}
return stringBuilder.toString();
}
}