Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find a codec for class org.json.JSONArray

Tags:

java

mongodb

 private void getUsersWithin24Hours(String id, Map < String, Object > payload) throws JSONException {
  JSONObject json = new JSONObject(String.valueOf(payload.get("data")));
  Query query = new Query();

  query.addCriteria(Criteria.where("user_id").is(id).and("timezone").in(json.get("timezone")).and("gender").in(json.get("gender")).and("locale").in(json.get("language")).and("time").gt(getDate()));
  mongoTemplate.getCollection("user_log").distinct("user_id", query.getQueryObject());
 }

I was going to made a query and get result from mongodb and I was succeed with mongo terminal command:

db.getCollection('user_log').find({"user_id" : "1", "timezone" : {$in: [5,6]}, "gender" : {$in : ["male", "female"]}, "locale" : {$in : ["en_US"]}, "time" : {$gt : new ISODate("2017-01-26T16:57:52.354Z")}}) 

but from java when I was trying it gave me below error.

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.json.JSONArray

What is the ideal way to do this?

Hint : actually I think in my code error occurred of this part json.get("timezone"). because it contains array. When I am using hardcode string arrays this code works

like image 276
Januka samaranyake Avatar asked Oct 30 '22 13:10

Januka samaranyake


1 Answers

You don't have to use JSONObject/JSONArray for conversion.

Replace with below line if the payload.get("data") is Map

BasicDBObject json = new BasicDBObject(payload.get("data"));

Replace with below line if the payload.get("data") holds json string.

BasicDBObject json =(BasicDBObject) JSON.parse(payload.get("data"));
like image 75
s7vr Avatar answered Nov 13 '22 01:11

s7vr