package jsontobom;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import bom.BSMember;
import bom.BSShow;
import bom.BSShowCenter;
import brequest.BetaSeriesReq;

public class ConvJSONToBOM {

	public void getUserConnectionInfos(BSMember member) throws Exception{
		BetaSeriesReq bsReq = new BetaSeriesReq();
		String response = bsReq.connectBetaseriesAPI(member);
		JSONParser parserJSON = new JSONParser();
		Object jsonRes = parserJSON.parse(response);
		if (jsonRes.getClass() == JSONObject.class)
		{
			JSONObject objRes = ((JSONObject) jsonRes);
			String token = extractString(objRes,"token");
			System.out.println("token : "+token);
			member.setToken(token);
			JSONObject objUser = extractJSONObject(objRes, "user");
			long userId = extractLong(objUser, "id");
			System.out.println("userId : "+userId);
			member.setUserId(userId);
		}
	}
	
	public void getUserRemainingEpisodes(BSMember member) throws Exception{
		BetaSeriesReq bsReq = new BetaSeriesReq();
		String response = bsReq.getUsersEpisodesList(member);
		JSONParser parserJSON = new JSONParser();
		Object jsonRes = parserJSON.parse(response);
		if (jsonRes.getClass() == JSONObject.class)
		{
			JSONObject objRes = ((JSONObject) jsonRes);
			JSONArray shows = extractArray(objRes,"shows");
			System.out.println("There are "+shows.size()+" shows");
			for(int itShows = 0; itShows < shows.size(); itShows++){
				JSONObject show = ((JSONObject) shows.get(itShows));
				BSShow bomShow = new BSShow();
				bomShow.setId(extractLong(show, "id"));
				bomShow.setImdb_id(extractLong(show, "thetvdb_id"));
				bomShow.setTitle(extractString(show, "title"));
				BSShowCenter.INSTANCE.addShow(bomShow);
			}
			
		}
	}

	private JSONArray extractArray(JSONObject objRes, String key) {
		return (JSONArray) objRes.get(key);
	}

	private String extractString(JSONObject objRes, String key) {
		return (String) objRes.get(key);
	}
	
	private int extractInt(JSONObject objRes, String key) {
		return (int) objRes.get(key);
	}
	
	private long extractLong(JSONObject objRes, String key) {
		return (long) objRes.get(key);
	}
	
	private JSONObject extractJSONObject(JSONObject objRes, String key) {
		return (JSONObject) objRes.get(key);
	}
}

@@ -2029,19 +2029,23 @@
 return (
-int
+(Number
 ) objRes
@@ -2045,32 +2045,44 @@
  objRes.get(key)
+).intvalue()
 ;%0A%09%7D%0A%09%0A%09private 
@@ -2141,20 +2141,23 @@
 return (
-long
+(Number
 ) objRes
@@ -2157,32 +2157,44 @@
  objRes.get(key)
+).intvalue()
 ;%0A%09%7D%0A%09%0A%09private 

Update ConvJSONToBOM.java