Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

In this page you can find the example usage for org.json JSONArray length.

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.InviteToSharedAppFeedObj.java

public void handleDirectMessage(Context context, Contact from, JSONObject obj) {
    try {/*from www . j  ava2 s  .  c o m*/
        String packageName = obj.getString(PACKAGE_NAME);
        String feedName = obj.getString("sharedFeedName");
        JSONArray ids = obj.getJSONArray(PARTICIPANTS);
        Intent launch = new Intent();
        launch.setAction(Intent.ACTION_MAIN);
        launch.addCategory(Intent.CATEGORY_LAUNCHER);
        launch.putExtra("type", "invite_app_feed");
        launch.putExtra("creator", false);
        launch.putExtra("sender", from.id);
        launch.putExtra("sharedFeedName", feedName);
        launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        long[] idArray = new long[ids.length()];
        for (int i = 0; i < ids.length(); i++) {
            idArray[i] = ids.getLong(i);
        }
        launch.putExtra("participants", idArray);
        launch.setPackage(packageName);
        final PackageManager mgr = context.getPackageManager();
        List<ResolveInfo> resolved = mgr.queryIntentActivities(launch, 0);
        if (resolved.size() == 0) {
            Toast.makeText(context, "Could not find application to handle invite.", Toast.LENGTH_SHORT).show();
            return;
        }
        ActivityInfo info = resolved.get(0).activityInfo;
        launch.setComponent(new ComponentName(info.packageName, info.name));
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launch,
                PendingIntent.FLAG_CANCEL_CURRENT);

        (new PresenceAwareNotify(context)).notify("New Invitation from " + from.name,
                "Invitation received from " + from.name, "Click to launch application: " + packageName,
                contentIntent);
    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
    }
}

From source file:com.timrae.rikaidroid.MainActivity.java

private void addKuromojiWebItems(StringBuilder builder, JSONArray tokens) {
    for (int i = 0; i < tokens.length(); i++) {
        String reading;//from  ww w.  ja v  a 2 s  . com
        String surface;
        try {
            JSONObject token = tokens.getJSONObject(i);
            //base = token.getString("base");
            reading = token.getString("reading");
            surface = token.getString("surface");

            String formattedText;
            if (hasKanji(surface)) {
                // Try to join the surface form with any particles following it
                if (i < tokens.length() - 1) {
                    JSONObject nextToken = tokens.getJSONObject(i);
                    String pos = nextToken.getString("pos");
                    if (pos.contains("") || pos.contains("")) {
                        surface += nextToken.getString("surface");
                        i++;
                    }
                }
                formattedText = makeFurigana(surface, katToHira(reading));
                if (isAedictPresent(this)) {
                    formattedText = String.format(INTENT_URL, token.getString("base"), formattedText);
                }
            } else {
                formattedText = surface;
            }

            builder.append(formattedText);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.skalski.raspberrycontrol.Activity_GPIO.java

Handler getClientHandler() {

    return new Handler() {
        @Override//from w ww.  j  av  a2  s  .  c o  m
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            JSONObject root;
            JSONArray gpios;
            gpioArray = new ArrayList<Custom_GPIOAdapter>();
            gpioLayout.setRefreshing(false);

            Log.i(LOGTAG, LOGPREFIX + "new message received from server");

            try {

                root = new JSONObject(msg.obj.toString());

                if (root.has(TAG_ERROR)) {

                    String err = getResources().getString(R.string.com_msg_3) + root.getString(TAG_ERROR);
                    Log.e(LOGTAG, LOGPREFIX + root.getString(TAG_ERROR));
                    toast_connection_error(err);

                } else {

                    gpios = root.getJSONArray(TAG_GPIOSTATE);

                    for (int i = 0; i < gpios.length(); i++) {

                        JSONObject gpioss = gpios.getJSONObject(i);

                        int gpio = gpioss.getInt(TAG_GPIO);
                        int value = gpioss.getInt(TAG_VALUE);
                        String direction = gpioss.getString(TAG_DIRECTION);

                        gpioArray.add(new Custom_GPIOAdapter(gpio, value, direction));
                    }

                    if (gpios.length() == 0) {
                        Log.w(LOGTAG, LOGPREFIX + "can't find exported GPIO's on server side");
                        toast_connection_error(getResources().getString(R.string.error_msg_8));
                    }

                    if (gpioPinout.getDrawable().getConstantState() == getResources()
                            .getDrawable(R.drawable.gpio_unknown).getConstantState()) {
                        if (root.has(TAG_REVISION)) {
                            String revision;
                            revision = root.getString(TAG_REVISION);

                            Log.i(LOGTAG, LOGPREFIX + "set new GPIO layout image");

                            if (revision.equals("0002") || revision.equals("0003")) {

                                gpioPinout.setImageResource(R.drawable.gpio_pinout_1);

                            } else if (revision.equals("0004") || revision.equals("0005")
                                    || revision.equals("0006") || revision.equals("0007")
                                    || revision.equals("0008") || revision.equals("0009")
                                    || revision.equals("000d") || revision.equals("000e")
                                    || revision.equals("000f")) {

                                gpioPinout.setImageResource(R.drawable.gpio_pinout_2);

                            } else if (revision.equals("0010") || revision.equals("0011")) {

                                gpioPinout.setImageResource(R.drawable.gpio_pinout_3);

                            } else {
                                Log.wtf(LOGTAG, LOGPREFIX + "your Raspberry Pi board is weird");
                            }
                        }
                    }
                }

            } catch (Exception ex) {
                Log.e(LOGTAG, LOGPREFIX + "received invalid JSON object");
                toast_connection_error(getResources().getString(R.string.error_msg_2));
            }

            setListAdapter(new Custom_GPIOArrayAdapter(getApplicationContext(), gpioArray));
        }
    };
}

From source file:org.klnusbaum.udj.network.ServerConnection.java

private static List<String> toStringList(final JSONArray array) throws JSONException {
    ArrayList<String> toReturn = new ArrayList();
    for (int i = 0; i < array.length(); ++i) {
        toReturn.add(array.getString(i));
    }/*from   w  ww  . j a  v a 2s.  c  o  m*/
    return toReturn;
}

From source file:uk.ac.imperial.presage2.core.cli.run.ExecutorModule.java

/**
 * <p>// www  .ja v  a2s  .  c om
 * Load an {@link AbstractModule} which can inject an
 * {@link ExecutorManager} with the appropriate {@link SimulationExecutor}s
 * as per provided configuration.
 * </p>
 * 
 * <p>
 * The executor config can be provided in two ways (in order of precedence):
 * </p>
 * <ul>
 * <li><code>executors.properties</code> file on the classpath. This file
 * should contain a <code>module</code> key who's value is the fully
 * qualified name of a class which extends {@link AbstractModule} and has a
 * public constructor which takes a single {@link Properties} object as an
 * argument or public no-args constructor. An instance of this class will be
 * returned.</li>
 * <li><code>executors.json</code> file on the classpath. This file contains
 * a specification of the executors to load in JSON format. If this file is
 * valid we will instantiate each executor defined in the spec and add it to
 * an {@link ExecutorModule} which will provide the bindings for them.</li>
 * </ul>
 * 
 * <h3>executors.json format</h3>
 * 
 * <p>
 * The <code>executors.json</code> file should contain a JSON object with
 * the following:
 * <ul>
 * <li><code>executors</code> key whose value is an array. Each element of
 * the array is a JSON object with the following keys:
 * <ul>
 * <li><code>class</code>: the fully qualified name of the executor class.</li>
 * <li><code>args</code>: an array of arguments to pass to a public
 * constructor of the class.</li>
 * <li><code>enableLogs</code> (optional): boolean value whether this
 * executor should save logs to file. Defaults to global value.</li>
 * <li><code>logDir</code> (optional): string path to save logs to. Defaults
 * to global value</li>
 * </ul>
 * </li>
 * <li><code>enableLogs</code> (optional): Global value for enableLogs for
 * each executor. Defaults to false.</li>
 * <li><code>logDir</code> (optional): Global value for logDir for each
 * executor. Default values depend on the executor.</li>
 * </ul>
 * </p>
 * <p>
 * e.g.:
 * </p>
 * 
 * <pre class="prettyprint">
 * {
 *    "executors": [{
 *       "class": "my.fully.qualified.Executor",
 *       "args": [1, "some string", true]
 *    },{
 *       ...
 *    }],
 * "enableLogs": true
 * }
 * </pre>
 * 
 * @return
 */
public static AbstractModule load() {
    Logger logger = Logger.getLogger(ExecutorModule.class);
    // look for executors.properties
    // This defines an AbstractModule to use instead of this one.
    // We try and load the module class given and return it.
    try {
        Properties execProps = new Properties();
        execProps.load(ExecutorModule.class.getClassLoader().getResourceAsStream("executors.properties"));
        String moduleName = execProps.getProperty("module", "");

        Class<? extends AbstractModule> module = Class.forName(moduleName).asSubclass(AbstractModule.class);
        // look for suitable ctor, either Properties parameter or default
        Constructor<? extends AbstractModule> ctor;
        try {
            ctor = module.getConstructor(Properties.class);
            return ctor.newInstance(execProps);
        } catch (NoSuchMethodException e) {
            ctor = module.getConstructor();
            return ctor.newInstance();
        }
    } catch (Exception e) {
        logger.debug("Could not create module from executors.properties");
    }
    // executors.properties fail, look for executors.json
    // This file defines a set of classes to load with parameters for the
    // constructor.
    // We try to create each defined executor and add it to this
    // ExecutorModule.
    try {
        // get executors.json file and parse to JSON.
        // throws NullPointerException if file doesn't exist, or
        // JSONException if we can't parse the JSON.
        InputStream is = ExecutorModule.class.getClassLoader().getResourceAsStream("executors.json");
        logger.debug("Processing executors from executors.json");
        JSONObject execConf = new JSONObject(new JSONTokener(new InputStreamReader(is)));
        // Create our module and look for executor specs under the executors
        // array in the JSON.
        ExecutorModule module = new ExecutorModule();
        JSONArray executors = execConf.getJSONArray("executors");

        // optional global settings
        boolean enableLogs = execConf.optBoolean("enableLogs", false);
        String logDir = execConf.optString("logDir");

        logger.info("Building Executors from executors.json");

        // Try and instantiate an instance of each executor in the spec.
        for (int i = 0; i < executors.length(); i++) {
            try {
                JSONObject executorSpec = executors.getJSONObject(i);
                String executorClass = executorSpec.getString("class");
                JSONArray args = executorSpec.getJSONArray("args");
                Class<? extends SimulationExecutor> clazz = Class.forName(executorClass)
                        .asSubclass(SimulationExecutor.class);
                // build constructor args.
                // We assume all types are in primitive form where
                // applicable.
                // The only available types are boolean, int, double and
                // String.
                Class<?>[] argTypes = new Class<?>[args.length()];
                Object[] argValues = new Object[args.length()];
                for (int j = 0; j < args.length(); j++) {
                    argValues[j] = args.get(j);
                    Class<?> type = argValues[j].getClass();
                    if (type == Boolean.class)
                        type = Boolean.TYPE;
                    else if (type == Integer.class)
                        type = Integer.TYPE;
                    else if (type == Double.class)
                        type = Double.TYPE;

                    argTypes[j] = type;
                }
                SimulationExecutor exe = clazz.getConstructor(argTypes).newInstance(argValues);
                logger.debug("Adding executor to pool: " + exe.toString());
                module.addExecutorInstance(exe);
                // logging config
                boolean exeEnableLog = executorSpec.optBoolean("enableLogs", enableLogs);
                String exeLogDir = executorSpec.optString("logDir", logDir);
                exe.enableLogs(exeEnableLog);
                if (exeLogDir.length() > 0) {
                    exe.setLogsDirectory(exeLogDir);
                }
            } catch (JSONException e) {
                logger.warn("Error parsing executor config", e);
            } catch (ClassNotFoundException e) {
                logger.warn("Unknown executor class in config", e);
            } catch (IllegalArgumentException e) {
                logger.warn("Illegal arguments for executor ctor", e);
            } catch (NoSuchMethodException e) {
                logger.warn("No matching public ctor for args in executor config", e);
            } catch (Exception e) {
                logger.warn("Could not create executor from specification", e);
            }
        }
        return module;
    } catch (JSONException e) {
        logger.debug("Could not create module from executors.json");
    } catch (NullPointerException e) {
        logger.debug("Could not open executors.json");
    }

    // no executor config, use a default config: 1 local sub process
    // executor.
    logger.info("Using default ExecutorModule.");
    return new ExecutorModule(1);
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

/**
 * Given either a {@link JSONArray} or a {@link JSONObject}, a key ending and a prefix to add, 
 * it looks for keys at any object inside the provided JSON ending with the key ending and adds 
 * the given prefix to that ending.// ww w. j a v  a  2s.c o m
 * This method calls itself recursively to traverse inner parts of {@link JSONObject}s.
 * @param jsonRoot The root {@link JSONObject} or {@link JSONArray}. If an object of any other type is provided, 
 *                the method just does nothing.
 * @param oldKeyEnding the ending to look for.
 * @param prefixToAdd the prefix to add to that ending.
 */
private void addPrefixToJSONKeysEndingsRecursive(Object jsonRoot, String oldKeyEnding, String prefixToAdd) {
    if (jsonRoot instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) jsonRoot;
        SortedSet<String> keys = ImmutableSortedSet.<String>naturalOrder().addAll(jsonObject.keySet()).build();
        for (String key : keys) {
            Object value = jsonObject.get(key);
            addPrefixToJSONKeysEndingsRecursive(value, oldKeyEnding, prefixToAdd);
            if (key.endsWith(oldKeyEnding)) {
                String newKey = key.replaceAll(Pattern.quote(oldKeyEnding) + "$", prefixToAdd + oldKeyEnding);
                jsonObject.remove(key);
                jsonObject.put(newKey, value);
            }
        }
    } else if (jsonRoot instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) jsonRoot;
        for (int i = 0; i < jsonArray.length(); i++) {
            Object value = jsonArray.get(i);
            addPrefixToJSONKeysEndingsRecursive(value, oldKeyEnding, prefixToAdd);
        }
    } else {
        return;
    }
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

/**
 * This method adds additional quotes to any String value inside a JSON. 
 * This helps to distinguish whether values come from strings or other 
 * primitive types when the JSON is converted to XML. 
 * @param jsonRoot//from w w w .  jav  a 2  s.c  om
 */
private void quoteStringsAtJSON(Object jsonRoot) {
    if (jsonRoot instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) jsonRoot;
        ImmutableSet<String> jsonObjectKeys = ImmutableSet.copyOf(jsonObject.keys());
        for (String key : jsonObjectKeys) {
            Object value = jsonObject.get(key);
            if (value instanceof String) {
                String valueStr = (String) value;
                jsonObject.put(key, "\"" + valueStr + "\"");
            } else if (value instanceof JSONObject || value instanceof JSONArray) {
                quoteStringsAtJSON(value);
            }
        }
    } else if (jsonRoot instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) jsonRoot;
        for (int i = 0; i < jsonArray.length(); i++) {
            Object value = jsonArray.get(i);
            if (value instanceof String) {
                String valueStr = (String) value;
                jsonArray.put(i, "\"" + valueStr + "\"");
            } else if (value instanceof JSONObject || value instanceof JSONArray) {
                quoteStringsAtJSON(value);
            }
        }
    } else {
        return;
    }
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

/**
 * This method looks recursively for arrays and encapsulates each of them into another array. 
 * So, anything like this:<br/>//  w  w w  .ja v  a2s.  co  m
 * <code>
 * {"myKey":["stuff1","stuff2"]}
 * </code><br/>
 * would become this:<br/>
 * <code>
 * {"myKey":[["stuff1","stuff2"]]}
 * </code><br/>
 * We do this strange preprocessing because structures like the first example are  
 * converted to XML producing this result:<br/>
 * <code>
 * &lt;myKey&gt;stuff1&lt;/myKey&gt;&lt;myKey&gt;stuff2&lt;/myKey&gt;
 * </code><br/>
 * Which makes impossible to distinguish single-element arrays from an element 
 * outside any array, because, both this:
 * <code>
 * {"singleElement":["stuff"]}
 * </code><br/>
 * and this:<br/>
 * <code>
 * {"singleElement":"stuff"}
 * </code><br/>
 * Would be converted to:<br/>
 * <code>
 * &lt;singleElement&gt;stuff&lt;/singleElement&gt;
 * </code><br/>
 * By doing this, we allow distingushing a single-element array from an non-array element, because 
 * the first one would be converted to:<br/>
 * <code>
 * &lt;singleElement&gt;&lt;array&gt;stuff&lt;/array&gt;&lt;/singleElement&gt;
 * 
 * @param jsonRoot The {@link JSONObject} or {@link JSONArray} which is the root of our JSON document.
 */
private void encapsulateArraysAtJSONRecursive(Object jsonRoot) {
    if (jsonRoot instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) jsonRoot;
        Set<String> keys = ImmutableSet.copyOf(jsonObject.keySet());
        for (String key : keys) {
            Object value = jsonObject.get(key);
            encapsulateArraysAtJSONRecursive(value);
            if (value instanceof JSONArray) {
                JSONArray encapsulatingJsonArray = new JSONArray();
                encapsulatingJsonArray.put(0, value);
                jsonObject.put(key, encapsulatingJsonArray);
            }
        }
    } else if (jsonRoot instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) jsonRoot;
        for (int i = 0; i < jsonArray.length(); i++) {
            Object value = jsonArray.get(i);
            encapsulateArraysAtJSONRecursive(value);
            if (value instanceof JSONArray) {
                JSONObject encapsulatingJsonObject = new JSONObject();
                encapsulatingJsonObject.put(XSDInferenceConfiguration.ARRAY_ELEMENT_NAME, value);
                jsonArray.put(i, encapsulatingJsonObject);
            }
        }
    } else {
        return;
    }
}

From source file:com.nonobay.fana.udacityandroidproject1popularmovies.FetchMovieTask.java

/**
 * Take the String representing the complete movies in JSON Format and
 * pull out the data we need to construct the Strings needed for the wireframes.
 * <p/>//from   ww  w  . j  a  v a 2s .  co m
 * Fortunately parsing is easy:  constructor takes the JSON string and converts it
 * into an Object hierarchy for us.
 */
private void getMovieDataFromJson(String moviesJsonStr) throws JSONException {

    // Now we have a String representing the complete movie list in JSON Format.
    // Fortunately parsing is easy:  constructor takes the JSON string and converts it
    // into an Object hierarchy for us.

    /* example
    {
    "page": 1,
    "results": [
        {
            "adult": false,
                "backdrop_path": "/razvUuLkF7CX4XsLyj02ksC0ayy.jpg",
                "genre_ids": [
            80,
                    28,
                    53
            ],
            "id": 260346,
                "original_language": "en",
                "original_title": "Taken 3",
                "overview": "Ex-government operative Bryan Mills finds his life is shattered when he's falsely accused of a murder that hits close to home. As he's pursued by a savvy police inspector, Mills employs his particular set of skills to track the real killer and exact his unique brand of justice.",
                "release_date": "2015-01-09",
                "poster_path": "/c2SSjUVYawDUnQ92bmTqsZsPEiB.jpg",
                "popularity": 11.737899,
                "title": "Taken 3",
                "video": false,
                "vote_average": 6.2,
                "vote_count": 698
        }
    ],
    "total_pages": 11543,
    "total_results": 230847
    }*/

    // These are the names of the JSON objects that need to be extracted.
    final String JSON_PAGE = "page";
    final String JSON_PAGE_TOTAL = "total_pages";
    final String JSON_MOVIE_LIST = "results";
    final String JSON_MOVIE_TOTAL = "total_results";

    final String JSON_MOVIE_ID = "id";
    final String JSON_MOVIE_TITLE = "original_title";
    final String JSON_MOVIE_DATE = "release_date";
    final String JSON_MOVIE_POSTER = "poster_path";
    final String JSON_MOVIE_OVERVIEW = "overview";
    final String JSON_MOVIE_VOTE_AVERAGE = "vote_average";

    try {

        JSONObject moviesJson = new JSONObject(moviesJsonStr);

        JSONArray movieArray = moviesJson.getJSONArray(JSON_MOVIE_LIST);

        // Insert the new movie information into the database
        Vector<ContentValues> cVVector = new Vector<>(movieArray.length());

        // These are the values that will be collected.
        String releaseTime;
        long movieId;
        double vote_average;
        String overview;
        String original_title;
        String poster_path;

        for (int i = 0; i < movieArray.length(); i++) {

            // Get the JSON object representing the movie
            JSONObject eachMovie = movieArray.getJSONObject(i);

            movieId = eachMovie.getLong(JSON_MOVIE_ID);
            original_title = eachMovie.getString(JSON_MOVIE_TITLE);
            overview = eachMovie.getString(JSON_MOVIE_OVERVIEW);
            poster_path = eachMovie.getString(JSON_MOVIE_POSTER);
            vote_average = eachMovie.getDouble(JSON_MOVIE_VOTE_AVERAGE);
            releaseTime = eachMovie.getString(JSON_MOVIE_DATE);

            ContentValues movieValues = new ContentValues();
            movieValues.put(MovieContract.MovieEntry.COLUMN_MOVIE_ID, movieId);
            movieValues.put(MovieContract.MovieEntry.COLUMN_ORIGINAL_TITLE, original_title);
            movieValues.put(MovieContract.MovieEntry.COLUMN_POSTER_THUMBNAIL, poster_path);
            movieValues.put(MovieContract.MovieEntry.COLUMN_SYNOPSIS, overview);
            movieValues.put(MovieContract.MovieEntry.COLUMN_RELEASE_DATE, releaseTime);
            movieValues.put(MovieContract.MovieEntry.COLUMN_USER_RATING, vote_average);

            cVVector.add(movieValues);
        }

        // add to database
        if (cVVector.size() > 0) {
            // Student: call bulkInsert to add the weatherEntries to the database here
            mContext.getContentResolver().delete(MovieContract.MovieEntry.CONTENT_URI, null, null);
            mContext.getContentResolver().bulkInsert(MovieContract.MovieEntry.CONTENT_URI,
                    cVVector.toArray(new ContentValues[cVVector.size()]));
        }

    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
}

From source file:com.nextgis.maplib.map.VectorLayer.java

public String createFromGeoJSON(JSONObject geoJSONObject) {
    try {//  w  w  w. j  a v a  2 s  .  com
        //check crs
        boolean isWGS84 = true; //if no crs tag - WGS84 CRS
        if (geoJSONObject.has(GEOJSON_CRS)) {
            JSONObject crsJSONObject = geoJSONObject.getJSONObject(GEOJSON_CRS);
            //the link is unsupported yet.
            if (!crsJSONObject.getString(GEOJSON_TYPE).equals(GEOJSON_NAME)) {
                return mContext.getString(R.string.error_crs_unsuported);
            }
            JSONObject crsPropertiesJSONObject = crsJSONObject.getJSONObject(GEOJSON_PROPERTIES);
            String crsName = crsPropertiesJSONObject.getString(GEOJSON_NAME);
            if (crsName.equals("urn:ogc:def:crs:OGC:1.3:CRS84")) { // WGS84
                isWGS84 = true;
            } else if (crsName.equals("urn:ogc:def:crs:EPSG::3857") || crsName.equals("EPSG:3857")) { //Web Mercator
                isWGS84 = false;
            } else {
                return mContext.getString(R.string.error_crs_unsuported);
            }
        }

        //load contents to memory and reproject if needed
        JSONArray geoJSONFeatures = geoJSONObject.getJSONArray(GEOJSON_TYPE_FEATURES);
        if (0 == geoJSONFeatures.length()) {
            return mContext.getString(R.string.error_empty_dataset);
        }

        List<Feature> features = new ArrayList<>();
        List<Pair<String, Integer>> fields = new ArrayList<>();

        int geometryType = GTNone;
        for (int i = 0; i < geoJSONFeatures.length(); i++) {
            JSONObject jsonFeature = geoJSONFeatures.getJSONObject(i);
            //get geometry
            JSONObject jsonGeometry = jsonFeature.getJSONObject(GEOJSON_GEOMETRY);
            GeoGeometry geometry = GeoGeometry.fromJson(jsonGeometry);
            if (geometryType == GTNone) {
                geometryType = geometry.getType();
            } else if (!Geo.isGeometryTypeSame(geometryType, geometry.getType())) {
                //skip different geometry type
                continue;
            }

            //reproject if needed
            if (isWGS84) {
                geometry.setCRS(CRS_WGS84);
                geometry.project(CRS_WEB_MERCATOR);
            } else {
                geometry.setCRS(CRS_WEB_MERCATOR);
            }

            int nId = i;
            if (jsonFeature.has(GEOJSON_ID))
                nId = jsonFeature.getInt(GEOJSON_ID);
            Feature feature = new Feature(nId, fields); // ID == i
            feature.setGeometry(geometry);

            //normalize attributes
            JSONObject jsonAttributes = jsonFeature.getJSONObject(GEOJSON_PROPERTIES);
            Iterator<String> iter = jsonAttributes.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                Object value = jsonAttributes.get(key);
                int nType = NOT_FOUND;
                //check type
                if (value instanceof Integer || value instanceof Long) {
                    nType = FTInteger;
                } else if (value instanceof Double || value instanceof Float) {
                    nType = FTReal;
                } else if (value instanceof Date) {
                    nType = FTDateTime;
                } else if (value instanceof String) {
                    nType = FTString;
                } else if (value instanceof JSONObject) {
                    nType = NOT_FOUND;
                    //the some list - need to check it type FTIntegerList, FTRealList, FTStringList
                }

                if (nType != NOT_FOUND) {
                    int fieldIndex = NOT_FOUND;
                    for (int j = 0; j < fields.size(); j++) {
                        if (fields.get(j).first.equals(key)) {
                            fieldIndex = j;
                        }
                    }
                    if (fieldIndex == NOT_FOUND) { //add new field
                        Pair<String, Integer> fieldKey = Pair.create(key, nType);
                        fieldIndex = fields.size();
                        fields.add(fieldKey);
                    }
                    feature.setFieldValue(fieldIndex, value);
                }
            }
            features.add(feature);
        }

        String tableCreate = "CREATE TABLE IF NOT EXISTS " + mPath.getName() + " ( " + //table name is the same as the folder of the layer
                "_ID INTEGER PRIMARY KEY, " + "GEOM BLOB";
        for (int i = 0; i < fields.size(); ++i) {
            Pair<String, Integer> field = fields.get(i);

            tableCreate += ", " + field.first + " ";
            switch (field.second) {
            case FTString:
                tableCreate += "TEXT";
                break;
            case FTInteger:
                tableCreate += "INTEGER";
                break;
            case FTReal:
                tableCreate += "REAL";
                break;
            case FTDateTime:
                tableCreate += "TIMESTAMP";
                break;
            }
        }
        tableCreate += " );";

        GeoEnvelope extents = new GeoEnvelope();
        for (Feature feature : features) {
            //update bbox
            extents.merge(feature.getGeometry().getEnvelope());
        }

        //1. create table and populate with values
        MapContentProviderHelper map = (MapContentProviderHelper) MapBase.getInstance();
        SQLiteDatabase db = map.getDatabase(true);
        db.execSQL(tableCreate);
        for (Feature feature : features) {
            ContentValues values = new ContentValues();
            values.put("_ID", feature.getId());
            try {
                values.put("GEOM", feature.getGeometry().toBlob());
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < fields.size(); ++i) {
                if (!feature.isValuePresent(i))
                    continue;
                switch (fields.get(i).second) {
                case FTString:
                    values.put(fields.get(i).first, feature.getFieldValueAsString(i));
                    break;
                case FTInteger:
                    values.put(fields.get(i).first, (int) feature.getFieldValue(i));
                    break;
                case FTReal:
                    values.put(fields.get(i).first, (double) feature.getFieldValue(i));
                    break;
                case FTDateTime:
                    values.put(fields.get(i).first, feature.getFieldValueAsString(i));
                    break;
                }
            }
            db.insert(mPath.getName(), "", values);
        }

        //2. save the layer properties to config.json
        mGeometryType = geometryType;
        mExtents = extents;
        mIsInitialized = true;
        setDefaultRenderer();

        save();

        //3. fill the geometry and labels array
        mVectorCacheItems = new ArrayList<>();
        for (Feature feature : features) {
            mVectorCacheItems.add(new VectorCacheItem(feature.getGeometry(), feature.getId()));
        }

        if (null != mParent) { //notify the load is over
            LayerGroup layerGroup = (LayerGroup) mParent;
            layerGroup.onLayerChanged(this);
        }

        return "";
    } catch (JSONException e) {
        e.printStackTrace();
        return e.getLocalizedMessage();
    }
}