奥门威尼斯网址String 的类型判断

by admin on 2019年9月5日
function ajaxGetMenuList(){ 
         $.getJSON("login.do", function(json){ 

          var r = "";
    zNodes += "[";
             $(json.menuList).each(function(i){ 

     r = json.menuList[i];
     if(i>0) zNodes += ",";
     zNodes += '{';
     zNodes += 'id:'+r.SId;
     zNodes += ', pId:'+r.SPid;
     zNodes += ', name:"'+r.STitle;
     if(r.SPid == 0) zNodes += '", open:true';
     else zNodes += '"';
     zNodes += '}';

             }) 

             zNodes += "]";
    zNodes = stringToJSON(zNodes);
    createTree(zNodes);
         }); 

  } 

  /**
   * json转String
   * @param {Object} obj
   * @memberOf {TypeName} 
   * @return {TypeName} 
   */
  function jsonToString (obj){   
         var THIS = this;    
         switch(typeof(obj)){   
             case 'string':   
                 return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';   
             case 'array':   
                 return '[' + obj.map(THIS.jsonToString).join(',') + ']';   
             case 'object':   
                  if(obj instanceof Array){   
                     var strArr = [];   
                     var len = obj.length;   
                     for(var i=0; i<len; i++){   
                         strArr.push(THIS.jsonToString(obj[i]));   
                     }   
                     return '[' + strArr.join(',') + ']';   
                 }else if(obj==null){   
                     return 'null';   

                 }else{   
                     var string = [];   
                     for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property]));   
                     return '{' + string.join(',') + '}';   
                 }   
             case 'number':   
                 return obj;   
             case false:   
                 return obj;   
         }   
     }

   /**
    * String转json
    * @param {Object} obj
    * @return {TypeName} 
    */
  function stringToJSON(obj){   
          return eval('(' + obj + ')');   
  } 
//判断对象类型
function getType(x){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(x==null){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return "null";
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;var t= typeof x;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(t!="object"){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return t;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;var c=Object.prototype.toString.apply(x);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;c=c.substring(8,c.length-1);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(c!="Object"){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return c;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(x.constructor==Object){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return c
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if("classname" in x.prototype.constructor
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&& typeof x.prototype.constructor.classname=="string"){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return x.constructor.prototype.classname;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return "<unknown type>";
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}

最推荐的判断一个变量是否是String的方法是:
var a = "a"; Object.prototype.toString.call(a) === "[object String]"

  

 架包: import com.alibaba.fastjson.JSON;


 String arryStr="[{\"Name\": \"A\", \"Address\":\"北京\"},{\"Name\": \"B\", \"Address\":\"上海\"},{\"Name\": \"C\", \"Address\":\"安徽\"}]";  //json 数组

 JSONArray jsonarry=(JSONArray)JSONArray.parse(arryStr);//json字符串转JsonArry  

 List<TestEntity> list=JSON.parseArray(arryStr,TestEntity.class);//json字符串转List集合

 TestEntity model=JSON.parseObject(jsonarry.get(1).toString(),TestEntity.class);//json 轉java對象

json转对象

 

为什么不用 typeof:
因为一个通过原始函数构造的string在typeof时 值为object;
var a = new String("a"); typeof a ;//object

  

InvestDetailCash investDetailCash = new InvestDetailCash();

        try {
            String json = request.getParameter("content");
            if (!StringUtils.isEmpty(json)) {
                ObjectMapper oMapper = new ObjectMapper();
                oMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                investDetailCash = oMapper.readValue(json, InvestDetailCash.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST)
@ResponseBody
public void updateInvestorApplyAccountNo(HttpServletRequest request,
HttpServletResponse response,
@RequestBody String requestBody) {
int num = 0;
String result = "";
//下面是把拿到的json字符串转成 json对象
JSONObject jsStr = JSONObject.parseObject(requestBody); //将字符串{“id”:1}
//int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值


/** 
* json对象转换成java对象 
*/ 
InvestorApplyModel stud = (InvestorApplyModel) JSONObject.toJavaObject(jsStr,InvestorApplyModel.class);

}

ajaxGetMenuList(){ $.getJSON(login.do,
function(json){ var r = ;zNodes += [;
$(json.menuList).each(function(i){ r = json.menuList[i];if(i0) zNodes
+= ,;zNodes += {;zNodes…

xml转对象
xml

 

public class PFServiceResult {
    public String getInstallWorkOrderResult() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Data>\n" +
                "\t<QueryMethod>queryWorkOrderToDolist</QueryMethod>\n" +
                "\t<Return>\n" +
                "\t\t<ContentList>\n" +
                "\t\t\t<Content>\n" +
                "\t\t\t\t<Public>\n" +
                "\t\t\t\t\t<WorkOrderID>13418857</WorkOrderID>\n" +
                "\t\t\t\t\t<OrderID>3467119</OrderID>\n" +
                "\t\t\t\t\t<OrderCode>710000037570127940182</OrderCode>\n" +
                "\t\t\t\t\t<ServiceName>天翼宽带-订购</ServiceName>\n" +
                "\t\t\t\t\t<AccNbr>04714736690</AccNbr>\n" +
                "\t\t\t\t\t<CustName>闫万明\n" +
                "用户等级:三星用户\n" +
                "附属产品:\n" +
                "速率:\n" +
                "天翼高清帐号:04715221212\n" +
                "\n" +
                "受理时间:2018/01/22 11:45:24</CustName>\n" +
                "\t\t\t\t\t<CustLinkPerson>闫万明</CustLinkPerson>\n" +
                "\t\t\t\t\t<CustLinkPhone>15049144139</CustLinkPhone>\n" +
                "\t\t\t\t\t<TacheName>外线施工</TacheName>\n" +
                "\t\t\t\t\t<TacheCode>FWKT_WXSG</TacheCode>\n" +
                "\t\t\t\t\t<Address>内蒙古呼和浩特赛罕区兴安南路冶金厅冶金厅12号楼1单元(SF)5楼西户10号</Address>\n" +
                "\t\t\t\t\t<SlaTime/>\n" +
                "\t\t\t\t\t<WorkOrderType>正常工单</WorkOrderType>\n" +
                "\t\t\t\t\t<CreateDate>2017-11-30 12:49:42.0</CreateDate>\n" +
                "\t\t\t\t\t<flag>正常工单</flag>\n" +
                "\t\t\t\t\t<iomCreateDate/>\n" +
                "\t\t\t\t\t<PARTY_TYPE>STA</PARTY_TYPE>\n" +
                "\t\t\t\t</Public>\n" +
                "\t\t\t</Content>\n" +
                "\t\t</ContentList>\n" +
                "\t\t<Result>000</Result>\n" +
                "\t\t<ErrorDesc>查找成功</ErrorDesc>\n" +
                "\t\t<PageSize>15</PageSize>\n" +
                "\t\t<PageNum>1</PageNum>\n" +
                "\t\t<TotalPage>3</TotalPage>\n" +
                "\t\t<TotalNum>40</TotalNum>\n" +
                "\t</Return>\n" +
                "</Data>";
    }
}

XmltoModel

@Component
public class XmltoModel {

    /*
    * XML解析需要替换其中结果标签,替换的标签为对象的实例(字符)
     */
    public String replaceString(String xml,String replaced,String objectName){
        xml = xml.replaceAll(replaced,objectName);
        return xml;
    }

    /*
    *@param <T>
    *@param t
    *
     */
    public <T> T xmltoModel(String xml, T t) {

        try {
            JAXBContext context = JAXBContext.newInstance(t.getClass());
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
            return t;
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return t;
    }
}

InstallWorkOrderResult

@XmlRootElement(name = "Data")
public class InstallWorkOrderResult {
    String queryMethod;

    @XmlElement(name = "QueryMethod")
    public String getQueryMethod() {
        return queryMethod;
    }

    public void setQueryMethod(String queryMethod) {
        this.queryMethod = queryMethod;
    }

    @XmlElement(name = "stringReturn")
    public StringReturn getStringReturn() {
        return stringReturn;
    }

    public void setStringReturn(StringReturn stringReturn) {
        this.stringReturn = stringReturn;
    }

    StringReturn stringReturn;

    public static class StringReturn {
        String result;
        String errorDesc;
        String pageSize;
        String pageNum;
        String totalPage;
        String totalNum;
        List<InstallWorkOrder> installWorkOrderList;

        @XmlElement(name = "Result")
        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        @XmlElement(name = "ErrorDesc")
        public String getErrorDesc() {
            return errorDesc;
        }

        public void setErrorDesc(String errorDesc) {
            this.errorDesc = errorDesc;
        }

        @XmlElement(name = "PageSize")
        public String getPageSize() {
            return pageSize;
        }

        public void setPageSize(String pageSize) {
            this.pageSize = pageSize;
        }

        @XmlElement(name = "PageNum")
        public String getPageNum() {
            return pageNum;
        }

        public void setPageNum(String pageNum) {
            this.pageNum = pageNum;
        }

        @XmlElement(name = "TotalPage")
        public String getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(String totalPage) {
            this.totalPage = totalPage;
        }

        @XmlElement(name = "TotalNum")
        public String getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(String totalNum) {
            this.totalNum = totalNum;
        }


        @XmlElementWrapper(name = "installWorkOrderList")
        @XmlElement(name = "installWorkOrder")
        public List<InstallWorkOrder> getInstallWorkOrderList() {
            return installWorkOrderList;
        }

        public void setInstallWorkOrderList(List<InstallWorkOrder> installWorkOrderList) {
            this.installWorkOrderList = installWorkOrderList;
        }
    }
}

解析xml成对象的方法

public InstallWorkOrderResult getInstallWorkOrderResult
    String result = pfServiceResult.getInstallWorkOrderResult();
    result = result.replaceAll("<Content>\n" +
                "\t\t\t\t<Public>", "<installWorkOrder>");
        result = result.replaceAll("</Public>\n" +
                "\t\t\t</Content>", "</installWorkOrder>");
        result = result.replaceAll("Return", "stringReturn");
        result = result.replaceAll("ContentList", "installWorkOrderList");
        System.out.println(result);
    installWorkOrderResult = xmltoModel.xmltoModel(result, installWorkOrderResult);
    return installWorkOrderResult;
}

解析xml
ParseXMLUtil

@Component
public class ParseXMLUtil {

     /**entity map对象,key:name ,value:entity的属性map集**/
    public Map entityMap ;

    /**column map 对象,key:entityName_colName , value:column的属性map集 **/
    public Map columnMap;

    /**rule map 对象,key:entityName_colName_ruleName, value: rule 的map集:找到一行rule**/
    public Map ruleMap ;

    /**rules map 对象, key:entityName_colName, value: rules 的map集:找到该column下所有的rule**/
    public Map  columnRulesMap ;

    /**entity--column map: key:entityName, value: column list:根据实体类名得到所有的列**/
    public Map columnListMap ;

   /**column list**/
    public List columnList ;

    public String entityName;


    /**主方法**/
//  public static void main(String[] args) {
//      File file = new File("src/company.xml");
////        new ParseXMLUtil(file);
//      parseXMLUtil(file);
//  }

    /**开始解析xml文件**/
    public void parseXMLUtil(File xmlFilePath){
        FileInputStream in = null;
        try {
            if(xmlFilePath == null){
                 throw new FileNotFoundException();
            }
            SAXReader reader = new SAXReader();
            in = new FileInputStream(xmlFilePath);
            Document doc = reader.read(in);
            Element root = doc.getRootElement();
            Iterator itEntity = root.elements("entity").iterator();
            while(itEntity.hasNext()){  //现在是用的全局变量,只能有一个entity节点!!
                Element entity = (Element) itEntity.next();
                parseEntity(entity);
            }

            /**测试entityMap 是否正确**/
            Map enMap = (Map) this.getEntityMap().get(entityName); //entityName:entity节点下的name
            Set<?> set = enMap.keySet();
            Iterator it = set.iterator();
            while(it.hasNext()){
                String uu = (String) it.next();
                System.out.println("entity properties:"+uu+" = "+enMap.get(uu));
            }

/*          *//**测试column list是否正确**//*
            List colList = (List) this.getColumnListMap().get("用户表");
            System.out.println("column size:"+colList.size());

            *//**测试columnMap是否正确**//*
            Map colMap = (Map) this.getColumnMap().get("用户表_员工号");
                Set<?> coListSet = colMap.keySet();
                Iterator coListIt = coListSet.iterator();
                while(coListIt.hasNext()){
                    String coListKey = (String) coListIt.next();
                    System.out.println("column  properties: "+coListKey+" = "+colMap.get(coListKey));
                }
            *//**测试ruleMap是否正确**//*
            if(this.getColumnRulesMap() != null){
                List rulesValidList = (List) this.getColumnRulesMap().get("用户表_员工号");
                for(int i=0;i<rulesValidList.size(); i++){
                    Map colRuleMap = (Map) rulesValidList.get(i);
                    String ruleName = (String) colRuleMap.get("name");
                    Map ruleMa = (Map) this.getRuleMap().get("用户表_员工号_"+ruleName); //eg: 用户表_用户名_nullable
                    String mess = (String) ruleMa.get("message");
                    System.out.println("Validate Rules"+i+" : "+mess);
                }
            }*/
        }catch(Exception e){
            e.printStackTrace();
        }

    }

     /**开始解析entity**/
    @SuppressWarnings("unchecked")
    public void parseEntity(Element entity){
        if(entity != null){

            /**对数据进行初始化设置**/
            columnListMap = new HashMap();
            columnMap = new HashMap();
            entityMap = new HashMap();
            ruleMap = new HashMap();
            columnRulesMap = new HashMap();
            columnList = new ArrayList();

            setEntityMap(entity);
//          String entityName = entity.attributeValue("name");
            entityName = entity.attributeValue("name");
            Iterator itColumn = entity.elements("column").iterator();
            while(itColumn.hasNext()){
                Element column = (Element) itColumn.next();
                setColumnMap(entityName,column);
            }
            columnListMap.put(entityName, columnList);
        }
    }



    /**将entity放入entityMap中**/
    @SuppressWarnings("unchecked")
    public void setEntityMap(Element entity){       
        Map ent = new HashMap();
        String name = entity.attributeValue("name");
        String code = entity.attributeValue("code");
        ent.put("name", name);
        ent.put("code", code);
        entityMap.put(name, ent);           
    }

    /**将column放入columnMap中**/
    @SuppressWarnings("unchecked")
    public void setColumnMap(String entityName,Element column){
        if(column != null){     
            Map col = new HashMap();
            String name = column.attributeValue("name");
            String code = column.attributeValue("code");
            String type = column.attributeValue("type");
            col.put("name", name);
            col.put("code", code);
            col.put("type", type);
            String columnMapKey = entityName+"_"+name;    //eg:  用户表_用户名
            columnMap.put(columnMapKey, col);       
            columnList.add(col);
            Iterator ruleIt = column.elements("rules").iterator();  //获得rules
            while(ruleIt.hasNext()){
                Element rules = (Element)ruleIt.next(); 
                Iterator rule  = rules.elements("rule").iterator();   //获得 rule
                while(rule.hasNext()){
                    Element ruleValid = (Element) rule.next();     //获得每一行rule
                    setRuleMap(entityName,name,ruleValid);                  
                }
            }
        }
    }

    /**将 rule 验证规则放入ruleMap中**/
    @SuppressWarnings("unchecked")
    public void setRuleMap(String entityName,String columnName,Element ruleValid){
        if(ruleValid != null){          
            String ruleName = ruleValid.attributeValue("name");
            String ruleMsg = ruleValid.attributeValue("message");
            Map ruleValidMap = new HashMap();
            ruleValidMap.put("name", ruleName);
            ruleValidMap.put("message", ruleMsg);
            String ruleStrKey = entityName+"_"+columnName+"_"+ruleName;
            String colStrKey = entityName+"_"+columnName;
            if(this.getColumnRulesMap().containsKey(colStrKey)){
                List valids = (List) this.getColumnRulesMap().get(colStrKey);
                valids.add(ruleValidMap);
            }else{
                List valids = new ArrayList();
                valids.add(ruleValidMap);
                this.columnRulesMap.put(colStrKey, valids);  //将每个column下的所有rules存入该map中
            }
            ruleMap.put(ruleStrKey, ruleValidMap); //将每个column下的一条rule存入该map中
        }
    }


    /**所有的get set 方法**/
    public Map getEntityMap() {
        return entityMap;
    }

    public void setEntityMap(Map entityMap) {
        this.entityMap = entityMap;
    }

    public Map getColumnMap() {
        return columnMap;
    }

    public void setColumnMap(Map columnMap) {
        this.columnMap = columnMap;
    }

    public Map getRuleMap() {
        return ruleMap;
    }

    public void setRuleMap(Map ruleMap) {
        this.ruleMap = ruleMap;
    }

    public Map getColumnRulesMap() {
        return columnRulesMap;
    }

    public void setColumnRulesMap(Map columnRulesMap) {
        this.columnRulesMap = columnRulesMap;
    }

    public Map getColumnListMap() {
        return columnListMap;
    }

    public void setColumnListMap(Map columnListMap) {
        this.columnListMap = columnListMap;
    }
}

URL url = ParseExcelUtil.class.getClassLoader().getResource("company.xml");
File xmlFile = new File(url.getFile());
parseXmlUtil.parseXMLUtil(xmlFile);

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图