现在的位置: 首页 > 综合 > 正文

JSON数据解析代码示例

2018年05月01日 ⁄ 综合 ⁄ 共 1428字 ⁄ 字号 评论关闭
public static  StartAPKInfo parseStartAPKJson(InputStream inputStream)
	{
		Log.i(TAG,  "parsePlaylistJson ");
		StartAPKInfo info = new StartAPKInfo();
		
		StringBuffer out = new StringBuffer();
		
		     byte[] b = new byte[4096];
		  
		     try {
				for (int n; (n = inputStream.read(b)) != -1;) {
				  out.append(new String(b, 0, n));
				 }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		
		     String str = out.toString();

		JSONObject obj = null;
		Iterator item = null;
		try {
			obj = new JSONObject(str);
			item = obj.keys();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
		
         
		while (item.hasNext()) {
			try {
				String key = (String) item.next();
				String s = obj.getString(key);
				if (key.equalsIgnoreCase("priority")) {
					info.priority = s;
				} 
				else if (key.equalsIgnoreCase("category"))
				{
					info.category= s;
				}
				else if (key.equalsIgnoreCase("source"))
				{
					info.source = s;
				}
				else if (key.equalsIgnoreCase("source_package"))
				{
					info.source_package = s;
				}
				else if (key.equalsIgnoreCase("video_tag"))
				{
					info.video_tag = s;
				}
				else if (key.equalsIgnoreCase("title"))
				{
					info.title = s;
				}
				else if (key.equalsIgnoreCase("img_url"))
				{
					info.img_url = s;
				}
				else if (key.equalsIgnoreCase("callback_action"))
				{
					info.callback_action = s;
				}
				else if (key.equalsIgnoreCase("callback_method"))
				{
					info.callback_method = s;
				}
				else if (key.equalsIgnoreCase("callback_flag"))
				{
					info.callback_flag = s;
				}
				else if (key.equalsIgnoreCase("callback_key"))
				{
					info.callback_key = s;
				}
				else if (key.equalsIgnoreCase("callback_value"))
				{
					info.callback_value = s;
				}
			} catch (JSONException e) {

				e.printStackTrace();
			}

		}

		return info;
	}

抱歉!评论已关闭.