Back to project page Grocery-Mate.
The source code is released under:
Apache License
If you think the Android project Grocery-Mate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2011 iMellon/*from w w w .j a v a2 s . c o m*/ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.imellon.android.grocerymate.io; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.imellon.android.grocerymate.model.CompanyPoiPrice; import com.imellon.android.grocerymate.model.CompanyPoiPriceList; import com.imellon.android.grocerymate.model.Poi; import com.imellon.android.grocerymate.model.PoiList; import com.imellon.android.grocerymate.model.PoiProductPrice; import com.imellon.android.grocerymate.model.PoiProductPriceList; import com.imellon.android.grocerymate.model.Product; import com.imellon.android.grocerymate.model.ProductList; import com.imellon.android.grocerymate.model.TerritoryList; import android.util.Log; /** * @author Christos Papazafeiropoulos, Dimitris Makris */ public class JsonParser { private static final String TAG = JsonParser.class.getSimpleName(); public static final int MAX_JSON_OBJECTS = 1000; public static Poi parsePoi(String result) throws JSONException{ JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; Poi p = new Poi(); if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { //Log.i(TAG, "processing Hag JSON Data Array"); int top = Math.min(MAX_JSON_OBJECTS, dataArray.length()); for (int i = 0; i < top; i++) { jo = dataArray.getJSONObject(i); p = parsePoiObject(jo); } } return p; } public static Poi parsePoiObject(JSONObject jo) throws JSONException{ Poi p = new Poi(); if(jo.has("PoiId") && !jo.isNull("PoiId")){ Log.d(TAG, "PoiId: "+jo.getString("PoiId")); p.poiId = jo.getString("PoiId"); } if(jo.has("Name") && !jo.isNull("Name")){ Log.d(TAG, "Name: "+jo.getString("Name")); p.poiName = jo.getString("Name"); } if(jo.has("Address") && !jo.isNull("Address")){ Log.d(TAG, "Address: "+jo.getString("Address")); p.address = jo.getString("Address"); } if(jo.has("Source") && !jo.isNull("Source")){ Log.d(TAG, "Source: "+jo.getString("Source")); p.source = jo.getString("Source"); } return p; } public static PoiList parsePoiList(String result) throws JSONException{ JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; PoiList pl = new PoiList(); if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { for (int i = 0; i < dataArray.length(); i++) { jo = dataArray.getJSONObject(i); pl.list.add(parsePoiObject(jo)); } return pl; } return null; } public static TerritoryList parseTerritoryList(String result) throws JSONException{ JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; TerritoryList tl = new TerritoryList(); if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { for (int i = 0; i < dataArray.length(); i++) { jo = dataArray.getJSONObject(i); String name = ""; if(jo.has("Name") && !jo.isNull("Name")){ name = jo.getString("Name"); } String id = ""; if(jo.has("TerritoryId") && !jo.isNull("TerritoryId")){ id = jo.getString("TerritoryId"); } tl.list.put(name, id); } return tl; } return null; } public static ProductList parseProductsList(String result) throws JSONException{ JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; ProductList pl = new ProductList(); if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { //Log.i(TAG, "processing Hag JSON Data Array"); int top = Math.min(MAX_JSON_OBJECTS, dataArray.length()); for (int i = 0; i < top; i++) { jo = dataArray.getJSONObject(i); pl.product_list.add(parseSingleProductFromList(jo)); } } return pl; } private static Product parseSingleProductFromList(JSONObject jo) throws JSONException{ Product p = new Product(); if(jo.has("ProductId") && !jo.isNull("ProductId")){ Log.d(TAG, "ProductId: "+jo.getString("ProductId")); p.ProductId = jo.getString("ProductId"); } if(jo.has("Name") && !jo.isNull("Name")){ Log.d(TAG, "Name: "+jo.getString("Name")); p.Name = jo.getString("Name"); } if(jo.has("ProductCompanyId") && !jo.isNull("ProductCompanyId")){ Log.d(TAG, "ProductCompanyId: "+jo.getString("ProductCompanyId")); p.ProductCompanyId = jo.getString("ProductCompanyId"); } if(jo.has("CategoryId") && !jo.isNull("CategoryId")){ Log.d(TAG, "CategoryId: "+jo.getString("CategoryId")); p.CategoryId = jo.getString("CategoryId"); } if(jo.has("Source") && !jo.isNull("Source")){ Log.d(TAG, "Source: "+jo.getString("Source")); p.Source = jo.getString("Source"); } if(jo.has("SourceId") && !jo.isNull("SourceId")){ Log.d(TAG, "SourceId: "+jo.getString("SourceId")); p.SourceId = jo.getString("SourceId"); } Log.d(TAG, "------------------------------"); return p; } public static PoiProductPriceList parseProductPricesList(String result) throws JSONException{ JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; PoiProductPriceList pppl = new PoiProductPriceList(); if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { //Log.i(TAG, "processing Hag JSON Data Array"); int top = Math.min(MAX_JSON_OBJECTS, dataArray.length()); for (int i = 0; i < top; i++) { jo = dataArray.getJSONObject(i); pppl.product_prices.add(parseSinglePoiProductPrice(jo)); } } return pppl; } private static PoiProductPrice parseSinglePoiProductPrice(JSONObject jo) throws JSONException{ PoiProductPrice ppp = new PoiProductPrice(); if(jo.has("PoiProductPriceId") && !jo.isNull("PoiProductPriceId")){ Log.d(TAG, "PoiProductPriceId: "+jo.getString("PoiProductPriceId")); ppp.PoiProductPriceId = jo.getString("PoiProductPriceId"); } if(jo.has("PoiId") && !jo.isNull("PoiId")){ Log.d(TAG, "PoiId: "+jo.getString("PoiId")); ppp.PoiId = jo.getString("PoiId"); } if(jo.has("ProductId") && !jo.isNull("ProductId")){ Log.d(TAG, "ProductId: "+jo.getString("ProductId")); ppp.ProductId = jo.getString("ProductId"); } if(jo.has("Price") && !jo.isNull("Price")){ Log.d(TAG, "Price: "+jo.getString("Price")); ppp.Price = jo.getString("Price"); } Log.d(TAG, "------------------------------"); return ppp; } private static void parseTerritories(String result) throws JSONException { } public static CompanyPoiPriceList parseCompanyPoiPriceList(String result) throws JSONException { JSONObject root = new JSONObject(result); JSONObject jo = null; JSONArray dataArray = null; CompanyPoiPriceList pppl = null; if (root.has("d")) dataArray = root.getJSONArray("d"); else throw new JSONException("Malformed Response"); if (dataArray != null) { for (int i = 0; i < dataArray.length(); i++) { jo = dataArray.getJSONObject(i); pppl = parseSinglePoiProductPriceWithCompany(jo); } } return pppl; } private static CompanyPoiPriceList parseSinglePoiProductPriceWithCompany(JSONObject jo) throws JSONException{ CompanyPoiPriceList pppl = new CompanyPoiPriceList(); if(jo.has("PoiProductPrice") && !jo.isNull("PoiProductPrice")){ JSONArray poiProductPriceArray = jo.getJSONArray("PoiProductPrice"); if(poiProductPriceArray != null){ for(int j=0; j < poiProductPriceArray.length(); j++){ JSONObject onePoiProductPriceArrayItem = poiProductPriceArray.getJSONObject(j); CompanyPoiPrice cpp = new CompanyPoiPrice(); if(onePoiProductPriceArrayItem!= null){ if(onePoiProductPriceArrayItem.has("ProductId") && !onePoiProductPriceArrayItem.isNull("ProductId")){ cpp.product_id = onePoiProductPriceArrayItem.getString("ProductId"); } if(onePoiProductPriceArrayItem.has("Price") && !onePoiProductPriceArrayItem.isNull("Price")){ String price = onePoiProductPriceArrayItem.getString("Price"); cpp.price = Double.parseDouble(price); } if(onePoiProductPriceArrayItem.has("Poi") && !onePoiProductPriceArrayItem.isNull("Poi")){ JSONObject poiObject = onePoiProductPriceArrayItem.getJSONObject("Poi"); if(poiObject != null){ if(poiObject.has("PoiCompany") && !poiObject.isNull("PoiCompany")){ JSONObject poiCompany = poiObject.getJSONObject("PoiCompany"); if(poiCompany != null){ if(poiCompany.has("PoiCompanyId") && !poiCompany.isNull("PoiCompanyId")){ int companyId = poiCompany.getInt("PoiCompanyId"); String companyName = poiCompany.getString("Name"); cpp.company_name = companyName; } } } } } } if(!pppl.checkIfAlreadyIn(cpp)){ pppl.list.add(cpp); } } } } return pppl; } }