packagecom.github.kewei1;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importcom.github.kewei1.model.JsonMetaNode;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Set;
publicclassJsonToBean {
publicstaticStringINTEGER="java.lang.Integer";
publicstaticStringLONG="java.lang.Long";
publicstaticStringSTRING="java.lang.String";
publicstaticStringJSONOBJECT="com.alibaba.fastjson.JSONObject";
publicstaticStringJSONArray="com.alibaba.fastjson.JSONArray";
publicstaticStringFLOAT="java.lang.Float";
publicstaticStringDOUBLE="java.lang.Double";
publicstaticStringBIG_DECIMAL="java.math.BigDecimal";
publicstaticStringDATE="java.util.Date";
privatestaticStringcreateTable(StringtableName, List<JsonMetaNode>jsonMetaNodeList) {
StringsqlCreate="CREATE TABLE "+tableName+"(\n"+getRowName(jsonMetaNodeList);
returnsqlCreate;
}
privatestaticStringgetRowName(List<JsonMetaNode>jsonMetaNodeList) {
StringBuffersqlRowNameBuffer=newStringBuffer();
booleanhasId=false;
for (JsonMetaNodejsonMetaNode : jsonMetaNodeList) {
Stringkey=jsonMetaNode.getKey();
StringvalueType=jsonMetaNode.getValueType();
Stringtype="";
if (INTEGER.equals(valueType)) {
type="int(100)";
} elseif (LONG.equals(valueType)) {
type="bigint(100)";
} elseif (STRING.equals(valueType)) {
type="varchar(100)";
} elseif (BIG_DECIMAL.equals(valueType)) {
type="decimal(18,8)";
} elseif (FLOAT.equals(valueType)) {
type="float(100,10)";
} elseif (DOUBLE.equals(valueType)) {
type="double(100,10)";
} elseif (DATE.equals(valueType)) {
type="datetime";
} elseif (JSONOBJECT.equals(valueType) ||JSONArray.equals(valueType)) {
type="json";
}else {
type="varchar(100)";
}
sqlRowNameBuffer.append(key).append(" ").append(type).append(" ").append("NULL");
if (key.equals("id")){
hasId=true;
sqlRowNameBuffer.append(" ").append("NOT NULL ,").append("\r\n");
}else {
sqlRowNameBuffer.append(",").append("\r\n");
}
}
if (!hasId){
sqlRowNameBuffer.append("id").append(" ").append("int(12)").append(" ").append(" ")
.append("NOT NULL ,").append("\r\n PRIMARY KEY (`id`) ");
}
sqlRowNameBuffer.deleteCharAt(sqlRowNameBuffer.length() -1);
sqlRowNameBuffer.append(" ) ENGINE=InnoDB;");
StringsqlRowName=sqlRowNameBuffer.toString();
returnsqlRowName;
}
publicstaticStringjsontosql(StringtableName, Stringjson) {
List<JsonMetaNode>jsonMetaNodeList=newArrayList<JsonMetaNode>();
JSONObjectjsonObject= (JSONObject) JSON.parse(json);
Set<String>strings=jsonObject.keySet();
Iterator<String>iterator=strings.iterator();
while (iterator.hasNext()) {
JsonMetaNodejsonMete=newJsonMetaNode();
Stringnext=iterator.next();
jsonMete.setKey(next);
Objecto=jsonObject.get(next);
if (o!=null) {
Stringname=o.getClass().getName();
jsonMete.setValueType(name);
}
jsonMetaNodeList.add(jsonMete);
}
StringsqlCreateTable=createTable(tableName, jsonMetaNodeList);
System.out.println(sqlCreateTable);
returnsqlCreateTable;
}
publicstaticvoidmain(String[] args) {
Strings="{\"p_id\":\"73\",\"p_title\":\"微光波炉\\/烤箱保养\",\"p_mode\":\"1\",\"p_summary\":\"健康生活\",\"p_icon\":\"1491634953186.jpeg\",\"p_imageUrl\":null,\"p_priceA\":\"80\",\"p_priceB\":null,\"p_duration\":\"60\",\"p_introduce\":\"微波炉、烤箱等长时间不清洁会造成机器内汤汁堆积,对加热食物带来种种异味,滋生细菌,直接会导致肠道疾病。我们的高温深层次消毒服务,将微波炉进行深度清洁,去异味,高温杀菌,保障健康生活,延长微波炉使用寿命。\",\"p_fit_people\":\"微波炉\\/烤箱\",\"p_service_introduce\":\"\",\"p_pubtime\":\"1491634954\",\"p_pv\":\"1927\",\"c_id\":\"30\",\"m_id\":\"100\"}";
Stringyy="{\n"+"\t\"animals\":{\n"+"\t\"dog\":[\n"+"\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n"+"\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n"+"\t],\n"+"\t\"cat\":{\"name\":\"Matilda\"}\n"+"}\n"+"}";
Stringjsontosql=jsontosql("test", yy);
}
}