I'm new to Java and having problems with parsing json. I have a json file in res folder and need to get lat/lng from the file which will be displayed with a marker later. How can I parse the file within public void without needing to create a new Java Class or Activity?
Json
{
"type":"FeatureCollection",
"crs":{
"type":"name",
"properties":{
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features":[
{
"type":"Feature",
"properties":{
"MEAN_X":13.34994,
"MEAN_Y":52.54291,
"UID":"B154"
},
"geometry":{
"type":"Point",
"coordinates":[
13.34993674,
52.54291394
]
}
},
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
JSONObject jsonObj = new JSONObject(R.raw.level1_points);
final String TAG_FEATURES = jsonObj.getString("features");
final String TAG_PROPERTIES = jsonObj.getString("properties");
final double TAG_MEANX = jsonObj.getDouble("MEAN_X");
final double TAG_MEANY = jsonObj.getDouble("MEAN_X");
final String TAG_UID = jsonObj.getString("UID");
try {
JSONArray features = jsonObj.getJSONArray(TAG_FEATURES);
for (int i = 0; i < features.length(); i++) {
// Create a marker for each room in the JSON data.
JSONObject c = features.getJSONObject(i);
JSONObject properties = c.getJSONObject(TAG_PROPERTIES);
Double MEAN_X = properties.getDouble(TAG_MEANX);
Double MEAN_Y = properties.getDouble(TAG_MEANY);
String UID = properties.getString(TAG_UID);
if (spinner.getSelectedItem().toString().equals(UID)) {
LatLng room = new LatLng(MEAN_X; MEAN_Y);
mMap.addMarker(new MarkerOptions().position(raum).title("Room"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(room));
mMap.animateCamera(CameraUpdateFactory.zoomTo(16));
<code>