您的位置:首页 > 编程语言 > Java开发

Java实现json+cookie购物车模块

2013-08-23 23:38 330 查看
一直听到和看到说json好用的,在以前的项目里也只是看别人用过,自己没亲自动手写过json的处理。

一个电商系统很经典的模块——购物车,一般实现起来有cookie、session和数据库三种方式。原来用cookie存储购物车信息时,字符串的拼接修改是个比较麻烦的事,都要自己写原生的js代码,和容易出错。

这次在网上查阅了一些资料以后,决定用json+cookie来实现购物车的功能,代码及注意事项记录如下。

首先是一个非常简单的product类:

product类

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package

test
;

public

class

Product

{

private

String

id
;

private

int

num
;

public

String

getId
(
)

{

return

id
;

}

public

void

setId
(
String

id
)

{

this
.
id

=

id
;

}

public

int

getNum
(
)

{

return

num
;

}

public

void

setNum
(
int

num
)

{

this
.
num

=

num
;

}

}

接下来就是json的操作类,定义了几个购物车常用的方法:

购物车操作类,处理json

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

/**

* @author VikingZ

* @version0.2 2013-08-21

*/

package

test
;

import

java
.
util
.
ArrayList
;

import

java
.
util
.
List
;

import

net
.
sf
.
json
.
JSONArray
;

import

net
.
sf
.
json
.
JSONObject
;

public

class

TestJson

{

/**

* @param args

*/

private

static

final

String

KEY_ID

=

"id"
;

private

static

final

String

KEY_NUM

=

"num"
;

/**

* add data into json

*

* @param id

* @param num

* @param json

* @return

*/

public

String

add
(
String

id
,

int

num
,

String

json
)

{

if

(
isNull
(
id
)
)

{

return

null
;

}

String

keyId

=

id
.
trim
(
)
;

int

temp_index

=

queryIndex
(
keyId
,

json
)
;

List
<Product>

temp_list

=

query
(
json
)
;

Product

p

=

new

Product
(
)
;

p
.
setId
(
keyId
)
;

p
.
setNum
(
num
)
;

if

(
temp_list

==

null
)

{

temp_list

=

new

ArrayList
<Product>
(
)
;

temp_list
.
add
(
p
)
;

}

else

{

if

(
temp_index

!=

-
1

&&

temp_index

!=

-
2
)

{

int

temp_num

=

temp_list
.
get
(
temp_index
)
.
getNum
(
)
;

temp_list
.
get
(
temp_index
)
.
setNum
(
temp_num

+

num
)
;

}

else

{

temp_list
.
add
(
p
)
;

}

}

return

writeJson
(
temp_list
)
;

}

/**

* minus from json

*

* @param id

* @param num default: num=1

* @param json

* @return

*/

public

String

reduce
(
String

id
,

String

json
)

{

String

keyId

=

id
.
trim
(
)
;

int

temp_index

=

queryIndex
(
keyId
,

json
)
;

List
<Product>

temp_list

=

query
(
json
)
;

if

(
temp_list

==

null
)

{

return

json
;

}

else

{

if

(
temp_index

!=

-
1

&&

temp_index

!=

-
2
)

{

int

temp_num

=

temp_list
.
get
(
temp_index
)
.
getNum
(
)
;

if

(
temp_num

<=

1
)

{

temp_list
.
remove
(
temp_index
)
;

}

else

{

temp_list
.
get
(
temp_index
)
.
setNum
(
temp_num

-

1
)
;

}

}

}

return

writeJson
(
temp_list
)
;

}

/**

* modfiy json

*

* @param id

* @param num

* @param json

* @return

*/

public

String

modify
(
String

id
,

int

num
,

String

json
)

{

String

keyId

=

id
.
trim
(
)
;

int

valueNum

=

num
;

int

temp_index

=

queryIndex
(
keyId
,

json
)
;

List
<Product>

temp_list

=

query
(
json
)
;

if

(
temp_list

==

null
)

{

return

json
;

}

else

{

if

(
temp_index

!=

-
1

&&

temp_index

!=

-
2
)

{

temp_list
.
get
(
temp_index
)
.
setNum
(
valueNum
)
;

}

}

return

writeJson
(
temp_list
)
;

}

/**

* remove one product

*

* @param id

* @param json

* @return

*/

public

String

remove
(
String

id
,

String

json
)

{

String

keyId

=

id
.
trim
(
)
;

int

temp_index

=

queryIndex
(
keyId
,

json
)
;

List
<Product>

temp_list

=

query
(
json
)
;

if

(
temp_list

==

null
)

{

return

json
;

}

else

{

if

(
temp_index

!=

-
1

&&

temp_index

!=

-
2
)

{

temp_list
.
remove
(
temp_index
)
;

}

}

return

writeJson
(
temp_list
)
;

}

/**

* query json

*

* @param json

* @return

*/

public

List
<Product>

query
(
String

json
)

{

if

(
isNull
(
json
)
)

{

return

null
;

}

List
<Product>

temp_list

=

new

ArrayList
<Product>
(
)
;

JSONArray
array

=

JSONArray
.
fromObject
(
json
)
;

for

(
int

i

=

0
;

i

<

array
.
size
(
)
;

i
++
)

{

JSONObject
object

=

JSONObject
.
fromObject
(
array
.
get
(
i
)
)
;

Product

p

=

new

Product
(
)
;

p
.
setId
(
object
.
getString
(
KEY_ID
)
)
;

p
.
setNum
(
object
.
getInt
(
KEY_NUM
)
)
;

temp_list
.
add
(
p
)
;

}

if

(
(
temp_list

!=

null

&&

temp_list
.
size
(
)

==

0
)

||

temp_list

==

null
)

{

return

null
;

}

return

temp_list
;

}

/**

* json to string

*

* @param list

* @return

*/

public

String

writeJson
(
List
<Product>

list
)

{

if

(
list

==

null
)

{

return

null
;

}

JSONArray
array

=

JSONArray
.
fromObject
(
list
)
;

return

array
.
toString
(
)
;

}

/**

* get the index from json

*

* @param id

* @param json

* @return

*/

public

int

queryIndex
(
String

id
,

String

json
)

{

String

temp_id

=

id
.
trim
(
)
;

if

(
json

==

null

||

temp_id

==

null
)

{

return

-
1
;

}

JSONArray
array

=

JSONArray
.
fromObject
(
json
)
;

for

(
int

i

=

0
;

i

<

array
.
size
(
)
;

i
++
)

{

JSONObject
object

=

JSONObject
.
fromObject
(
array
.
get
(
i
)
)
;

if

(
object
.
getString
(
KEY_ID
)
.
equals
(
temp_id
)
)

{

return

i
;

}

}

return

-
2
;

}

/**

* delete all

*

* @param json

* @return

*/

public

String

removeAll
(
String

json
)

{

return

writeJson
(
null
)
;

}

/**

* test null

*

* @param str

* @return

*/

private

static

boolean

isNull
(
String

str
)

{

if

(
str

==

null

||

""
.
equals
(
str
)

||

null

==

str
.
trim
(
)
)

{

return

true
;

}

return

false
;

}

public

static

void

main
(
String
[
]

args
)

{

TestJson
testJson

=

new

TestJson
(
)
;

List
<Product>

list

=

new

ArrayList
<Product>
(
)
;

Product
p1

=

new

Product
(
)
;

p1
.
setId
(
"198954"
)
;

p1
.
setNum
(
1
)
;

list
.
add
(
p1
)
;

Product
p2

=

new

Product
(
)
;

p2
.
setId
(
"301774"
)
;

p2
.
setNum
(
1
)
;

list
.
add
(
p2
)
;

JSONArray
array

=

JSONArray
.
fromObject
(
list
)
;

String

xxx

=

array
.
toString
(
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
modify
(
"301774"
,

3
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
reduce
(
"198954"
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
reduce
(
"301774"
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
add
(
"33333"
,

6
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
remove
(
"301774"
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
removeAll
(
xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

xxx

=

testJson
.
add
(
"301774"
,

1
,

xxx
)
;

System
.
out
.
println
(
"Cart: "

+

xxx
)
;

}

}

测试代码的main方法运行结果如下:

运行结果,于操作相符,成功!

Default

1

2

3

4

5

6

7

8

Cart
:

[
{
"id"
:
"198954"
,
"num"
:
1
}
,
{
"id"
:
"301774"
,
"num"
:
1
}
]

Cart
:

[
{
"id"
:
"198954"
,
"num"
:
1
}
,
{
"id"
:
"301774"
,
"num"
:
3
}
]

Cart
:

[
{
"id"
:
"301774"
,
"num"
:
3
}
]

Cart
:

[
{
"id"
:
"301774"
,
"num"
:
2
}
]

Cart
:

[
{
"id"
:
"301774"
,
"num"
:
2
}
,
{
"id"
:
"33333"
,
"num"
:
6
}
]

Cart
:

[
{
"id"
:
"33333"
,
"num"
:
6
}
]

Cart
:

null

Cart
:

[
{
"id"
:
"301774"
,
"num"
:
1
}
]

到这里,一个简单实用的json购物车模块就写好了,需要cookie存储的话,只要把返回的String字符串写到cookie里就可以了。

json需要引入的jar包有:

commons-lang.jar

commons-beanutils.jar

commons-collections.jar

commons-logging.jar

ezmorph.jar

json-lib-2.4-jdk15.jar

附上网盘的下载地址:http://vdisk.weibo.com/s/ABqatf7qNDXU

运行测试过程中碰到的问题如下:

提示如下错误:Exception in thread “main” net.sf.json.JSONException: java.lang.NoSuchMethodException: Property ‘id’ has no getter method
可我明明写了get、set方法的呀!解决方法:声明bean为public class xxx,必须是public,开始我用默认类型(class xxx)所以不行。

这就是我对json-lib包的初次尝试,写下来方便自己日后查看,也希望对于想尝试json和做购物车模块的新手能有所帮助。

原文首发于我的博客 http://www.lovelx.com/808.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json 购物车