1+ package com .titizz .jsonparser ;
2+ 3+ import com .titizz .jsonparser .model .JsonArray ;
4+ import com .titizz .jsonparser .model .JsonObject ;
5+ import org .apache .http .client .fluent .Request ;
6+ import org .apache .http .client .fluent .Response ;
7+ import org .apache .http .message .BasicNameValuePair ;
8+ import org .junit .Test ;
9+ 10+ import java .io .IOException ;
11+ import java .nio .charset .Charset ;
12+ import java .nio .file .Files ;
13+ import java .nio .file .Paths ;
14+ import java .util .ArrayList ;
15+ import java .util .List ;
16+ 17+ import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .assertFalse ;
19+ import static org .junit .Assert .assertTrue ;
20+ 21+ /**
22+ * Created by code4wt on 17/9/1.
23+ */
24+ public class JSONParserTest {
25+ 26+ @ Test
27+ public void fromJSON () throws Exception {
28+ String path = this .getClass ().getResource ("/music.json" ).getFile ();
29+ String json = new String (Files .readAllBytes (Paths .get (path )), "utf-8" );
30+ // String json = getJSON();
31+ 32+ JSONParser jsonParser = new JSONParser ();
33+ JsonObject jsonObject = (JsonObject ) jsonParser .fromJSON (json );
34+ System .out .println (jsonObject );
35+ 36+ JsonObject playlist = jsonObject .getJsonObject ("playlist" );
37+ assertEquals (52 , playlist .get ("commentCount" ));
38+ assertEquals (19208468137575293L , playlist .get ("coverImgId" ));
39+ assertEquals ("2017年八月最热新歌TOP50" , playlist .get ("name" ));
40+ assertFalse ((Boolean ) playlist .get ("highQuality" ));
41+ 42+ JsonArray trackIds = playlist .getJsonArray ("trackIds" );
43+ assertEquals (50 , trackIds .size ());
44+ JsonObject trackId = trackIds .getJsonObject (7 );
45+ assertEquals (499274374 , trackId .get ("id" ));
46+ assertEquals (14 , trackId .get ("v" ));
47+ 48+ JsonArray tracks = playlist .getJsonArray ("tracks" );
49+ JsonObject track3 = tracks .getJsonObject (3 );
50+ assertEquals ("带你去旅行" , track3 .get ("name" ));
51+ assertEquals (4 , track3 .get ("v" ));
52+ JsonObject track17 = tracks .getJsonObject (17 );
53+ assertEquals ("EVERYDAY" , track17 .get ("name" ));
54+ assertEquals (null , track17 .get ("a" ));
55+ assertEquals (5619229 , track17 .get ("mv" ));
56+ }
57+ 58+ @ Test
59+ public void fromJSON1 () throws Exception {
60+ String json = "{\" a\" : 1, \" b\" : \" b\" , \" c\" : {\" a\" : 1, \" b\" : null, \" d\" : [0.1, \" a\" , 1,2, 123, 1.23e+10, true, false, null]}}" ;
61+ JSONParser jsonParser = new JSONParser ();
62+ JsonObject jsonObject = (JsonObject ) jsonParser .fromJSON (json );
63+ System .out .println (jsonObject );
64+ 65+ assertEquals (1 , jsonObject .get ("a" ));
66+ assertEquals ("b" , jsonObject .get ("b" ));
67+ 68+ JsonObject c = jsonObject .getJsonObject ("c" );
69+ assertEquals (null , c .get ("b" ));
70+ 71+ JsonArray d = c .getJsonArray ("d" );
72+ assertEquals (0.1 , d .get (0 ));
73+ assertEquals ("a" , d .get (1 ));
74+ assertEquals (123 , d .get (4 ));
75+ assertEquals (1.23e+10 , d .get (5 ));
76+ assertTrue ((Boolean ) d .get (6 ));
77+ assertFalse ((Boolean ) d .get (7 ));
78+ assertEquals (null , d .get (8 ));
79+ }
80+ 81+ private String getJSON () throws IOException {
82+ String url = "http://music.163.com/weapi/v3/playlist/detail" ;
83+ List <BasicNameValuePair > params = new ArrayList <>();
84+ params .add (new BasicNameValuePair ("params" , "kJMudgZJvK8p8STuuxRpkUvO71Enw4C9y91PBkVTv2SMVnWG30eDKK1iAPcXnEah" ));
85+ params .add (new BasicNameValuePair ("encSecKey" , "d09b0b95b7d5b4e68aa7a16d6177d3f00a78bfa013ba59f309d41f18a2b4ea066cdea7863866b6283f403ddcd3bfb51f73f8ad3c6818269ceabff934a645196faf7a9aae0edde6e232b279fd495140e6252503291cf819eabbd9f3373648775201a70f179b7981d627257d3bba5a5e1b99d0732ce3e898db3614d82bcbe1a6a8" ));
86+ Response response = Request .Post (url )
87+ .bodyForm (params )
88+ .execute ();
89+ 90+ return response .returnContent ().asString (Charset .forName ("utf-8" ));
91+ }
92+ }
0 commit comments