JSON入门到精通

册噩噩 2020-09-08 09:39:22
JSON的语法格式?
*对象表示为键值对
*数据由逗号分割
*花括号保存对象
*方括号保存数组

实例:
{"username":"十五万","age":"11","sex":"男"}
追加:
JSON是一种轻量级的数据交换格式,它基于JavaScript的一个子集.
JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。

JSON 和 JS 对象互转(这里我们用jackson这个,不用fastjson!)
var user = {
name :"孙伟浩",
age : 16,
sex : "男"
};

console.log(user);
js对象通过.stringfy(参数)来得到var形式的jason字符串
var str = JSON.stringify(user);
console.log(str);
用.parse(参数)方法把json字符串转成js对象形式
var obj = JSON.parse(str);
console.log(obj);

package com.example.fuxi.controller;

import com.example.fuxi.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

//produce:指定响应体的返回类型和编码-----防止返回到页面乱码
@RequestMapping(value = "/json",produces = "application/json;charset=utf-8") //String 的转换格式 produces = "text/html;charset=utf-8"
@ResponseBody
public String json1(){

User user = new User("十五万","11","男");

ObjectMapper mapper = new ObjectMapper(); //需要一个jackson的对象映射器,也就是一个类,可以将对象转换为字符串

String userJson = null;

try {
userJson = mapper.writeValueAsString(user); //将一个java对象,转换成字符串
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(userJson);

return userJson;
}


}



对上面进行简化如下

@RequestMapping(value = "/json2",produces = "application/json;charset=utf-8") //String 的转换格式 produces = "text/html;charset=utf-8"
@ResponseBody
public String json2 () throws JsonProcessingException {
User user = new User("李太白","26","男");
return new ObjectMapper().writeValueAsString(user);
}


list集合转换json

@RequestMapping(value = "/json3",produces = "application/json;charset=utf-8") //String 的转换格式 produces = "text/html;charset=utf-8"
@ResponseBody
public String json3 () throws JsonProcessingException {
List<User> list = new ArrayList<User>();
list.add(new User("李太白1","26","男"));
list.add(new User("李太白2","25","男"));
list.add(new User("李太白3","24","男"))
list.add(new User("李太白4","23","男"));
return new ObjectMapper().writeValueAsString(list);
}






通过配置文件设置json格式

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
...全文
2487 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
册噩噩 2020-10-17
  • 打赏
  • 举报
回复
jackson
册噩噩 2020-09-18
  • 打赏
  • 举报
回复
引用 4 楼 蜜义的回复:
一般用在哪些地方呢
controller层传json到页面层
LwinnerG 2020-09-09
  • 打赏
  • 举报
回复
一切V随缘 2020-09-09
  • 打赏
  • 举报
回复
一般用在哪些地方呢
聪头 2020-09-09
  • 打赏
  • 举报
回复
KeepSayingNo 2020-09-09
  • 打赏
  • 举报
回复
册噩噩 2020-09-08
  • 打赏
  • 举报
回复
望各位指教

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧