Android 解析JSON

	public void parseSearchDirectAllData(ArrayList<ProgrammeSite> programmeSites) {
		if(programmeSites != null) {
			
			programmeSites.clear();
			
			try {
		             JSONArray segsArray = new JSONArray(jsonString);
		       
		             if (segsArray != null)
					for (int k = 0; k < segsArray.length(); k++) {
						JSONObject segObject = segsArray.optJSONObject(k);
						if (segObject != null) {
							if(segObject.has("programmeSite")) {
								JSONArray jsonResultsArray = segObject.optJSONArray("programmeSite");
								if(jsonResultsArray != null && jsonResultsArray.length() > 0) {
									
									for (int i = 0; i < jsonResultsArray.length(); i++) {
										JSONObject jsonResultObject = jsonResultsArray.optJSONObject(i);
										if(jsonResultObject != null) {
											ProgrammeSite programmeSite = new ProgrammeSite();
											programmeSite.setSiteId(jsonResultObject.optInt("siteId"));
											
											if(jsonResultObject.has("episode")) {
												JSONArray jsonResultsSerisesArray = jsonResultObject.optJSONArray("episode");
												if(jsonResultsSerisesArray != null && jsonResultsSerisesArray.length() > 0) {
													
													ArrayList<Episode> episodes = new ArrayList<Episode>();
													
													for (int j = 0; j < jsonResultsSerisesArray.length(); j++) {
														JSONObject jsonResultSerisesObject = jsonResultsSerisesArray.optJSONObject(j);
														if(jsonResultSerisesObject != null) {
															Episode episode = new Episode();
															
															episode.setName(jsonResultSerisesObject.optString("name"));
															episode.setUrl(jsonResultSerisesObject.optString("url"));
															episodes.add(episode);
														}
													}
													
													programmeSite.setEpisodes(episodes);
												}
											}
											programmeSites.add(programmeSite);
										}
									}
									
								}
							}
						}
					}
			} catch (Exception e) {
				Logger.e(Youku.TAG_GLOBAL, "ParseJson#parseSearchDirectAllData()", e);
			}
		}
	}


上面代码输入JSON数据如下:

[
	"programmeSite": [
	      		 {//begin
				"id": 405642,
				"siteId": 17,
				"episode":[
				  	  {
						    "url": "http://www.letv.com/ptv/vplay/1606059.html",
						    "name": "黑狐01",
						    "seconds": 2701,
						    "orderId": 1,
						    "orderStage": 1,
						    "lastUpdate": 1376496000,
						    "viewOrder": 1,
						    "logo": "",
						    "tagType": 0,
						    "vid": 0,
				  	 },
				  	 {
						    "url": "http://www.letv.com/ptv/vplay/1427315.html",
						    "name": "黑狐02",
						    "seconds": 2699,
						    "orderId": 2,
						    "orderStage": 2,
						    "lastUpdate": 1376496000,
						    "viewOrder": 1,
						    "logo": "",
						    "tagType": 0,
						    "vid": 0,
				  	 },
	      	 		]
	    	        }//end
			{//begin
			       "id": 405715,
			       "siteId": 1,
			       "episode": [
					  {
					    "url": "http://www.tudou.com/albumplay/hLzWIWJyMXk/AHf67i8rVrg.html",
					    "name": "黑狐-第1集",
					    "seconds": 2699,
					    "orderId": 1,
					    "orderStage": 1,
					    "lastUpdate": 1386604800,
					    "viewOrder": 2,
					    "logo": "http://r3.ykimg.com/0543040850EB192B6A0A470A11CD1DA6",
					    "tagType": 0,
					    "vid": 130472276,

					  },
					  {
					    "url": "http://www.tudou.com/albumplay/hLzWIWJyMXk/XaJL16jobbE.html",
					    "name": "黑狐-第2集",
					    "seconds": 2699,
					    "orderId": 2,
					    "orderStage": 2,
					    "lastUpdate": 1387468800,
					    "viewOrder": 2,
					    "logo": "http://r3.ykimg.com/0543040850BE87EF6A0A442EFAD0048F",
					    "tagType": 0,
					    "vid": 130472277,
					  },
			      ]
		     }//end
	]
]



Android 解析JSON

上一篇:Android中 dip 和 px 的关系


下一篇:Android保存用户名和密码