Example usage for org.json JSONObject getString

List of usage examples for org.json JSONObject getString

Introduction

In this page you can find the example usage for org.json JSONObject getString.

Prototype

public String getString(String key) throws JSONException 

Source Link

Document

Get the string associated with a key.

Usage

From source file:uk.co.senab.photup.model.Place.java

public Place(JSONObject object, Account account) throws JSONException {
    super(object, account);
    mCategory = object.getString("category");

    JSONObject location = object.getJSONObject("location");
    mLatitude = location.getDouble("latitude");
    mLongitude = location.getDouble("longitude");
}

From source file:org.mapsforge.poi.exchange.GeoJsonPoiReader.java

@Override
public Collection<PointOfInterest> read() {
    final Collection<PointOfInterest> pois;

    try {/* w ww.  j  a  va2  s .  com*/
        JSONObject geoJson = new JSONObject(geoJsonString);

        String type = geoJson.getString("type");

        if (type.equals("FeatureCollection")) {
            pois = fromFeatureCollection(geoJson.getJSONArray("features"));
        } else if (type.equals("GeometryCollection")) {
            pois = fromGeometryCollection(geoJson.getJSONArray("geometries"));
        } else {
            throw new IllegalArgumentException("GeoJson of unknown type");
        }
    } catch (JSONException e) {
        throw new IllegalArgumentException(e.getMessage());
    }

    return pois;
}

From source file:org.mapsforge.poi.exchange.GeoJsonPoiReader.java

PointOfInterest fromFeature(JSONObject feature) throws JSONException {
    String type = feature.getString("type").toString();
    PointOfInterest point;//  w  ww . ja  va  2  s  . co m
    String name = null;
    String url = null;
    Long id = null;

    if (type.equals("Feature")) {
        point = fromPoint(feature.getJSONObject("geometry"));

        if (feature.has("properties")) {
            JSONObject properties = feature.getJSONObject("properties");
            if (properties.has("name")) {
                name = properties.getString("name");
            }
            if (properties.has("url")) {
                url = properties.getString("url");
            }
            if (properties.has("id")) {
                id = properties.getLong("id");
            }
        }

    } else {
        throw new IllegalArgumentException();
    }

    return new PoiBuilder(id, point.getLatitude(), point.getLongitude(), this.category).setName(name)
            .setUrl(url).build();
}

From source file:org.mapsforge.poi.exchange.GeoJsonPoiReader.java

PointOfInterest fromPoint(JSONObject point) throws JSONException {
    String type = point.getString("type");
    double lat;/*from   w  w w  .  j av a 2 s. c  o m*/
    double lng;

    if (type.equals("Point")) {
        JSONArray coords = point.getJSONArray("coordinates");
        lng = coords.getDouble(0);
        lat = coords.getDouble(1);
    } else {
        throw new IllegalArgumentException();
    }

    return new PoiBuilder(0, lat, lng, this.category).build();
}

From source file:org.restcomm.app.utillib.Reporters.WebReporter.WebReporter.java

public String getTwitterHandle(HashMap<String, String> carrier) throws LibException {
    try {//from   www .  ja v a2 s.  c o  m
        URL request = NetworkRequest.getURL(mHost, mApiKey, carrier);
        HttpURLConnection connection = (HttpURLConnection) request.openConnection();
        connection.connect();
        verifyConnectionResponse(connection);
        String networksResponseString = readString(connection);

        //JSONArray networks = new JSONObject(networksResponseString).getJSONArray(JSON_NETWORK_KEY);
        JSONObject network = new JSONObject(networksResponseString).getJSONObject(JSON_NETWORK_KEY);
        String twitter = network.getString(JSON_NETWORK_TWITTERHANDLE_KEY);
        return twitter;
    } catch (IOException e) {
        throw new LibException(e);
    } catch (Exception e) {
        throw new LibException(e);
    }
}

From source file:org.restcomm.app.utillib.Reporters.WebReporter.WebReporter.java

public static String geocode(Context context, double latitude, double longitude) {
    String addressString = String.format("%.4f, %.4f", latitude, longitude);
    try {//  w  w  w.j  a  va 2  s  .c  o  m
        String apiKey = Global.getApiKey(context);
        String server = Global.getApiUrl(context);
        String url = server + "/api/osm/location?apiKey=" + apiKey + "&location=" + latitude + "&location="
                + longitude;
        String response = WebReporter.getHttpURLResponse(url, false);

        JSONObject json = null;
        JSONArray jsonArray = null;

        if (response == null)
            return addressString;

        try {
            jsonArray = new JSONArray(response);
        } catch (JSONException e) {
            return addressString;
        }
        try {
            for (int i = 0; i < jsonArray.length(); i++) {
                json = jsonArray.getJSONObject(i);
                if (json.has("error")) {
                    String error = json.getString("error");
                    return null;
                } else {
                    addressString = "";
                    json = json.getJSONObject("address");
                    String number = "";
                    if (json.has("house_number")) {
                        number = json.getString("house_number");
                        addressString += number + " ";
                    }
                    String road = json.getString("road");
                    addressString += road;// + suburb;
                    return addressString;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
    }

    return addressString;
}

From source file:com.jaspersoft.jasperserver.ps.OAuth.JSONUtils.java

private static Map<String, String> parse(JSONObject json, Map<String, String> out) throws JSONException {
    Iterator<String> keys = json.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        String val = null;
        try {/*from w w w  .j a  v a2 s .  co m*/
            JSONObject value = json.getJSONObject(key);
            parse(value, out);
        } catch (Exception e) {
            val = json.getString(key);
        }

        if (val != null) {
            out.put(key, val);
        }
    }
    return out;
}

From source file:edu.asu.msse.gnayak2.main.CollectionSkeleton.java

public String callMethod(String request) {
    JSONObject result = new JSONObject();
    try {/* w ww .  j a va  2s.c om*/
        JSONObject theCall = new JSONObject(request);
        System.out.println(request);
        String method = theCall.getString("method");
        int id = theCall.getInt("id");
        JSONArray params = null;
        if (!theCall.isNull("params")) {
            params = theCall.getJSONArray("params");
            System.out.println(params);
        }
        result.put("id", id);
        result.put("jsonrpc", "2.0");
        if (method.equals("resetFromJsonFile")) {
            mLib.resetFromJsonFile();
            result.put("result", true);
            System.out.println("resetFromJsonCalled");
        } else if (method.equals("remove")) {
            String sName = params.getString(0);
            boolean removed = mLib.remove(sName);
            System.out.println(sName + " deleted");
            result.put("result", removed);
        } else if (method.equals("add")) {
            MovieImpl movie = new MovieImpl(params.getString(0));
            boolean added = mLib.add(movie);
            result.put("result", added);
        } else if (method.equals("get")) {
            String sName = params.getString(0);
            MovieImpl movie = mLib.get(sName);
            result.put("result", movie.toJson());
        } else if (method.equals("getNames")) {
            String[] names = mLib.getNames();
            JSONArray resArr = new JSONArray();
            for (int i = 0; i < names.length; i++) {
                resArr.put(names[i]);
            }
            result.put("result", resArr);
        } else if (method.equals("saveToJsonFile")) {
            boolean saved = mLib.saveToJsonFile();
            result.put("result", saved);
        } else if (method.equals("getModelInformation")) {
            //mLib.resetFromJsonFile();
            result.put("result", mLib.getModelInformation());
        } else if (method.equals("update")) {
            String movieJSONString = params.getString(0);
            Movie mo = new MovieImpl(movieJSONString);
            mLib.updateMovie(mo);
        } else if (method.equals("deleteAndAdd")) {
            String oldMovieJSONString = params.getString(0);
            String editedMovieJSONString = params.getString(1);
            boolean deletionSuccessful = false;
            boolean additionSuccessful = false;
            MovieImpl oldMovie = new MovieImpl(oldMovieJSONString);
            MovieImpl newMovie = new MovieImpl(editedMovieJSONString);
            deletionSuccessful = mLib.deleteMovie(oldMovie);
            additionSuccessful = mLib.add(newMovie);
            result.put("result", deletionSuccessful & additionSuccessful);
        }
    } catch (Exception ex) {
        System.out.println("exception in callMethod: " + ex.getMessage());
    }
    System.out.println("returning: " + result.toString());
    return "HTTP/1.0 200 Data follows\nServer:localhost:8080\nContent-Type:text/plain\nContent-Length:"
            + (result.toString()).length() + "\n\n" + result.toString();
}

From source file:uk.co.senab.photup.model.AbstractFacebookObject.java

public AbstractFacebookObject(JSONObject object, Account account) throws JSONException {
    mId = object.getString("id");
    mName = object.getString("name");

    if (null != account) {
        mAccountId = account.getId();/*from   w  ww  . j a  v  a  2 s . c  om*/
    }
}

From source file:com.tinyhydra.botd.VoteServerResource.java

@Put
public int store(String vote) throws SQLException {
    String email = "";
    String shopId = "";
    String shopRef = "";
    try {/* w w  w. j  a  v  a2s .c o  m*/
        JSONObject jo = new JSONObject(vote);
        email = jo.getString(JSONvalues.email.toString());
        shopId = jo.getString(JSONvalues.shopId.toString());
        shopRef = jo.getString(JSONvalues.shopRef.toString());
    } catch (JSONException jex) {
        System.out.println(jex);
    }
    int success = getSqi().SubmitVote(email, shopId, 100, shopRef) ? 0 : 1;
    sqi.con.close();
    return success;
}