Cannot find codec for class org.json.JSONArray

 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 make a request and get the result from mongodb, and I succeeded in the 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 tried, it gave me the error below.

org.bson.codecs.configuration.CodecConfigurationException: cannot find codec for class org.json.JSONArray

What is the perfect way to do this?

Hint: I actually think that an error has occurred in my code for this part of json.get ("time zone"). because it contains an array. When I use hardcode string arrays, this code works

+4
source share
2 answers

MongoDB MongoDB MongoDB "students" "":

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mongodb</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
</project>

//Main.java

package com.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.*;
import org.bson.Document;
import org.bson.conversions.Bson;

import javax.print.Doc;

public class Main {

    public static void main(String[] args) {

        MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("students");
        final MongoCollection<Document> collection = database.getCollection("grades");

        Bson sort = new Document("student_id", 1).append("score", 1);
        MongoCursor<Document> cursor = collection.find().sort(sort).iterator();

        try {
            Integer student_id = -1;
            while (cursor.hasNext()) {
                Document document = cursor.next();
                // Doing more stuff
            }
        } finally {
            cursor.close();
        }
    }
}
+1

JSONObject/JSONArray .

, payload.get("data") - Map

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

, payload.get("data") json.

BasicDBObject json =(BasicDBObject) JSON.parse(payload.get("data"));
+1

Source: https://habr.com/ru/post/1668002/


All Articles