package com.example.automatedtrading.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class JsonStrong {
public static void main(String[] args) throws IOException {
writeJson();
JSONObject jsonObject = readJson();
JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("contractsPool").toString());
JSONObject parseObject = JSONObject.parseObject(jsonArray.get(0).toString());
System.out.println(parseObject.get("key"));
}
public static JSONObject readJson() throws IOException {
String path = System.getProperty("user.dir")+"/json";
File file = new File(path+"/"+"test.json");
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String str;
StringBuilder stringBuilder= new StringBuilder();
while ((str = bufferedReader.readLine())!=null){
System.out.println(str);
stringBuilder.append(str);
}
bufferedReader.close();
reader.close();
return JSONObject.parseObject(stringBuilder.toString());
}
public static void writeJson() throws IOException {
Map<String,Object> map = new JSONObject();
map.put("dominantContract","SA208");
map.put("direction",3);
map.put("contractsPool","[{\"key\":1,\"edit\":false,\"contractName\":\"SA207\",\"buyOpen1\":1,\"sellFlat1\":-1,\"sellOpen1\":-1,\"buyFlat1\":-1,\"buyOpen2\":-1,\"sellFlat2\":-1,\"sellOpen2\":-1,\"buyFlat2\":-1,\"ratio\":-1}]");
map.put("orderNumber",1);
String json = formatJson(JSONObject.parseObject(map.toString()).toJSONString());
String path = System.getProperty("user.dir")+"/json";
File fileJson = new File(path);
if (!fileJson.exists()){
System.out.println("successful?"+fileJson.mkdirs());
}
File file = new File(path+"/"+"test.json");
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.write("\n");
writer.close();
}
/**
* 格式化
*
*/
public static String formatJson(String jsonStr) {
if (null == jsonStr || "".equals(jsonStr)) {
return "";
}
StringBuilder sb = new StringBuilder();
char last;
char current = '\0';
int indent = 0;
boolean isInQuotationMarks = false;
for (int i = 0; i < jsonStr.length(); i++) {
last = current;
current = jsonStr.charAt(i);
switch (current) {
case '"':
if (last != '\\'){
isInQuotationMarks = !isInQuotationMarks;
}
sb.append(current);
break;
case '{':
case '[':
sb.append(current);
if (!isInQuotationMarks) {
sb.append('\n');
indent++;
addIndentBlank(sb, indent);
}
break;
case '}':
case ']':
if (!isInQuotationMarks) {
sb.append('\n');
indent--;
addIndentBlank(sb, indent);
}
sb.append(current);
break;
case ',':
sb.append(current);
if (last != '\\' && !isInQuotationMarks) {
sb.append('\n');
addIndentBlank(sb, indent);
}
break;
default:
sb.append(current);
}
}
return sb.toString();
}
/**
* 添加space
*/
private static void addIndentBlank(StringBuilder sb, int indent) {
for (int i = 0; i < indent; i++) {
sb.append('\t');
}
}
}
控制台输出:
{
"contractsPool":"[{\"key\":1,\"edit\":false,\"contractName\":\"SA207\",\"buyOpen1\":1,\"sellFlat1\":-1,\"sellOpen1\":-1,\"buyFlat1\":-1,\"buyOpen2\":-1,\"sellFlat2\":-1,\"sellOpen2\":-1,\"buyFlat2\":-1,\"ratio\":-1}]",
"orderNumber":1,
"dominantContract":"SA208",
"direction":3
}
1