【java/Json】用Java对象构建Json语法树
本文后续:https://www.cnblogs.com/xiandedanteng/p/11973129.html
编译第一步:将文本解析成Java对象构成的语法树
第二步:将语法树输出整形好的Json新文本。
下面完成的是第二步:
package com.hy; import java.util.Collections; import java.util.LinkedList; import java.util.List; import org.apache.log4j.Logger; public class Json implements Comparable<Json>{ private static Logger log = Logger.getLogger(Json.class); // There are value types private int Type_String=1; private int Type_Array=2; private int Type_List=3; // Key always is String private String key; // There are three types of value private int valueType; private String valueString; private List<Json> valueArray;// 本质一致,表现不同 private List<Json> valueList; // indent depth private int depth; /** * Contructor1 */ public Json(String key,String value) { this.key=key; this.valueType=Type_String; this.valueString=value; this.depth=0; } public Json(String key,int type) { this.key=key; if(type==Type_List) { this.valueType=Type_List; this.valueList=new LinkedList<Json>(); }else if(type==Type_Array) { this.valueType=Type_Array; this.valueArray=new LinkedList<Json>(); } } public void addJsonToList(Json json) { if(valueList!=null) { valueList.add(json); adjustDepth(); } } public void addJsonToArray(Json json) { if(valueArray!=null) { valueArray.add(json); adjustDepth(); } } private void adjustDepth() { if(valueType==Type_List) { for(Json json:valueList) { json.depth=this.depth+1; json.adjustDepth(); } } if(valueType==Type_Array) { for(Json json:valueArray) { json.depth=this.depth+1; json.adjustDepth(); } } } public String toString() { StringBuilder sb=new StringBuilder(); // key String tabs=getIndentSpace(); sb.append(tabs); //sb.append("\""+(key==null?"":key)+"\""); if(key!=null) { sb.append("\""+key+"\""); sb.append(":"); }else { } // value if(valueType==Type_String) { sb.append("\""+valueString+"\""); }else if(valueType==Type_Array) { sb.append("[\n"); /*for(Json json:valueArray) { sb.append(json.toString()+"\n"); }*/ int n=valueArray.size(); for(int i=0;i<n;i++) { Json json=valueArray.get(i); if(i!=n-1) { sb.append(json.toString()+",\n"); }else { sb.append(json.toString()+"\n"); } } sb.append(tabs+"]"); }else if(valueType==Type_List) { sb.append("{\n"); Collections.sort(valueList); /*for(Json json:valueList) { sb.append(json.toString()+"\n"); }*/ int n=valueList.size(); for(int i=0;i<n;i++) { Json json=valueList.get(i); if(i!=n-1) { sb.append(json.toString()+",\n"); }else { sb.append(json.toString()+"\n"); } } sb.append(tabs+"}"); } //sb.append(","); return sb.toString(); } public int compareTo(Json other) { return this.key.compareTo(other.key); } private String getIndentSpace() { return String.join("", Collections.nCopies(this.depth, " ")); } public static void main(String[] args) { Json id1=new Json("id","001"); Json name1=new Json("name","白菜"); Json title=new Json("title",3); title.addJsonToList(id1); title.addJsonToList(name1); Json empty1=new Json(null,3); empty1.addJsonToList(new Json("id","001")); empty1.addJsonToList(new Json("id","你好白菜")); Json empty2=new Json(null,3); empty2.addJsonToList(new Json("id","001")); empty2.addJsonToList(new Json("id","你好萝卜")); Json content=new Json("content",2); content.addJsonToArray(empty1); content.addJsonToArray(empty2); Json data=new Json("data",3); data.addJsonToList(title); data.addJsonToList(content); Json status=new Json("status","0000"); Json message=new Json("message","success"); Json root=new Json(null,3); root.addJsonToList(status); root.addJsonToList(message); root.addJsonToList(data); System.out.println(root.toString()); } }
输出:
{ "data":{ "content":[ { "id":"001", "value":"你好白菜" }, { "id":"002", "value":"你好萝卜" } ], "title":{ "id":"001", "name":"白菜" } }, "message":"success", "status":"0000" }
可以看到,子对象都是按字母序排列的,这也是制作此类的最终目的之一。
--END-- 2019年12月1日11:26:39
分类:
Java.JSON
, Java.Compiler
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)