json轉換成java對象樣本_java

來源:互聯網
上載者:User

json字串轉Java對象有很多工具可以使用,下面的小例子只是我練手的

複製代碼 代碼如下:

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('{' == item || '}' == item){
    dkh = !dkh;
   }
   if(dyh&&syh&&dkh)if('[' == item || ']' == item){
    zkh = !zkh;
   }
   if(dyh&&dkh&&zkh)if('"' == item){
    syh = !syh;
    continue;
   }
   if(syh&&dkh&&zkh)if(syh)if('\'' == item){
    dyh = !dyh;
    continue;
   }
   if(dyh&&syh&&dkh&&zkh)if(r2==null)if(dyh)if(':'==item){
    isKey = false;
    continue;
   }
   if(dyh&&syh&&dkh&&zkh)if(','==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()));
   }
  }

 }
}

控制台輸出

複製代碼 代碼如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.