Home > Net >  org.json.JSONException: JSONObject["properties"] not found
org.json.JSONException: JSONObject["properties"] not found

Time:04-19

I am new to programming and am trying to read massages from Kafka as a whole String and convert them into a JSON object (Array) and then access each element in the JSON Array to extract the values. But it is giving me the following error:

org.json.JSONException: JSONObject["properties"] not found.

Here is my JSON Array: Please note that I have multiple JSON arrays like this which is why I am looping them all to iterate this process and put them each in a separate JSON Object.

{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"STATIONS_ID": 662, "MESS_DATUM": 202204120000, "RWS_DAU_10": 0, "RWS_10": 0.0, "RWS_IND_10": 0}, "geometry": {"type": "Point", "coordinates": [10.516667, 52.266666]}}]}

This is my code:

// input massages from a Kafka topic
JSONObject json_obj = new JSONObject(valueFromKafka); 
// Access to the JSON Array element "features"
JSONArray jsonFeatures =  (JSONArray) objJsonObject.getJSONArray("features");

// for anhanced loop to iterate this process
for (Object feature_obj : jsonFeatures) {
JSONObject feature = new JSONObject(feature_obj);

// access and get values from the JSON Array
Long STATIONS_ID = feature.getJSONObject("properties").getLong("STATIONS_ID");
Long MESS_DATUM = feature.getJSONObject("properties").getLong("MESS_DATUM");
Double RWS_DAU_10 = feature.getJSONObject("properties").getDouble("RWS_DAU_10");
Long RWS_IND_10 = feature.getJSONObject("properties").getLong("RWS_IND_10");
Float RWS_10 = feature.getJSONObject("properties").getFloat("RWS_10");
JSONArray coordJson = feature.getJSONObject("properties").getJSONObject("geometry").getJSONArray("coordinates");
String pointWKT = "POINT("   coordJson.getFloat(0)   " "   coordJson.getFloat(1)   ")";

CodePudding user response:

Try the below code:

        JSONObject json_obj = new JSONObject(valueFromKafka);
        JSONArray features = json_obj.getJSONArray("features");
        for (int i = 0; i < features.length(); i  ) {
            JSONObject feature = features.getJSONObject(i);
            /*JSONObject properties = feature.getJSONObject("properties");*/
            Long STATIONS_ID = feature.getJSONObject("properties").getLong("STATIONS_ID");
            Long MESS_DATUM = feature.getJSONObject("properties").getLong("MESS_DATUM");
            Double RWS_DAU_10 = feature.getJSONObject("properties").getDouble("RWS_DAU_10");
            Long RWS_IND_10 = feature.getJSONObject("properties").getLong("RWS_IND_10");
            Float RWS_10 = feature.getJSONObject("properties").getFloat("RWS_10");
            JSONArray coordJson = feature.getJSONObject("geometry").getJSONArray("coordinates");
            String pointWKT = "POINT("   coordJson.getDouble(0)   " "   coordJson.getDouble(1)   ")";
        }

CodePudding user response:

try this(Use FastJson)

ValType target = JSON.jsonToObject(val, ValType.class);

and then u can continue to foreach this Collection

  • Related