怎么把String字符串转换成集合

XiaoYi96 2016-07-14 11:09:08
这是一个String字符串,我想把它转换成MAP,求教怎么写后台代码,包括实体类该写啥,经理说必须是在后台转
{"code":"1","table":{
"title" :["单据名称","单据日期", "提交人员", "单据流水","帐套编号","帐套名称", "逻辑表名"],
"value0":["采购请购单","20160124","0034", "86", "005", "演示", "CGQGD"],
"value1":["采购请购单","20160123","0056", "78", "005", "演示", "CGQGD"],
"value2":["采购请购单","20160123","0041" ,"80", "005", "演示", "CGQGD"],
"value3":["采购请购单","20160122","0037", "81", "005", "演示", "CGQGD"],
"value4":["采购请购单","20160122","0052", "82", "005", "演示", "CGQGD"],
"value5":["采购请购单","20160122","0043", "83", "005", "演示", "CGQGD"],
"value6":["采购请购单","20160124","0034", "84", "005", "演示", "CGQGD"],
"value7":["采购请购单","20160126","0054", "88", "005", "演示", "CGQGD"]}}
...全文
2633 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
halaoda 2018-02-24
  • 打赏
  • 举报
回复
不清楚吖,,我也在纳闷
XiaoYi96 2016-07-14
  • 打赏
  • 举报
回复
[quote=引用 3 楼 rickylin86 的回复:] 请问一下,大神,那个file.bin是什么
rickylin86 2016-07-14
  • 打赏
  • 举报
回复

import java.io.IOException;


import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.Scanner;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessBill{
	public static void main(String[] args){
		String content = loadContent("file.bin");
		HashMap<String,Bill> billRecords = process(content);
		Set<String> keySet = billRecords.keySet();
		Iterator<String> keys = keySet.iterator();
		while(keys.hasNext()){
			String key = keys.next();
			Bill bill = billRecords.get(key);
			System.out.printf("%s:%s\n",key,bill);
		}
	}

	private static String loadContent(String filePath){
		StringBuilder result = new StringBuilder();
		Path path = Paths.get(filePath);
		try(Scanner file = new Scanner(path);){
			while(file.hasNextLine()){
				result.append(file.nextLine());
			}
		}catch(IOException e){
			e.printStackTrace();
			System.exit(1);
		}
		return result.toString();
	}

	private static HashMap<String,Bill> process(String content){
		HashMap<String,Bill> result = new HashMap<>();
		String regex = "\"(?<key>[^\"]+)\":\\[\"(?<billType>[^\"]+)\"\\s*,\\s*\"(?<billDate>[^\"]+)\"\\s*,\\s*\"(?<submitPerson>[^\"]+)\"\\s*,\\s*\"(?<billNumber>[^\"]+)\"\\s*,\\s*\"(?<accountNumber>[^\"]+)\"\\s*,\\s*\"(?<accountName>[^\"]+)\"\\s*,\\s*\"(?<sheetName>[^\"]+)\"\\]";
		Matcher matcher = Pattern.compile(regex).matcher(content);
		String billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName;
		String key;
		while(matcher.find()){
			key = matcher.group("key");
			billType = matcher.group("billType");
			billDate = matcher.group("billDate");
			submitPerson = matcher.group("submitPerson");
			billNumber = matcher.group("billNumber");
			accountNumber = matcher.group("accountNumber");
			accountName = matcher.group("accountName");
			sheetName = matcher.group("sheetName");
			result.put(key,new Bill(billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName));
		}
		return result;
	}

	private static class Bill{

		public Bill(String billType,String billDate,String submitPerson,String billNumber,String accountNumber,String accountName,String sheetName){
			this.billType = billType;
			this.billDate = billDate;
			this.submitPerson = submitPerson;
			this.billNumber = billNumber;
			this.accountNumber = accountNumber;
			this.accountName = accountName;
			this.sheetName = sheetName;
		}

		@Override
		public String toString(){
			return String.format("[%s,%s,%s,%s,%s,%s,%s]",billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName);
		}
		

		private String billType;
		private String billDate;
		private String submitPerson;
		private String billNumber;
		private String accountNumber;
		private String accountName;
		private String sheetName;
	}


}
XiaoYi96 2016-07-14
  • 打赏
  • 举报
回复
引用 1 楼 qq_15915835 的回复:
中国程序员的质量真是让人担心啊。
大神就不要嘲笑我了哭,应届生真的不会
qq_15915835 2016-07-14
  • 打赏
  • 举报
回复
中国程序员的质量真是让人担心啊。
  • 打赏
  • 举报
回复
不能把数据组织成json标准格式么,用字符串处理多麻烦,下次万一不一样了呢
rickylin86 2016-07-14
  • 打赏
  • 举报
回复
文件内容复制到file.bin文件中.要不需要转义太多的双引号了.

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧