[英]org.json.JSONException: JSONObject["properties"] not found
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.我是编程新手,正在尝试从 Kafka 读取按摩作为一个整体字符串并将它们转换为 JSON object(数组),然后访问 JSON 数组中的每个元素以提取值。 But it is giving me the following error:
但它给了我以下错误:
org.json.JSONException: JSONObject["properties"] not found. org.json.JSONException:未找到 JSONObject [“属性”]。
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.这是我的 JSON 数组:请注意,我有多个像这样的 881569990078588 arrays,这就是为什么我将它们全部循环以迭代此过程并将它们分别放在单独的 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) + ")";
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) + ")";
}
try this(Use FastJson)试试这个(使用 FastJson)
ValType target = JSON.jsonToObject(val, ValType.class);
and then u can continue to foreach this Collection然后你可以继续 foreach 这个 Collection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.