package wordcount.算数表达式;
import org.apache.commons.io.IOUtils;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Str2Map {
public String strs;
int position=0;
Object current;
Object nextToken(){
while (position<strs.length()){
switch (strs.charAt(position)){
case '\r':
position++ ; break;
case '\n':
position++ ; break;
case '\t':
position++ ; break;
case ' ':
position++ ; break;
case '"':
return current=getStr('"');
case '\'':
return current=getStr('\'');
case '{':
++position;
return current=Token.LJ;
case '}':
++position;
return current=Token.RJ;
case '[':
++position;
return current=Token.LA;
case ']':
++position;
return current=Token.RA;
case ',':
++position;
return current=Token.DOUHAO;
case ':':
++position;
return current=Token.MAO;
case 'n':
position+=4;
return current=Token.NULL;
default:{
if( '0'<=strs.charAt(position) && strs.charAt(position)<='9'){
return current=getNum();
}
}
}
}
return null;
}
private Number getNum() {
StringBuffer stringBuffer=new StringBuffer();
boolean isDouble=false;
while ('0'<=strs.charAt(position) && strs.charAt(position)<='9'||strs.charAt(position)=='.'){
if(strs.charAt(position)=='.'){
isDouble=true;
}
stringBuffer.append(strs.charAt(position));
position++;
}
if(!isDouble) {
return Integer.valueOf(stringBuffer.toString());
}else{
return Double.valueOf(stringBuffer.toString());
}
}
public String getStr(char end){
StringBuffer stringBuffer=new StringBuffer();
position++;
while (strs.charAt(position)!=end){
stringBuffer.append(strs.charAt(position));
position++;
}
position++;
return stringBuffer.toString();
}
public Map<String,Object> getJSON(){
Map<String,Object> json=new HashMap<>();
nextToken();
while (current!=Token.RJ){
String key= (String) current;
nextToken();
Object o = nextToken();
if(o == Token.NULL){
json.put(key,null );
nextToken();
}
else if(!(o instanceof Token)){
json.put(key,o );
nextToken();
}
else if ( o == Token.LJ){
json.put(key,getJSON() );
}
else if( o == Token.LA){
json.put(key, getArr());
}
if(current == Token.DOUHAO){
nextToken();
}
}
nextToken();
return json;
}
public List getArr(){
List list=new ArrayList();
nextToken();
while (current!=Token.RA){
if(current == Token.LA){
list.add(getArr());
}
else if(current == Token.LJ){
list.add(getJSON());
}
else{
if(current == Token.NULL){
list.add(null);
}else {
list.add(current);
}
nextToken();
}
if(current == Token.DOUHAO){
nextToken();
}
}
nextToken();
return list;
}
Object getObj(){
nextToken();
if(current==Token.LJ){
return getJSON();
}
else{
return getArr();
}
}
public static void main(String[] args) throws Exception {
Str2Map str2Map=new Str2Map();
str2Map.strs=IOUtils.toString(new FileReader("C:\\Users\\Administrator\\Desktop\\bb\\case.txt"));
System.out.println(str2Map.getObj());
}
}
//{"a":"a1","b":123,"c":{"a":"a1","b":123},"d":{"a":"a1","b":123,"c":{"a":"a1","b":123}}}
package wordcount.算数表达式;
public enum Token {
LJ("{"),RJ("}"),LA("["),RA("]"),DOUHAO(","),MAO(":"),NULL("null") ;
String str;
private Token(String str){
this.str=str;
}
}