首页 > Web开发 > 详细

集算器解析动态JSON入库

时间:2015-08-08 02:12:59      阅读:204      评论:0      收藏:0      [点我收藏+]

? 来源:http://bbs.csdn.net/topics/390611005?。

??系统采集的JSON格式数据(s.json)如下:

??{

??? “SUCCESS”: [

??????? {

??????????? "MESSAGE": "IMEI Service List",

??????????? "LIST": {

??????????????? "MOVISTAR SPAIN": {

??????????????????? "GROUPNAME": "MOVISTAR SPAIN",

??????????????????? "SERVICES": {

??????????????????????? "3": {

?????????? ?????????????????"SERVICEID": 32,

??????????????????????????? "SERVICENAME": "MOVISTAR NOKIA INSTANTE",

??????????????????????????? "CREDIT": 4,

??????????????????????????? "TIME": "1-30 Minutes",

??????????????????????????? "INFO": "<p style=\"text-align: center;\">…… </p>",

??????????????????????????? "Requires.Network": "None",

??????????????????????????? "Requires.Mobile": "None",

??????????????????????????? "Requires.Provider": "None",

??????????????????????????? "Requires.PIN": "None",

??????????????????????????? "Requires.KBH": "None",

??????????????????????????? "Requires.MEP": "None",

??????????????????????????? "Requires.PRD": "None",

??????????????????????????? "Requires.Type": "None",

??????????????????????????? "Requires.Locks": "None",

??????????????????????????? "Requires.Reference": "None"

??????????????????????? },

??????????????????????? "8": {

??????????????????????????? "SERVICEID": 77,

??????????????????????????? "SERVICENAME": "MOVISTAR NOKIA 20 NCK",

???????????????????? ???????"CREDIT": 12,

??????????????????????????? "TIME": "1-30 Minutes",

??????????????????????????? "INFO": "<p style=\"text-align: center;\">……</p>",

??????????????????????????? "Requires.Network": "None",

??????????????????????????? "Requires.Mobile": "None",

??????????????????????????? "Requires.Provider": "None",

??????????????????????????? "Requires.PIN": "None",

??????????????????????????? "Requires.KBH": "None",

??????????????????????????? "Requires.MEP": "None",

??????????????????????????? "Requires.PRD": "None",

??????????????????????????? "Requires.Type": "None",

??????????????????????????? "Requires.Locks": "None",

??????????????????????????? "Requires.Reference": "None"

??????????????????????? }

??????????????????? }

??????????????? },

??????????????? "VODAFONE SPAIN": {

??????????????????? "GROUPNAME": "VODAFONE SPAIN",

??????????????????? "SERVICES": {

??????????????????????? "5": {

??????????????????????????? "SERVICEID": 50,

??????????????????????????? "SERVICENAME": "VODAFONE NOKIA BB5 SL3",

??????????????????????????? "CREDIT": 5,

??????????????????????????? "TIME": "1-60 Minutes",

??????????????????????????? "INFO": "<p style=\"text-align: center;\">……</p>",

??????????????????????????? "Requires.Network": "None",

????????????????? ??????????"Requires.Mobile": "None",

??????????????????????????? "Requires.Provider": "None",

??????????????????????????? "Requires.PIN": "None",

??????????????????????????? "Requires.KBH": "None",

??????????????????????????? "Requires.MEP": "None",

????? ??????????????????????"Requires.PRD": "None",

??????????????????????????? "Requires.Type": "None",

??????????????????????????? "Requires.Locks": "None",

??????????????????????????? "Requires.Reference": "None"

??????????????????????? },

?????????????????? ?????"10": {

??????????????????????????? "SERVICEID": 95,

??????????????????????????? "SERVICENAME": "VODAFONE SONY&;SONY ERIC(RAPIDO)",

??????????????????????????? "CREDIT": 16,

??????????????????????????? "TIME": "1-24 Hours",

?????????????????????????? ?"INFO": "<p style=\"text-align: center;\">……</p>",

??????????????????????????? "Requires.Network": "None",

??????????????????????????? "Requires.Mobile": "None",

??????????????????????????? "Requires.Provider": "None",

??????????????????????????? "Requires.PIN": "None",

??????????????????????????? "Requires.KBH": "None",

??????????????????????????? "Requires.MEP": "None",

??????????????????????????? "Requires.PRD": "None",

??????????????????????????? "Requires.Type": "None",

??????????????????????? ????"Requires.Locks": "None",

??????????????????????????? "Requires.Reference": "None"

??????????????????????? }

??????????????????? }

??????????????? }

??????????? }

??????? }

??? ],

??? “apiversion”: “2.0.0″

}

??现需要按照JSON数据内容,将相应节点下的属性值更新到数据库的对应表中,需要更新的2张表如下:

Create table [dbo].[Groups]

(

? [ID] [int] IDENTITY(1,1) NOT NULL,?????????????????? –id

? [Groupname] [nvarchar] (50) not null default(”),??? –名称

? [groupid] [int] not null default(0),

?CONSTRAINT [PK_Groups_id] PRIMARY KEY CLUSTERED

(

??? [id] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

?

CREATE TABLE [dbo].[Services](

? [id]??? [int] IDENTITY(1,1) NOT NULL,????? ???????????????????????–id

? [Serviceid] [int] not null default(0),???????????

? [Servicename] [nvarchar] (50) not null default(”),???

? [groupid] [int] not null default(0),????????????????

? [Credit] [decimal] not null default(0.00),

? [Time] [nvarchar] (50) not null default(”),

? [INFO] [nvarchar] (3000) not null default(”),

? [Network] [nvarchar] (100) not null default(‘none’),

? [Mobile] [nvarchar] (100) not null default(‘none’),

? [Provider] [nvarchar] (100) not null default(‘none’),

? [PIN] [nvarchar] (100) not null default(‘none’),

? [KBH] [nvarchar] (100) not null default(‘none’),

? [MEP] [nvarchar] (100) not null default(‘none’),

? [PRD] [nvarchar] (100) not null default(‘none’),

? [Type] [nvarchar] (100) not null default(‘none’),

? [Locks] [nvarchar] (100) not null default(‘none’),

? [Reference] [nvarchar] (100) not null default(‘none’),

? [isstatus] [nvarchar] (1) not null default(’0′),

? [remark] [nvarchar] (255) not null default(”),

? [Pricingid] [int] not null default(0),

?CONSTRAINT [PK_Services_id] PRIMARY KEY CLUSTERED

(

??? [id] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

?

??其中SERVICES下属性名即为groupid,如3,5,8,10,剩余字段与属性名一一对应。由于LIST和SERVICES下的属性名不固定,给解析带来了难度。区别于一般高级语言,集算器支持动态数据结构和集合运算,实现并不困难,脚本如下:


bubuko.com,布布扣
?

? ?A1:以字符串读入JSON文件内容,并使用import@j()将字符串转为行列式带有层次的序表。


bubuko.com,布布扣
?

? ?A2-A3:根据两个目标表,创建空序表,用于存储解析结果后一次更新到数据库。

??A4-B4:循环序表,在B4中计算LIST下节点数量。

??B5-C6:循环获取LIST节点内容,并在C6中计算SERVICES下节点数量。


bubuko.com,布布扣
?

? ?C7-D8:循环获取SERVICES节点下属性名和属性值。


bubuko.com,布布扣
?

? ? D9-D10:将解析后结果分别写回到A2A3中的空序表中。


bubuko.com,布布扣
?

? ?A11:以groupid为主键,将A2内容更新到groups表中。


bubuko.com,布布扣
?

? ?A12:以Serviceid为主键,将A3内容更新到services表中。


bubuko.com,布布扣
?

集算器解析动态JSON入库

原文:http://datamachine.iteye.com/blog/2233468

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!