package androidapp.smartshopper.smartshopper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by JohnS on 2016-10-23.
 */

public class JsonParser {
    private String COLLECTIONS_TAG = "collections";
    private String COLUMN_TAG = "col_name_1";
    private String NAME_TAG = "name";
    private String PRICE_TAG = "price";
    private String STORE_TAG = "store";
    private String URL_TAG = "image url";

    public JsonParser() {}

    public List<Product> parseJSON(String json) {
        if(json != null) {
            List<Product> parseList = new ArrayList<Product>();

            try{
                JSONObject jsonObj = new JSONObject(json);
                JSONArray collections = jsonObj.getJSONArray(COLLECTIONS_TAG);

                for(int i = 0; i < collections.length(); i++) {
                    JSONObject currColle = collections.getJSONObject(i);

                    Iterator<?> colleKeys = currColle.keys();
                    while(colleKeys.hasNext()){
                        JSONArray items = currColle.getJSONArray(colleKeys.next().toString());
                        for(int j = 0; j < items.length(); j++) {
                            JSONObject currItem = items.getJSONObject(j);
                            String name = currItem.getString(NAME_TAG);
                            String price = currItem.getString(PRICE_TAG);
                            String store = currItem.getString(STORE_TAG);
                            String img = currItem.getString(URL_TAG);

                            Product currProduct = new Product(name, price, store, img);
                            parseList.add(currProduct);
                        }
                    }
                }
            }catch (JSONException e) {
                e.printStackTrace();
            }

            return parseList;
        }
        else {
            return null;
        }
    }
}

@@ -299,37 +299,25 @@
 ing 
-COLLECTIONS_TAG = %22collection
+ITEMS_TAG = %22item
 s%22;%0A
@@ -795,26 +795,20 @@
 ONArray 
-collection
+item
 s = json
@@ -828,18 +828,12 @@
 ray(
-COLLECTION
+ITEM
 S_TA
@@ -872,26 +872,20 @@
  0; i %3C 
-collection
+item
 s.length
@@ -907,32 +907,34 @@
                 
+//
 JSONObject currC
@@ -970,17 +970,16 @@
 ect(i);%0A
-%0A
         
@@ -994,50 +994,54 @@
     
-Iterator%3C?%3E colleKeys = currColle.keys(
+JSONArray currColle = items.getJSONArray(i
 );%0A
+%0A
     
@@ -1060,135 +1060,52 @@
     
-while(colleKeys.hasNext())%7B%0A                        JSONArray items = currColle.getJSONArray(colleKeys.next().toString());%0A    
+//Iterator%3C?%3E colleKeys = currColle.keys();%0A
     
@@ -1139,21 +1139,25 @@
  0; j %3C 
-items
+currColle
 .length(
@@ -1163,23 +1163,18 @@
 (); j++)
- 
 %7B%0A
-    
         
@@ -1211,21 +1211,25 @@
 rItem = 
-items
+currColle
 .getJSON
@@ -1239,20 +1239,17 @@
 ect(j);%0A
-    
+%0A
         
@@ -1300,36 +1300,32 @@
 ring(NAME_TAG);%0A
-    
                 
@@ -1398,28 +1398,24 @@
             
-    
 String store
@@ -1440,36 +1440,32 @@
 ing(STORE_TAG);%0A
-    
                 
@@ -1539,20 +1539,16 @@
         
-    
 Product 
@@ -1623,20 +1623,16 @@
         
-    
 parseLis
@@ -1655,34 +1655,8 @@
 t);%0A
-                        %7D%0A
     

parser needs fixin