0

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>
tmyvu
  • 1
  • 1
    does [`JSONObject`](https://developer.android.com/reference/org/json/JSONObject.html) have `int` constructor? [check out here](https://stackoverflow.com/questions/4087674/android-read-text-raw-resource-file) how to read from `raw` – snachmsm Jun 25 '17 at 18:39
  • could you please elaborate your problem. – Developer_vaibhav Jun 25 '17 at 18:59
  • I'd like to get the coordinates from local json file with InputStream but I get JsonException Errors. – tmyvu Jun 25 '17 at 19:08
  • No JSONObject cannot revolve int constructor. That's why I need to convert from an InputStream – tmyvu Jun 25 '17 at 19:58

1 Answers1

0

you can do the following changes first you'll have to get the json file from the res folder and then parse the json data

// get the json file 

InputStream inputStream = getResources().openRawResource(R.raw.level1_points); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ctr;
    try {
        ctr = inputStream.read();
        while (ctr != -1) {
            byteArrayOutputStream.write(ctr);
            ctr = inputStream.read();
        }
        inputStream.close();

        String jsonString = byteArrayOutputStream.toString();
        Log.v("Text Data", jsonString);


        JSONObject jsonObj = new JSONObject(jsonString);
        JSONArray features = jsonObj.getJSONArray("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("properties");
            Double MEAN_X = properties.getDouble("MEAN_X");
            Double MEAN_Y = properties.getDouble("MEAN_Y");
            String UID = properties.getString("UID");

            Log.e(TAG, "onCreate: " + MEAN_X + MEAN_Y + UID);
        }
    } catch (Exception e) {
        Log.e(TAG, "onCreate: " + e.getMessage());
    }
Viraj
  • 1
  • 1