Home  >  Article  >  Java  >  Example of converting json to java object

Example of converting json to java object

高洛峰
高洛峰Original
2017-01-19 14:48:45978browse

There are many tools that can be used to convert json string to Java object. The following small example is just my practice

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.kit.JsonKit;

public class JsonToJavaObject {
 public static void main(String[] args) {
  Object o1 = parse("{\"aa\":123,cc:[1,2,3,4,{cd:f,bb:234}]}");
  System.out.println(JsonKit.toJson(o1));
 }
 public static Object parse(String json){
  if(json == null){
   return null;
  }
  json = json.trim();
  if("string".equals(typeof(json))){
   return json;
  }

  if("map".equals(typeof(json))){
   return parseMap(json);
  }

  if("list".equals(typeof(json))){
   return parseList(json);
  }

  return null;
 }

 public static Map parseMap(String json){
  if(!"map".equals(typeof(json))){
   throw new RuntimeException("json 不是Map类型");
  }
  Map r = new HashMap();
  parseToken(r,json,null);
  return r;
 }

 public static List parseList(String json){
  if(!"list".equals(typeof(json))){
   throw new RuntimeException("json 不是list类型");
  }
  List r = new ArrayList();
  parseToken(null, json, r);
  return r;
 }

 public static String typeof(String json){
  if(json.length() == 0)return "string";
  if('{'==json.charAt(0)){
   if('}' == json.charAt(json.length()-1)){
    return "map";
   }
  }

  if('['==json.charAt(0)){
   if(']'==json.charAt(json.length()-1)){
    return "list";
   }
  }

  return "string";
 }
 private static void parseToken(Map r, String json,List r2) {
  boolean syh = true; //双引号
  boolean dyh = true;//单引号
  boolean dkh = true;//大括号
  boolean zkh = true;//中括号
  boolean isKey = true;
  StringBuffer key = new StringBuffer();
  StringBuffer value = new StringBuffer();
  for(int i=1;i<json.length()-1;i++){
   char item = json.charAt(i);
   if(dyh&&syh&&zkh)if(&#39;{&#39; == item || &#39;}&#39; == item){
    dkh = !dkh;
   }
   if(dyh&&syh&&dkh)if(&#39;[&#39; == item || &#39;]&#39; == item){
    zkh = !zkh;
   }
   if(dyh&&dkh&&zkh)if(&#39;"&#39; == item){
    syh = !syh;
    continue;
   }
   if(syh&&dkh&&zkh)if(syh)if(&#39;\&#39;&#39; == item){
    dyh = !dyh;
    continue;
   }
   if(dyh&&syh&&dkh&&zkh)if(r2==null)if(dyh)if(&#39;:&#39;==item){
    isKey = false;
    continue;
   }
   if(dyh&&syh&&dkh&&zkh)if(&#39;,&#39;==item){
    isKey = true;
    if(r != null){
     r.put(key.toString(), parse(value.toString()));
    }
    if(r2 != null){
     r2.add(parse(key.toString()));
    }
    key = new StringBuffer();
    value = new StringBuffer();
    continue;
   }
   if(isKey){
    key.append(item);
   }else{
    value.append(item);
   }
  }
  if(!key.toString().trim().equals("")){
   if(r != null){
    if(value.toString().trim().equals(""))throw new RuntimeException("json 格式错误");
    r.put(key.toString(), parse(value.toString()));
   }
   if(r2 != null){
    r2.add(parse(key.toString()));
   }
  }

 }
}

Console output

{"aa":"123","cc":["1","2","3","4",{"bb":"234","cd":"f"}]}

More json to java object examples related articles Please pay attention to PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn