Java實現json+cookie購物車模塊

一直聽到和看到說json好用的,在之前的項目裏也只是看別人用過,本身沒親自動手寫過json的處理。 html

一個電商系統很經典的模塊——購物車,通常實現起來有cookie、session和數據庫三種方式。原來用cookie存儲購物車信息時,字符串的拼接修改是個比較麻煩的事,都要本身寫原生的js代碼,和容易出錯。 java

此次在網上查閱了一些資料之後,決定用json+cookie來實現購物車的功能,代碼及注意事項記錄以下。 數據庫

首先是一個很是簡單的product類: json

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的操做類,定義了幾個購物車經常使用的方法: cookie

購物車操做類,處理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方法運行結果以下: session

運行結果,於操做相符,成功!
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包有: this

  1. commons-lang.jar
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. ezmorph.jar
  6. json-lib-2.4-jdk15.jar

附上網盤的下載地址:http://vdisk.weibo.com/s/ABqatf7qNDXU spa

運行測試過程當中碰到的問題以下: .net

提示以下錯誤: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

相關文章
相關標籤/搜索