首页 文章 精选 留言 我的

精选列表

搜索[转换工具],共10000篇文章
优秀的个人博客,低调大师

巧用fastjson自定义序列化类实现字段的转换

项目中突然需要增加另一个字段的查找,而这个查找需要查另一张表的记录。 但现在产品很多地方都要增加该字段,如何最快的实现该功能呢。 办法如下: 通过fastjson序列化时,增加该字段的序列化类,该序列化类通过CODE查找名称,并序列化到前端。 VO的功能修改 @Data public class SysDictItemSelectVo implements Serializable{ private static final long serialVersionUID = 1L; @JSONField(serializeUsing=ToSystemNameSerializer.class) private String systemName; public String getSystemName(){ return this.getCode(); } } 序列化的类的属性 import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.ObjectSerializer; import com.alibaba.fastjson.serializer.SerializeWriter; import java.io.IOException; import java.lang.reflect.Type; public class ToSystemNameSerializer implements ObjectSerializer { public static final ToSystemNameSerializer instance = new ToSystemNameSerializer(); @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.out; if (object == null) { out.writeNull(); return; } //原来的值code String strVal = object.toString(); System.out.println(strVal); //通过code查找相关名称,测试先写死值 String name="std"; out.writeString(name); } } 经测试,序列化正常,其他地方只需要在VO增加该注解即可.

优秀的个人博客,低调大师

使用Jackson来实现java对象和json格式的相互转换

在经过数据库持久化,网络编程,多线程了解之后, 可以进入一些开发技术了。 当然,数据结构,算法,开发模式,第三方库的了解也是必须的。 疯狂软件出的《Spring Mybatis企业应用实战》, 作为操作解说型的教程, 还是不错的! 以下几个文件,是为了怕自己删除了没地方找回来而存在的。 web.xml <?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> springmvc-config.xml <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="org.fkit.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> Book.java package org.fkit.domain; import java.io.Serializable; public class Book implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String name; private String author; public Book() { super(); } public Book(Integer id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + "]"; } } BookController.java package org.fkit.controller; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.fkit.domain.Book; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import com.fasterxml.jackson.databind.ObjectMapper; @Controller @RequestMapping("/json") public class BookController { private static final Log logger = LogFactory.getLog(BookController.class); @RequestMapping(value="/testRequestBody") public void setJson(@RequestBody Book book, HttpServletResponse response) throws Exception { ObjectMapper mapper = new ObjectMapper(); logger.info(mapper.writeValueAsString(book)); book.setAuthor("chengang"); response.setContentType("text/html; charset=UTF-8"); response.getWriter().println(mapper.writeValueAsString(book)); } } index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <title>测试接收JSON格式数据</title> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src="json2.js"></script> <script type="text/javascript"> $(document).ready(function() { testRequestBody(); }); function testRequestBody() { $.ajax("${pageContext.request.contextPath}/json/testRequestBody" , { dataType:"json", type:"post", contentType:"application/json", data:JSON.stringify({id:1, name: "Spring MVC"}), async: true, success: function(data) { console.log(data); $("#id").html(data.id); $("#name").html(data.name); $("#author").html(data.author); }, error: function() { alert("send data error!"); } }); } </script> </head> <body> <h2>测试接收JSON格式数据</h2> 编号:<span id="id"></span><br> 书名:<span id="name"></span><br> 作者:<span id="author"></span><br> </body> </html>

优秀的个人博客,低调大师

sklearn.feature_extraction.DictVectorizer将字典格式的数据转换为特征

class sklearn.feature_extraction. DictVectorizer ( dtype=<class ‘numpy.float64’> , separator=’=’ , sparse=True , sort=True ) Transforms lists of feature-value mappings to vectors. This transformer turns lists of mappings (dict-like objects ) of feature names to feature values into Numpy arrays or scipy. sparse matrices(稀疏矩阵) for use with scikit-learn estimators. When feature values are strings, this transformer will do a binary one-hot (aka one-of-K) coding: one boolean-valued feature is constructed for each of the possible string values that the feature can take on. For instance, a feature “f” that can take on the values “ham” and “spam” will become two features in the output, one signifying “f=ham”, the other “f=spam”. However, note that this transformer will only do a binary one-hot encoding when feature values are of type string. If categorical features are represented as numeric values such as int, the DictVectorizer can be followed by OneHotEncoder to complete binary one-hot encoding. Features that do not occur in a sample (mapping) will have a zero value in the resulting array/matrix. Read more in the User Guide . Parameters: dtype : callable, optional The type of feature values. Passed to Numpy array/scipy.sparse matrix constructors as the dtype argument. separator : string, optional Separator string used when constructing new features for one-hot coding. sparse : boolean, optional. Whether transform should produce scipy.sparse matrices. True by default. sort : boolean, optional. Whether feature_names_ and vocabulary_ should be sorted when fitting. True by default. Attributes: vocabulary_ : dict A dictionary mapping feature names to feature indices. feature_names_ : list A list of length n_features containing the feature names (e.g., “f=ham” and “f=spam”).

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册