不需要导入外部包,自行编写的json字符串转map工具类
来源:互联网 发布:王安石 张居正 知乎 编辑:程序博客网 时间:2024/06/05 00:53
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonUtils {
public static Map<String,Object> toMap(String ori) {
ori = removeBlack(ori);
return toMapHelp(ori);
}
//必须首尾是{}
public static Map<String,Object> toMapHelp(String ori) {
Map<String,Object> map = new HashMap<String,Object>();
String[] manyMaps = splitToSingeMap(ori);
for (String e: manyMaps) {
int indexOfF = 0;
int shuangyh = 0;
for (indexOfF = 0;indexOfF<e.length();indexOfF++) {
if (e.charAt(indexOfF) == '\"' && shuangyh==0) {
shuangyh ++;
continue;
}
if (e.charAt(indexOfF) == '\"' && shuangyh==1) {
shuangyh --;
continue;
}
if (shuangyh == 0 && e.charAt(indexOfF) == ':') {
break;
}
}
if (indexOfF < e.length()-1) {
String fieldName = e.substring(0, indexOfF);
String value = e.substring(indexOfF+1,e.length());
if (fieldName.charAt(0) == '\"') {//字符串需要截取
fieldName = fieldName.substring(1,fieldName.length()-1);
}
if (value.charAt(0) == '{') {//子map元素
map.put(fieldName, toMapHelp(value));
} else if (value.charAt(0) == '[') {//子list元素
map.put(fieldName, toList(value));
} else if (value.charAt(0) == '\"'){//字符串元素
map.put(fieldName,value.substring(1,value.length()-1));
} else {//其他元素
map.put(fieldName,value);
}
}
}
return map;
}
//必须首尾是[]
private static List<Object> toList(String ori) {
List<Object> list = new ArrayList<Object>();
String[] manyMaps = splitToSingeMap(ori);
for (String e:manyMaps) {
list.add(toMapHelp(e));
}
return list;
}
//必须首尾是{}或者[]
private static String[] splitToSingeMap (String ori) {
if (ori == null) {
return null;
}
int lastIndex = 1;//从第二个,第一个是{
int lastLeftC = 0;//左边的{数量,起始是0
int lastLeftR = 0;//左边的[数量,起始是0
int splotLength = 0;//map个数
int shuangyhao = 0;//考虑{}有可能在""里面
for (int index = 0;index<ori.length();index++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == '{') {
lastLeftC ++;
continue;
}
if (ori.charAt(index) == '[') {
lastLeftR ++;
continue;
}
if (lastLeftR + lastLeftC == 1 && (ori.charAt(index) == ',' || index >= ori.length()-1)) {
splotLength ++;
}
if (ori.charAt(index) == '}') {
lastLeftC --;
continue;
}
if (ori.charAt(index) == ']') {
lastLeftR --;
continue;
}
}
String[] maps = new String[splotLength];
lastIndex = 1;//从第二个,第一个是{
lastLeftC = 0;//左边的{数量,起始是0
int mapIndex = 0;
for (int index = 0;index<ori.length();index++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == '{') {
lastLeftC ++;
continue;
}
if (ori.charAt(index) == '[') {
lastLeftR ++;
continue;
}
if (lastLeftR + lastLeftC == 1 && (ori.charAt(index) == ',' || index >= ori.length()-1)) {
maps[mapIndex] = ori.substring(lastIndex, index);
lastIndex = index + 1;
mapIndex ++;
}
if (ori.charAt(index) == ']') {
lastLeftR --;
continue;
}
if (ori.charAt(index) == '}') {
lastLeftC --;
continue;
}
}
return maps;
}
private static String removeBlack(String ori) {
if (ori == null) {
return null;
}
int shuangyhao = 0;//考虑{}有可能在""里面
boolean isblanck[] = new boolean[ori.length()];
for (int index = 0; index < ori.length() ; index ++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == ' '
|| ori.charAt(index) == '\n'
|| ori.charAt(index) == '\t'
|| ori.charAt(index) == '\r') {
isblanck[index] = true;
}
}
StringBuilder sb = new StringBuilder("");
for (int i = 0;i< ori.length();i++) {
if (!isblanck[i]) {
sb.append(ori.charAt(i));
}
}
return sb.toString();
}
public static void main(String args[]) {
String s = "{aaa:[{\"album\":\"jj\"},{album:jjser},{album:jjfghfgh}]}";
Map<String,Object> result = null;
long time = System.currentTimeMillis();
for (int i=0;i<=100000;i++) {
result = toMap(s);
}
List list = (List) result.get("aaa");
List<UserAlbumRequest> listA = new ArrayList<UserAlbumRequest>();
for (Object o:list) {
Map<String,String> map = (Map<String, String>) o;
UserAlbumRequest re = new UserAlbumRequest();
re.setAlbum(map.get("album"));
listA.add(re);
}
System.out.println(list.get(0));
}
}
class UserAlbumRequest {
private String id;
private String album;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonUtils {
public static Map<String,Object> toMap(String ori) {
ori = removeBlack(ori);
return toMapHelp(ori);
}
//必须首尾是{}
public static Map<String,Object> toMapHelp(String ori) {
Map<String,Object> map = new HashMap<String,Object>();
String[] manyMaps = splitToSingeMap(ori);
for (String e: manyMaps) {
int indexOfF = 0;
int shuangyh = 0;
for (indexOfF = 0;indexOfF<e.length();indexOfF++) {
if (e.charAt(indexOfF) == '\"' && shuangyh==0) {
shuangyh ++;
continue;
}
if (e.charAt(indexOfF) == '\"' && shuangyh==1) {
shuangyh --;
continue;
}
if (shuangyh == 0 && e.charAt(indexOfF) == ':') {
break;
}
}
if (indexOfF < e.length()-1) {
String fieldName = e.substring(0, indexOfF);
String value = e.substring(indexOfF+1,e.length());
if (fieldName.charAt(0) == '\"') {//字符串需要截取
fieldName = fieldName.substring(1,fieldName.length()-1);
}
if (value.charAt(0) == '{') {//子map元素
map.put(fieldName, toMapHelp(value));
} else if (value.charAt(0) == '[') {//子list元素
map.put(fieldName, toList(value));
} else if (value.charAt(0) == '\"'){//字符串元素
map.put(fieldName,value.substring(1,value.length()-1));
} else {//其他元素
map.put(fieldName,value);
}
}
}
return map;
}
//必须首尾是[]
private static List<Object> toList(String ori) {
List<Object> list = new ArrayList<Object>();
String[] manyMaps = splitToSingeMap(ori);
for (String e:manyMaps) {
list.add(toMapHelp(e));
}
return list;
}
//必须首尾是{}或者[]
private static String[] splitToSingeMap (String ori) {
if (ori == null) {
return null;
}
int lastIndex = 1;//从第二个,第一个是{
int lastLeftC = 0;//左边的{数量,起始是0
int lastLeftR = 0;//左边的[数量,起始是0
int splotLength = 0;//map个数
int shuangyhao = 0;//考虑{}有可能在""里面
for (int index = 0;index<ori.length();index++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == '{') {
lastLeftC ++;
continue;
}
if (ori.charAt(index) == '[') {
lastLeftR ++;
continue;
}
if (lastLeftR + lastLeftC == 1 && (ori.charAt(index) == ',' || index >= ori.length()-1)) {
splotLength ++;
}
if (ori.charAt(index) == '}') {
lastLeftC --;
continue;
}
if (ori.charAt(index) == ']') {
lastLeftR --;
continue;
}
}
String[] maps = new String[splotLength];
lastIndex = 1;//从第二个,第一个是{
lastLeftC = 0;//左边的{数量,起始是0
int mapIndex = 0;
for (int index = 0;index<ori.length();index++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == '{') {
lastLeftC ++;
continue;
}
if (ori.charAt(index) == '[') {
lastLeftR ++;
continue;
}
if (lastLeftR + lastLeftC == 1 && (ori.charAt(index) == ',' || index >= ori.length()-1)) {
maps[mapIndex] = ori.substring(lastIndex, index);
lastIndex = index + 1;
mapIndex ++;
}
if (ori.charAt(index) == ']') {
lastLeftR --;
continue;
}
if (ori.charAt(index) == '}') {
lastLeftC --;
continue;
}
}
return maps;
}
private static String removeBlack(String ori) {
if (ori == null) {
return null;
}
int shuangyhao = 0;//考虑{}有可能在""里面
boolean isblanck[] = new boolean[ori.length()];
for (int index = 0; index < ori.length() ; index ++) {
if (ori.charAt(index) == '\"' && shuangyhao==0) {
shuangyhao ++;
continue;
}
if (ori.charAt(index) == '\"' && shuangyhao==1) {
shuangyhao --;
continue;
}
if (shuangyhao == 1) {
continue;
}
if (ori.charAt(index) == ' '
|| ori.charAt(index) == '\n'
|| ori.charAt(index) == '\t'
|| ori.charAt(index) == '\r') {
isblanck[index] = true;
}
}
StringBuilder sb = new StringBuilder("");
for (int i = 0;i< ori.length();i++) {
if (!isblanck[i]) {
sb.append(ori.charAt(i));
}
}
return sb.toString();
}
public static void main(String args[]) {
String s = "{aaa:[{\"album\":\"jj\"},{album:jjser},{album:jjfghfgh}]}";
Map<String,Object> result = null;
long time = System.currentTimeMillis();
for (int i=0;i<=100000;i++) {
result = toMap(s);
}
List list = (List) result.get("aaa");
List<UserAlbumRequest> listA = new ArrayList<UserAlbumRequest>();
for (Object o:list) {
Map<String,String> map = (Map<String, String>) o;
UserAlbumRequest re = new UserAlbumRequest();
re.setAlbum(map.get("album"));
listA.add(re);
}
System.out.println(list.get(0));
}
}
class UserAlbumRequest {
private String id;
private String album;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
}
阅读全文
0 0
- 不需要导入外部包,自行编写的json字符串转map工具类
- json字符串转List/Map的工具类
- json字符串与Object、List、Map的互转工具类
- JSON数组形式字符串转换为Map数组(转为其他的Bean的话,请参考自行变形)
- JSON数组形式字符串转换为Map数组(转为其他的Bean的话,请参考自行变形)
- json字符串转map
- 通用工具类(Map转Json)
- Java项目导入外部的JAR包
- 导入外部jar包的方法
- python模块及外部包的导入
- ViewPagerIndicator+viewpager的简单使用,不需要导入Library包
- EXCEL表格数据导入到数据库示例,请需要的兄弟自行编写数据层!
- java json字符串转map
- jsonlib 去掉不需要的Java bean属性 生成json 字符串
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- jackson工具类json转list、json转map、json转bean、bean转json
- List转JSON字符串工具类
- 将JSON转换成MAP的工具类
- TypeScript 学习笔记2: Getting Started
- Keil连接生成bin文件时报错:--- Error: User Command terminated, Exit-Code = 1
- poll服务器编程综合
- 普通的APK安装更新
- ES6
- 不需要导入外部包,自行编写的json字符串转map工具类
- Java最简单的UDP收发数据的例子
- 学习JVM
- 实战
- Windows下使用dev-cpp连接mysql数据库
- 一大波Android技术干货
- opencv安装
- git 的一些命令
- TypeScript 学习笔记3: ECMAScript 2015 的新特性