|
@@ -0,0 +1,65 @@
|
|
|
|
+package com.fd.util;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by Owen on 2019/11/22 0022 16:35
|
|
|
|
+ */
|
|
|
|
+public class JsonUtils {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String path = "static/config.json";
|
|
|
|
+ String s = readFile(path);
|
|
|
|
+ System.out.println(s);
|
|
|
|
+
|
|
|
|
+ writeJsonFile( s);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取json 文件, windows版
|
|
|
|
+ */
|
|
|
|
+ public static String readFile(String path){
|
|
|
|
+ String resource = FileUtils.getResource();
|
|
|
|
+ String param = FileUtils.readFile( resource+path);
|
|
|
|
+ return param;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void writeJsonFile(String str){
|
|
|
|
+ JSONObject original = JSON.parseObject(str);
|
|
|
|
+// String layers = original.getString("layers");
|
|
|
|
+
|
|
|
|
+ JSONArray layers = JSON.parseArray(original.getString("layers"));
|
|
|
|
+
|
|
|
|
+ JSONObject subJson = new JSONObject();
|
|
|
|
+ subJson.put("name", "111");
|
|
|
|
+ subJson.put("text", "222");
|
|
|
|
+ subJson.put("type", "3333");
|
|
|
|
+ subJson.put("checked", "444");
|
|
|
|
+ subJson.put("show", "555");
|
|
|
|
+ subJson.put("url", "6666");
|
|
|
|
+
|
|
|
|
+ layers.add(subJson);
|
|
|
|
+
|
|
|
|
+ original.put("layers", layers);
|
|
|
|
+
|
|
|
|
+ System.out.println();
|
|
|
|
+ System.out.println(original.toString());
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ FileUtils.fileWriter(JSON.toJSONString(original), "F:\\test\\123.json");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|