var user = {
name:"邪神",
age: "18",
sex:"男"
};
//对象转为字符串
var json=JSON.stringify(user);
//字符串转对象
var ob=JSON.parse(json);
console.log(user);
console.log(json);
console.log(ob);
==============================================
@Controller
public class controll {
@ResponseBody //不会进过试图解析器
@RequestMapping("J1")
public String jsonTest() throws JsonProcessingException {
User user = new User("邪神",18,"男");
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(user);
return s;
}
}
<!-- JSON乱码-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>