Search in sources :

Example 71 with JSONArray

use of org.json.JSONArray in project android-UniversalMusicPlayer by googlesamples.

the class RemoteJSONSource method iterator.

@Override
public Iterator<MediaMetadataCompat> iterator() {
    try {
        int slashPos = CATALOG_URL.lastIndexOf('/');
        String path = CATALOG_URL.substring(0, slashPos + 1);
        JSONObject jsonObj = fetchJSONFromUrl(CATALOG_URL);
        ArrayList<MediaMetadataCompat> tracks = new ArrayList<>();
        if (jsonObj != null) {
            JSONArray jsonTracks = jsonObj.getJSONArray(JSON_MUSIC);
            if (jsonTracks != null) {
                for (int j = 0; j < jsonTracks.length(); j++) {
                    tracks.add(buildFromJSON(jsonTracks.getJSONObject(j), path));
                }
            }
        }
        return tracks.iterator();
    } catch (JSONException e) {
        LogHelper.e(TAG, e, "Could not retrieve music list");
        throw new RuntimeException("Could not retrieve music list", e);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 72 with JSONArray

use of org.json.JSONArray in project android-maps-utils by googlemaps.

the class MyItemReader method read.

public List<MyItem> read(InputStream inputStream) throws JSONException {
    List<MyItem> items = new ArrayList<MyItem>();
    String json = new Scanner(inputStream).useDelimiter(REGEX_INPUT_BOUNDARY_BEGINNING).next();
    JSONArray array = new JSONArray(json);
    for (int i = 0; i < array.length(); i++) {
        String title = null;
        String snippet = null;
        JSONObject object = array.getJSONObject(i);
        double lat = object.getDouble("lat");
        double lng = object.getDouble("lng");
        if (!object.isNull("title")) {
            title = object.getString("title");
        }
        if (!object.isNull("snippet")) {
            snippet = object.getString("snippet");
        }
        items.add(new MyItem(lat, lng, title, snippet));
    }
    return items;
}
Also used : Scanner(java.util.Scanner) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) MyItem(com.google.maps.android.utils.demo.model.MyItem)

Example 73 with JSONArray

use of org.json.JSONArray in project gdk-compass-sample by googleglass.

the class Landmarks method populatePlaceList.

/**
     * Populates the internal places list from places found in a JSON string. This string should
     * contain a root object with a "landmarks" property that is an array of objects that represent
     * places. A place has three properties: name, latitude, and longitude.
     */
private void populatePlaceList(String jsonString) {
    try {
        JSONObject json = new JSONObject(jsonString);
        JSONArray array = json.optJSONArray("landmarks");
        if (array != null) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.optJSONObject(i);
                Place place = jsonObjectToPlace(object);
                if (place != null) {
                    mPlaces.add(place);
                }
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, "Could not parse landmarks JSON string", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 74 with JSONArray

use of org.json.JSONArray in project graphhopper by graphhopper.

the class GraphHopperWeb method readErrors.

public static List<Throwable> readErrors(JSONObject json) {
    List<Throwable> errors = new ArrayList<>();
    JSONArray errorJson;
    if (json.has("message")) {
        if (json.has("hints")) {
            errorJson = json.getJSONArray("hints");
        } else {
            // should not happen
            errors.add(new RuntimeException(json.getString("message")));
            return errors;
        }
    } else
        return errors;
    for (int i = 0; i < errorJson.length(); i++) {
        JSONObject error = errorJson.getJSONObject(i);
        String exClass = "";
        if (error.has("details"))
            exClass = error.getString("details");
        String exMessage = error.getString("message");
        if (exClass.equals(UnsupportedOperationException.class.getName()))
            errors.add(new UnsupportedOperationException(exMessage));
        else if (exClass.equals(IllegalStateException.class.getName()))
            errors.add(new IllegalStateException(exMessage));
        else if (exClass.equals(RuntimeException.class.getName()))
            errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
        else if (exClass.equals(IllegalArgumentException.class.getName()))
            errors.add(new DetailedIllegalArgumentException(exMessage, toMap(error)));
        else if (exClass.equals(ConnectionNotFoundException.class.getName())) {
            errors.add(new ConnectionNotFoundException(exMessage, toMap(error)));
        } else if (exClass.equals(PointNotFoundException.class.getName())) {
            int pointIndex = error.getInt("point_index");
            errors.add(new PointNotFoundException(exMessage, pointIndex));
        } else if (exClass.equals(PointOutOfBoundsException.class.getName())) {
            int pointIndex = error.getInt("point_index");
            errors.add(new PointOutOfBoundsException(exMessage, pointIndex));
        } else if (exClass.isEmpty())
            errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
        else
            errors.add(new DetailedRuntimeException(exClass + " " + exMessage, toMap(error)));
    }
    if (json.has("message") && errors.isEmpty())
        errors.add(new RuntimeException(json.getString("message")));
    return errors;
}
Also used : PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) GHPoint(com.graphhopper.util.shapes.GHPoint) ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) JSONObject(org.json.JSONObject) DetailedIllegalArgumentException(com.graphhopper.util.exceptions.DetailedIllegalArgumentException) PointOutOfBoundsException(com.graphhopper.util.exceptions.PointOutOfBoundsException) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException)

Example 75 with JSONArray

use of org.json.JSONArray in project physical-web by google.

the class PhysicalWebCollection method jsonSerialize.

/**
   * Create a JSON object that represents this data structure.
   * @return a JSON serialization of this data structure.
   */
public JSONObject jsonSerialize() {
    JSONObject jsonObject = new JSONObject();
    // Serialize the UrlDevices
    JSONArray urlDevices = new JSONArray();
    for (UrlDevice urlDevice : mDeviceIdToUrlDeviceMap.values()) {
        urlDevices.put(urlDevice.jsonSerialize());
    }
    jsonObject.put(DEVICES_KEY, urlDevices);
    // Serialize the URL metadata
    JSONArray metadata = new JSONArray();
    for (PwsResult pwsResult : mBroadcastUrlToPwsResultMap.values()) {
        metadata.put(pwsResult.jsonSerialize());
    }
    jsonObject.put(METADATA_KEY, metadata);
    JSONObject iconMap = new JSONObject();
    for (String iconUrl : mIconUrlToIconMap.keySet()) {
        iconMap.put(iconUrl, new String(Base64.encodeBase64(getIcon(iconUrl)), Charset.forName("UTF-8")));
    }
    jsonObject.put(ICON_MAP_KEY, iconMap);
    jsonObject.put(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
    return jsonObject;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Aggregations

JSONArray (org.json.JSONArray)1710 JSONObject (org.json.JSONObject)1191 JSONException (org.json.JSONException)738 ArrayList (java.util.ArrayList)323 IOException (java.io.IOException)243 Test (org.junit.Test)207 HashMap (java.util.HashMap)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)96 List (java.util.List)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 File (java.io.File)54 HashSet (java.util.HashSet)54 Date (java.util.Date)47 Query (org.b3log.latke.repository.Query)47 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 GraphObject (com.abewy.android.apps.klyph.core.graph.GraphObject)43 FileNotFoundException (java.io.FileNotFoundException)40 Map (java.util.Map)40 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)36 VolleyError (com.android.volley.VolleyError)35