的json和jsonbjson字段检验在插入的时候有什么区别

JAVA 对应实体类型 为Object, 在返回此值时,会将此值生成一个JSON 数据,自动生成 {"type":jsonb,"value":{数据}}如果不想要此格式,在实体get方法里
public Object getIdPayRule() {
if(null!=data){return JSONObject.parse(String.valueOf(data));}
}用了postgresql,在做插入时,一定要掌握数据类型转义操作符 ::如
insert into t1(id,json, jsonb) values(1,'{"bar": "baz", "balance": 7.77, "active":false}'::json, '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb)
【PostgreSQL】存取jsonb
PostgreSQL操作JSON数据
PostgreSQL之Json类型使用总结
使用mybatis拦截器做分表,mybatis与postgres josnb 数据类型映射
json,jsonb
PostgreSQL json jsonb 支持的value数据类型,如何构造一个jsonb
PostgreSQL学习篇9.15 JSON类型
postgresql----JSON类型和函数
没有更多推荐了,在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
postgresql 的json和jsonb字段在插入的时候有什么区别?多谢回答~
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
总体来说 jsonb 写入比 json 稍慢, 但检索较 json 快些,之前做过简单的测试比较,如下:
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
& & & & & json 特性的提升是9.4 的关键特性之一, 本人对于 json 的关注较少, 一方面由于之前版本的 json 并不十分成熟, 使用时需要配合使用外部模块如 PLV8, PLPerl 来弥补 JSON 功能的不足,一方面由于太懒没花精力研究; 但 9.4 版本的 JSON 功能完善很多, &jsonb 的出现带来了更多的函数, 更多的索引创建方式, 更多的操作符和更高的性能. 接下来通过一些例子来讲解, 希望更多的朋友能够了解并测试 PostgreSQL 的 json 功能.一 环境信息--1.1 之前写的关于 json 的博客&--1.2 测试环境硬件: 笔记本虚拟机系统: RHEL 6.2PG 版本: 9.4Beta1二生成测试数据--2.1 测试表user_ini: & & & & & & &基础数据表, 200 万数据.tbl_user_json: & &含有 json 数据类型表, 200 万数据tbl_user_jsonb: &含有 jsonb 数据类型表, &200 万数据--2.2 创建基础数据测试表francs=& create table user_ini(id int4 ,user_id int8, user_name character varying(64),create_time timestamp(6) with time zone default clock_timestamp());CREATE TABLEfrancs=& insert into user_ini(id,user_id,user_name) select r,round(random()*2000000), r || '_francs' from generate_series(1,2000000)INSERT 0 2000000备注: 生成 200 万测试数据.--2.3 生成 json 测试数据francs=& &create table tbl_user_json(id serial, user_info json);CREATE TABLEfrancs=& &insert into tbl_user_json(user_info) select row_to_json(user_ini) from user_INSERT 0 2000000Time:
ms--2.4 生成 jsonb 测试数据francs=& create table tbl_user_jsonb(id serial, user_info jsonb);CREATE TABLEfrancs=& insert into tbl_user_jsonb(user_info) select row_to_json(user_ini)::jsonb from user_ & &INSERT 0 2000000Time:
ms备注: &从时间来看, jsonb 插入速度比 json 插入速度稍慢, 再来看下两个表的大小如何?--2.5 比较表大小francs=& \dt+ tbl_user_json& & & & & & & & & & & &List of relations&Schema | & & Name & & &| Type &| Owner &| &Size &| Description&--------+---------------+-------+--------+--------+-------------&francs | tbl_user_json | table | francs | 269 MB |&(1 row)francs=& \dt+ tbl_user_jsonb& & & & & & & & & & & & List of relations&Schema | & & &Name & & &| Type &| Owner &| &Size &| Description&--------+----------------+-------+--------+--------+-------------&francs | tbl_user_jsonb | table | francs | 329 MB |&(1 row)--2.6 查看几条测试数据francs=& select * from tbl_user_jsonb limit 3;&id | & & & & & & & & & & & & & & & & & & & & & & & user_info & & & & & & & & & & & & & & & & & & & & & & &&----+-------------------------------------------------------------------------------------------------------& 1 | {"id": 1, "user_id": 116179, "user_name": "1_francs", "create_time": " 11:54:38."}& 2 | {"id": 2, "user_id": 956659, "user_name": "2_francs", "create_time": " 11:54:38."}& 3 | {"id": 3, "user_id": 1017031, "user_name": "3_francs", "create_time": " 11:54:38.37344+00"}(3 rows)备注: 以上是生成的测试数据, 列几条出来,方便查阅, 接下来看一个查询.三 基于 jsonb 字段 key 值的检索效率--3.1 根据 user_info 字段的 user_name key 检索&francs=& &select * from tbl_user_jsonb where user_info-&&'user_name'= '1_francs'; & &&id | & & & & & & & & & & & & & & & & & & & & & & & user_info & & & & & & & & & & & & & & & & & & & & & & &&----+-------------------------------------------------------------------------------------------------------& 1 | {"id": 1, "user_id": 116179, "user_name": "1_francs", "create_time": " 11:54:38."}(1 row)--3.2 执行计划和执行时间francs=& explain analyze select * from tbl_user_jsonb where user_info-&&'user_name'= '1_francs'; && & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & &---------------------------------------------------------------------------------------------------------------------&Seq Scan on tbl_user_jsonb &(cost=0.00..72097.82 rows=10000 width=140) (actual time=0.033.. rows=1 loops=1)& &Filter: ((user_info -&& 'user_name'::text) = '1_francs'::text)& &Rows Removed by Filter: 1999999&Planning time: 1.657 ms&Execution time:
ms(5 rows)备注: &此时还没建索引,走的全表扫, 花了将近 3 秒.--3.3 创建索引francs=& create index idx_gin_user_infob_user_name on tbl_user_jsonb using btree ((user_info -&& 'user_name'));CREATE INDEX--3.4 再次查看 &planfrancs=& explain analyze select * from tbl_user_jsonb where user_info-&&'user_name'= '1_francs'; && & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&-------------------------------------------------------------------------------------------------------------------------------------------&Bitmap Heap Scan on tbl_user_jsonb &(cost=233.93..23782.62 rows=10000 width=140) (actual time=0.046..0.047 rows=1 loops=1)& &Recheck Cond: ((user_info -&& 'user_name'::text) = '1_francs'::text)& &Heap Blocks: exact=1& &-& &Bitmap Index Scan on idx_gin_user_infob_user_name &(cost=0.00..231.43 rows=10000 width=0) (actual time=0.035..0.035 rows=1 loops=1)& & & & &Index Cond: ((user_info -&& 'user_name'::text) = '1_francs'::text)&Planning time: 0.144 ms&Execution time: 0.101 ms(7 rows)备注: 创建索引后, &上述查询走了索引, 仅花 &0.101 ms 完成检索, 挺给力!--3.5 根据 user_info 字段的 user_id 检索francs=& explain analyze select * from tbl_user_jsonb where user_info-&&'user_id'= '1'; && & & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & &&------------------------------------------------------------------------------------------------------------------------&Seq Scan on tbl_user_jsonb &(cost=0.00..72098.00 rows=10000 width=140) (actual time=89.888 rows=1 loops=1)& &Filter: ((user_info -&& 'user_id'::text) = '1'::text)& &Rows Removed by Filter: 1999999&Planning time: 3.304 ms&Execution time:
ms(5 rows)Time:
ms备注: 没走索引,花了 4 秒多,因为没建这个 key 上的索引.四 使用 GIN 索引& & &可以给 jsonb 字段创建 GIN 索引, GIN 索引有两种模式, 默认模式支持 &@&, ?, ?& 和 ?| 的索引查询, 我们这里使用默认模式.--4.1 删除之前索引,新建 gin 索引francs=& create index idx_tbl_user_jsonb_user_Info on tbl_user_jsonb using gin (user_Info);CREATE INDEXTime:
msfrancs=& \di+ idx_tbl_user_jsonb_user_Info& & & & & & & & & & & & & & & & & & & &List of relations&Schema | & & & & & & Name & & & & & & | Type &| Owner &| & & Table & & &| &Size &| Description&--------+------------------------------+-------+--------+----------------+--------+-------------&francs | idx_tbl_user_jsonb_user_info | index | francs | tbl_user_jsonb | 428 MB |&(1 row)备注: 索引很大,创建很慢,一般不会这么建索引.--4.2 基于 key/value 检索可以使用索引francs=& explain analyze select * from tbl_user_jsonb where user_info @& '{"user_id": 1017031}';& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &-----------------------------------------------------------------------------------------------------------------------------------------&Bitmap Heap Scan on tbl_user_jsonb &(cost=59.50..6637.58 rows=2000 width=140) (actual time=0.340..0.345 rows=1 loops=1)& &Recheck Cond: (user_info @& '{"user_id": 1017031}'::jsonb)& &Rows Removed by Index Recheck: 1& &Heap Blocks: exact=2& &-& &Bitmap Index Scan on idx_tbl_user_jsonb_user_info &(cost=0.00..59.00 rows=2000 width=0) (actual time=0.319..0.319 rows=2 loops=1)& & & & &Index Cond: (user_info @& '{"user_id": 1017031}'::jsonb)&Planning time: 0.118 ms&Execution time: 0.391 ms(8 rows)--4.3 以下查询不走索引francs=& explain analyze select * from tbl_user_jsonb where user_info-&&'user_name' ='4_francs';& & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & &---------------------------------------------------------------------------------------------------------------------&Seq Scan on tbl_user_jsonb &(cost=0.00..72098.00 rows=10000 width=140) (actual time=0.036.. rows=1 loops=1)& &Filter: ((user_info -&& 'user_name'::text) = '4_francs'::text)& &Rows Removed by Filter: 1999999&Planning time: 1.101 ms&Execution time:
ms(5 rows)francs=& explain analyze select * from tbl_user_jsonb where user_info-&'user_name' ?'4_francs';&& & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & &&--------------------------------------------------------------------------------------------------------------------&Seq Scan on tbl_user_jsonb &(cost=0.00..72098.00 rows=2000 width=140) (actual time=0.187.. rows=1 loops=1)& &Filter: ((user_info -& 'user_name'::text) ? '4_francs'::text)& &Rows Removed by Filter: 1999999&Planning time: 0.382 ms&Execution time:
ms(5 rows)备注: 以上的 ? 操作没走索引, 但 ? 操作支持索引检索,创建以下索引.--4.4 删除之前索引并新建以下索引francs=& create index idx_gin_user_info_user_name on tbl_user_jsonb using gin((user_info -& 'user_name'));CREATE INDEXfrancs=& explain analyze select * from tbl_user_jsonb where user_info-&'user_name' ?'4_francs';& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&----------------------------------------------------------------------------------------------------------------------------------------&Bitmap Heap Scan on tbl_user_jsonb &(cost=35.50..6618.58 rows=2000 width=140) (actual time=0.067..0.069 rows=1 loops=1)& &Recheck Cond: ((user_info -& 'user_name'::text) ? '4_francs'::text)& &Heap Blocks: exact=1& &-& &Bitmap Index Scan on idx_gin_user_info_user_name &(cost=0.00..35.00 rows=2000 width=0) (actual time=0.037..0.037 rows=1 loops=1)& & & & &Index Cond: ((user_info -& 'user_name'::text) ? '4_francs'::text)&Planning time: 0.151 ms&Execution time: 0.129 ms(7 rows)备注: 速度很快.五 对比 json 和 jsonb 的检索性能& & & &文档上提到了 jsonb 的检索效率要高于 json 的检索效率, 下面通过例子测试.& &&--5.1 删除之前创建的所有索引并创建函数索引francs=& create index idx_gin_user_info_id on tbl_user_json using btree (((user_info -&& 'id')::integer)); &CREATE INDEXfrancs=& create index idx_gin_user_infob_id on tbl_user_jsonb using btree (((user_info -&& 'id')::integer)); &CREATE INDEX备注: 为什么使用函数索引? &由于 --& 操作返回的是 text 类型, &接下来的查询会用到 id 字段比较, 需要转换成整型.--5.2 json 表范围扫描francs=& &explain analyze select id,user_info-&'id',user_info-&'user_name' from tbl_user_json where (user_info-&&'id')::int4 & '1' and (user_info-&&'id')::int4 & '10000';&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &--------------------------------------------------------------------------------------------------------------------------------------&Bitmap Heap Scan on tbl_user_json &(cost=190.94..22275.60 rows=10000 width=36) (actual time=2.417..60.585 rows=9998 loops=1)& &Recheck Cond: ((((user_info -&& 'id'::text))::integer & 1) AND (((user_info -&& 'id'::text))::integer & 10000))& &Heap Blocks: exact=167& &-& &Bitmap Index Scan on idx_gin_user_info_id &(cost=0.00..188.44 rows=10000 width=0) (actual time=2.329..2.329 rows=9998 loops=1)& & & & &Index Cond: ((((user_info -&& 'id'::text))::integer & 1) AND (((user_info -&& 'id'::text))::integer & 10000))&Planning time: 0.183 ms&Execution time: 64.116 ms(7 rows)--5.3 jsonb 表范围扫描francs=& &explain analyze select id,user_info-&'id',user_info-&'user_name' from tbl_user_jsonb where (user_info-&&'id')::int4 & '1' and (user_info-&&'id')::int4 & '10000';&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & QUERY PLAN & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&---------------------------------------------------------------------------------------------------------------------------------------&Bitmap Heap Scan on tbl_user_jsonb &(cost=190.94..23939.63 rows=10000 width=140) (actual time=2.593..24.308 rows=9998 loops=1)& &Recheck Cond: ((((user_info -&& 'id'::text))::integer & 1) AND (((user_info -&& 'id'::text))::integer & 10000))& &Heap Blocks: exact=197& &-& &Bitmap Index Scan on idx_gin_user_infob_id &(cost=0.00..188.44 rows=10000 width=0) (actual time=2.494..2.494 rows=9998 loops=1)& & & & &Index Cond: ((((user_info -&& 'id'::text))::integer & 1) AND (((user_info -&& 'id'::text))::integer & 10000))&Planning time: 0.142 ms&Execution time: 27.851 ms(7 rows)备注: 这里实验发现, jsonb 检索确实比 json 要快很多, &而本文开头插入数据时 jsonb 比 json 稍慢, 这也正好验证了 "jsonb 写入比 json 慢,但检索较 json 快的说法.", 我在之前的博客 && 有提到过.--六 附&&6.1&json&and&jsonb&OperatorsOperatorRight Operand TypeDescriptionExampleExample Result-&intGet JSON array element (indexed from zero)'[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json-&2{"c":"baz"}-&textGet JSON object field by key'{"a": {"b":"foo"}}'::json-&'a'{"b":"foo"}-&&intGet JSON array element as&text'[1,2,3]'::json-&&23-&&textGet JSON object field as&text'{"a":1,"b":2}'::json-&&'b'2#&text[]Get JSON object at specified path'{"a": {"b":{"c": "foo"}}}'::json#&'{a,b}'{"c": "foo"}#&&text[]Get JSON object at specified path as&text'{"a":[1,2,3],"b":[4,5,6]}'::json#&&'{a,2}'36.2&&Additional&jsonb&OperatorsOperatorRight Operand TypeDescriptionExample=jsonbAre the two JSON values equal?'[1,2,3]'::jsonb = '[1,2,3]'::jsonb@&jsonbDoes the left JSON value contain within it the right value?'{"a":1, "b":2}'::jsonb @& '{"b":2}'::jsonb&@jsonbIs the left JSON value contained within the right value?'{"b":2}'::jsonb &@ '{"a":1, "b":2}'::jsonb?textDoes the key/element&string&exist within the JSON value?'{"a":1, "b":2}'::jsonb ? 'b'?|text[]Do any of these key/element&strings&exist?'{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']?&text[]Do all of these key/element&strings&exist?'["a", "b"]'::jsonb ?& array['a', 'b']--七 参考
阅读(15191)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'PostgreSQL9.4:
性能测试',
blogAbstract:'& & & & & json 特性的提升是9.4 的关键特性之一, 本人对于 json 的关注较少, 一方面由于之前版本的 json 并不十分成熟, 使用时需要配合使用外部模块如 PLV8, PLPerl 来弥补 JSON 功能的不足,一方面由于太懒没花精力研究; 但 9.4 版本的 JSON 功能完善很多, &jsonb 的出现带来了更多的函数, 更多的索引创建方式, 更多的操作符和更高的性能. 接下来通过一些例子来讲解, 希望更多的朋友能够了解并测试 PostgreSQL 的 json 功能.一 环境信息--1.1 之前写的关于 json 的博客&',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:3,
publishTime:6,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}Q:什么是"JSON字符串",什么是"JSON对象",两者的区别?
a.JSON对象是直接可以使用JQuery操作的格式,如C#中可以用对象(类名)点出属性(方法)一样b.JSON字符串仅仅只是一个字符串,一个整体,不截取的话没办法取出其中存储的数据,不能直接使用,除非你只想alert()他;
var str2 = { "name": "deluyi", "sex": "man" };
JSON字符串:
var str1 = '{ "name": "deyuyi", "sex": "man" }';
Q:将"JSON字符串"转化为"JSON对象"的方法?
一:使用$.parseJSON(str)
//由JSON字符串转换为JSON对象
var str='{ "name": "John" }';
var obj = jQuery.parseJSON(str)
alert("1"+ obj.name);
ps:此种方式仅支持标准格式:var str='{ "name": "John" }';二:JSON.parse(str)
var str = '{ "name": "mady", "age": "24" }';
var obj = JSON.parse(str);
alert(obj.name);
ps:此种方式仅支持标准格式:var str='{ "name": "John" }';
三:使用eval('('+str+')');
//由JSON字符串转换为JSON对象
var str='{ "name": "John" }';
var obj = eval('(' + str + ')');
alert( obj.name);
var str2="{ 'name': 'John' }";
var obj2 = eval('(' + str2 + ')');
alert( obj2.name);
var str3="{ name: 'John' }";
var obj3 = eval('(' + str3 + ')');
alert( obj3.name);
以上均会输出结果&john&。
Eval方式可以转换以下标准和非标准格式字符串:
var str="{ 'name': 'John' }";
var str2='{ "name": "John" }';
var str3="{ name: 'John' }";
Q:将"JSON对象"转化为"JSON字符串"的方法?
一:使用全局方法JSON.stringify()与toJSONString()
var last=obj.toJSONString(); //将JSON对象转化为JSON字符
var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
alert(last);
Q:读取JSON对象的方法?
如上面的例子:
var str2 = { "name": "mady", "sex": "man" };
就可以这样读取:
alert(str2.name);//和C#一样直接往出点&
弹出& mady&。
我们遇到的JSON很少有这么简单的,比如复杂一点的JSON对象如:
var str={"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}};
alert(str.GetUserPostByIdResult.Name);//一次点不出来,我多点几次
弹出:&mady&。
阅读(...) 评论()有时候在做项目的时候时常将这两个概念弄混淆,尤其是在使用springmvc的时候,后台@RequestBody接受的是一个json格式的字符串,一定是一个字符串。&先介绍一下json对象,首先说到对象的概念,对象的属性是可以用:对象.属性进行调用的。例如:
var person={"name":"tom","sex":"男","age":"24"}
以上就是json对象。是一个用perosn.name这种方式进行属性的调用。第三行代码就是看person的类型,为object类型。
JSON字符串
字符串,我们常说的javascript中的字符串是单引号或者双引号引起来的。那么json字符串是什么概念呢?
var b='{"name":"2323","sex":"afasdf","age":"6262"}';//json字符串
console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"}
alert(typeof(b));//string
以上就是b就是一个字符串,也是一个json字符串,之所以叫json字符串,因为字符串的格式符合json的格式,所以叫做json字符串,第三行代码也匹配其中的类型为string。
json字符串和json对象的转换
json字符串转json对象,调用parse方法:
var b='{"name":"2323","sex":"afasdf","age":"6262"}'
json对象转为json字符串:
var a={"name":"tom","sex":"男","age":"24"}//json对象
var aToString=JSON.stringify(a);
console.log(aToString);//{"name":"tom","sex":"男","age":"24"}
Springmvc接受json字符串类型。
在有一段时间做项目,和朋友交流的时候,基于Rest风格的开发,一直理解为前端ajax提交的一个json对象,后台以@RequestBody接受的json对象,后来我发现自己错了,其实不然,前端传入后台的是一个json格式的字符串,下面有一个例子:
上面代码,首先push方法将其封装到数组中,其表现格式:
{"userName":"test","address":"gz"},
{"userName":"ququ","address":"gr"}
JSON.stringify(saveData)将其转换为json字符串:同时ajax请求的时候也要指定dataType: “json”,contentType:”application/json” 这样就可以轻易的将一个对象或者List传到Java端。&后台接受
@Controller
@RequestMapping(value = "saveUser", method=RequestMethod.POST )
@ResponseBody
public void saveUser(@RequestBody List&User& users) {
userService.batchSave(users);
后台用@RequestBody将其封装到List&User&中。然后进入Service层。
GET、POST方式提时, 根据request header Content-Type的值来判断:&application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);&multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);&其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理)。
阅读(...) 评论()

我要回帖

更多关于 shell解析json字段 的文章

 

随机推荐