- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
本框架JSON元素组成和分析,JsonElement分三大类型JsonArray,JsonObject,JsonString.
JsonArray:数组和Collection子类,指定数组的话,使用ArrayList来add元素,遍历ArrayList再使用Array.newInstance生成数组并添加元素即可. 。
JsonObject:带有泛型的封装类,给带有泛型的字段赋值,关键在于如何根据“指定的类型”和“Field.getGenericType”来生成新的字段Type.
需要你了解java.lang.reflect.Type的子类 。
java.lang.reflect.GenericArrayType //通用数组类型 T[] java.lang.reflect.ParameterizedType //参数类型,如: java.util.List<java.lang.String> java.util.Map<java.lang.String,java.lang.Object> java.lang.reflect.TypeVariable //类型变量 K,V java.lang.reflect.WildcardType //通配符类,例如: ?, ? extends Number, ? super Integer 。
java.lang.Class //int.class User.class ..... 。
JsonString:分析字符串并解析成指定的对应类型 。
下面是本JSON框架的分析图 。
。
下面是简陋版的解析代码。该类没有解析日期,数值等代码,只是方便理解解析过程.
package june.zero.json.reader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class JsonSimpleParser implements CharSequence { public static void main(String[] args) { String json = "[{a:1,b:2},[1,2,3]]"; Object obj = new JsonSimpleParser().parse(json); System.out.println(obj); } protected static final String JSON_EXTRA_CHAR_ERROR = "Extra characters exist! "; protected static final String JSON_OBJECT_COLON_ERROR = "Wrong format of JsonObject, no separator colon! "; protected static final String JSON_OBJECT_END_ERROR = "The JsonObject format must end with comma as a separator or with right curly brace! "; protected static final String JSON_ARRAY_END_ERROR = "The JsonArray format must end with comma as a separator or with right bracket! "; protected static final String JSON_STRING2_END_ERROR = "The character starts with double quotation marks, but does not end with double quotation marks! "; protected static final String JSON_STRING1_END_ERROR = "The character starts with single quotation marks, but does not end with single quotation marks! "; protected Reader reader; protected static final int capacity = 1024; protected final char[] cache = new char[capacity]; protected int count; protected int position; public void read() throws IOException { this.position = 0; this.count = this.reader.read(this.cache,0,capacity); } /** * 当前指针指向的字符 * @return */ public char current() { if(this.count==-1) return '\uffff'; return this.cache[this.position]; } /** * 当前指针指向的索引下标 * @return */ public int position() { return this.position; } /** * 指针指向下一个字符,判断是否需要重新读取数据 * @return * @throws IOException */ public boolean nextRead() throws IOException { return ++this.position>=this.count; } /** * 指针指向下一个字符 * @return * @throws IOException */ public void next() throws IOException { if(++this.position>=this.count){ this.position = 0; this.count = this.reader.read(this.cache,0,capacity); } } /** * 跳过空白字符 * @throws IOException */ public void skip() throws IOException { while(this.count!=-1&&Character.isWhitespace(this.current())){ if(++this.position>=this.count){ this.position = 0; this.count = this.reader.read(this.cache,0,capacity); } } } public Object parse(String json) throws RuntimeException { return parse(new StringReader(json)); } /** * 解析 */ public Object parse(Reader reader) throws RuntimeException { try { this.reader = reader; this.read(); Object value = parse(); this.skip(); if(this.count!=-1){ throw new RuntimeException(JSON_EXTRA_CHAR_ERROR); } return value; } catch (Throwable e) { throw new RuntimeException(e); } finally { try { this.close(); } catch (IOException e) { } } } protected Object parse() throws IOException { this.skip(); char current = this.current(); if(current=='{'){ this.next(); this.skip(); Map<Object, Object> object = new HashMap<Object, Object>(); if(this.current() == '}') { this.next(); return object; } do{ Object key = parse(); this.skip(); if(this.current()!=':'){ throw new RuntimeException(JSON_OBJECT_COLON_ERROR); } this.next(); object.put(key, parse()); this.skip(); current = this.current(); if(current=='}') break; if(current!=','){ throw new RuntimeException(JSON_OBJECT_END_ERROR); } this.next(); this.skip(); }while(true); this.next(); return object; } if(current=='['){ this.next(); this.skip(); List<Object> array = new ArrayList<Object>(); if(this.current() == ']'){ this.next(); return array; } do{ array.add(parse()); this.skip(); current = this.current(); if(current==']') break; if(current!=','){ throw new RuntimeException(JSON_ARRAY_END_ERROR); } this.next(); this.skip(); }while(true); this.next(); return array; } this.skip(); StringBuilder string = new StringBuilder(); if(current=='"'){ this.next(); int offset = this.position; while(this.count!=-1&& this.current()!='"'){ if(this.nextRead()){ string.append(this, offset, this.position); offset = 0; this.position = 0; this.count = this.reader.read(this.cache,0,capacity); } } string.append(this, offset, this.position); if(this.current()!='"'){ throw new RuntimeException(JSON_STRING2_END_ERROR); } this.next(); return string; } if(current=='\''){ if(++this.position>=this.count){ this.position = 0; this.count = this.reader.read(this.cache,0,capacity); } int offset = this.position; while(this.count!=-1&&this.current()!='\''){ if(this.nextRead()){ string.append(this, offset, this.position); offset = 0; this.read(); } } string.append(this, offset, this.position); if(this.current()!='\''){ throw new RuntimeException(JSON_STRING1_END_ERROR); } this.next(); return string; } int offset = this.position; while(this.count!=-1&& (current = this.current())!=','&& current!=':'&& current!=']'&& current!='}'&& !Character.isWhitespace(current)){ if(this.nextRead()){ string.append(this, offset, this.position); offset = 0; this.read(); } } string.append(this, offset, this.position); return string; } /** * 关闭流 * @throws IOException */ public void close() throws IOException{ if(this.reader!=null){ this.reader.close(); this.reader = null; } } @Override public char charAt(int index) { return this.cache[index]; } @Override public int length() { return this.count; } @Override public CharSequence subSequence(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) throw new StringIndexOutOfBoundsException(end); if (start > end) throw new StringIndexOutOfBoundsException(end - start); StringBuilder string = new StringBuilder(); for (int i = start; i < end; i++) { string.append(this.cache[i]); } return string; } @Override public String toString() { if(this.count<0) return null; return new String(this.cache,this.position,this.count-this.position); } }
。
转载请标明该来源。https://www.cnblogs.com/JuneZero/p/18252639 。
源码和jar包及其案例:https://www.cnblogs.com/JuneZero/p/18237283 。
。
最后此篇关于JavaJSON组成和解析的文章就讲到这里了,如果你想了解更多关于JavaJSON组成和解析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!