六、通过JSONObject与JSONArray来解析json
我们可以通过JSONObject与
JSONArray
的getInt,getString,getDouble,getJSONArray,getJSONObject等函数来解析json.
以下是一个通过网络取得json文本,然后解析的示例。
示例3
public AppGuessResponse getAppListFromHttp ( Context mContext ){String url = "http://10.158.166.110:8080/AndroidServer/JsonServlet" ;AppGuessResponse res = new AppGuessResponse ();try{HttpReturn ret = getDataFromHttp ( url );if ( ret . getCode () == HttpStatus . SC_OK ){res . parseFrom ( ret . getBody ());}} catch ( Exception e ){Log . e ( tag , "" , e );}return res ;}public HttpReturn getDataFromHttp ( String url ) { /* HttpGet对象*/ HttpGet httpRequest = new HttpGet ( url ); int code = - 1 ; try { /* HttpClient对象*/ HttpClient httpClient = new DefaultHttpClient (); /* 获得HttpResponse对象*/ HttpResponse httpResponse = httpClient . execute ( httpRequest ); code = httpResponse . getStatusLine (). getStatusCode (); if ( code == HttpStatus . SC_OK ) { // 取得返回的数据 byte bytes [] = EntityUtils . toByteArray ( httpResponse . getEntity ()); return new BaseHttpReturn ( code , bytes ); } else { return new BaseHttpReturn ( code ); } } catch ( ClientProtocolException e ) { e . printStackTrace (); } catch ( IOException e ) { e . printStackTrace (); } return new BaseHttpReturn ( code ); }
interface HttpReturn{int getCode ();byte [] getBody ();}
class BaseHttpReturn implements HttpReturn{final int code ;final byte body [];BaseHttpReturn ( int code ){this . code = code ;this . body = null ;}BaseHttpReturn ( int code , byte bytes []){this . code = code ;this . body = bytes ;}@Overridepublic int getCode (){// TODO Auto-generated method stubreturn 0;}@Overridepublic byte [] getBody (){// TODO Auto-generated method stubreturn null;}}
interface ResponseParse{public void parseFrom ( byte [] bytes );public void parseFrom ( String str );}
class Application{String name ;String packageName ;String version ;int versionCode ;double price ;long size ;long downloadCount ;public long getDownloadCount (){return downloadCount ;}public void setDownloadCount ( long downloadCount ){this . downloadCount = downloadCount ;}public long getSize (){return size ;}public void setSize ( long size ){this . size = size ;}public String getName (){return name ;}public void setName ( String name ){this . name = name ;}public String getPackageName (){return packageName ;}public void setPackageName ( String packageName ){this . packageName = packageName ;}public String getVersion (){return version ;}public void setVersion ( String version ){this . version = version ;}public int getVersionCode (){return versionCode ;}public void setVersionCode ( int versionCode ){this . versionCode = versionCode ;}public double getPrice (){return price ;}public void setPrice ( double price ){this . price = price ;}}
class AppGuessResponse implements ResponseParse{private List < Application > mApplications = new ArrayList < Application >();private boolean mIsFinish = false ;private boolean mIsSuccess = false ;private int allCount = 0 ;private Date expireDate = new Date ( System . currentTimeMillis () + 24 * 60* 60 * 1000 );public Date getExpireDate (){return expireDate ;}public void setExpireDate ( Date expireDate ){this . expireDate = expireDate ;}public int getAllCount (){return allCount ;}public void setAllCount ( int allCount ){this . allCount = allCount ;}public boolean getIsSuccess (){return mIsSuccess ;}public Application getApplicationItem ( int i ){return mApplications . get ( i );}public int getApplicationItemCount (){return mApplications . size ();}public List < Application > getApplicationItemList (){return mApplications ;}public boolean isFinish (){return mIsFinish ;}@Overridepublic void parseFrom ( byte [] bytes ){// TODO Auto-generated method stub}@Overridepublic void parseFrom ( String strJson ){try{JSONObject jsonObject = new JSONObject ( strJson );if ( jsonObject . has ( "endpage" )){mIsFinish = jsonObject . getInt ( "endpage" ) == 0 ? true : false ;} else{mIsFinish = false ;}if ( jsonObject . has ( "allcount" )){allCount = jsonObject . getInt ( "allcount" );}if ( jsonObject . has ( "list" )){JSONArray jsonArray = jsonObject . getJSONArray ( "list" );if ( jsonArray . length () != 0 ){for ( int i = 0 ; i < jsonArray . length (); i ++){JSONObject jsonObject2 = jsonArray . getJSONObject ( i );Application app = new Application ();app . setName ( jsonObject2 . getString ( "name" ));app . setPackageName ( jsonObject2 . getString ( "packageName" ));app . setSize ( jsonObject2 . getLong ( "size" ));app . setPrice ( jsonObject2 . optDouble ( "price" , 0.0 ));app . setVersion ( jsonObject2 . getString ( "version" ));app . setVersionCode ( jsonObject2 . getInt ( "versioncode" ));if ( jsonObject2 . has ( "downloadCount" )){app . setDownloadCount ( jsonObject2 . optLong ("downloadCount" , 0 ));}mApplications . add ( app );}}}mIsSuccess = true ;} catch ( JSONException e ){mIsSuccess = false ;}}