MongoDB"NumberLong/$ numberLong";转换回Java对象时出现问题 [英] MongoDB "NumberLong/$numberLong" issue while converting back to Java Object
问题描述
我有一个像{"Header" : {"name" : "TestData", "contactNumber" : 8019071740}}
如果我将此插入到mongoDB中,它将类似于
If i insert this to mongoDB it will be something like
{"_id" : ObjectId("58b7e55097989619e4ddb0bb"),"Header" : {"name" : "TestData","contactNumber" : NumberLong(8019071743)}
当我读回此数据并尝试使用Gson转换为java对象时,它会抛出异常com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a long but was BEGIN_OBJECT at line 1 column 109 path $.Header.contactNumber
When i read this data back and try to convert to java object using Gson it throws exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a long but was BEGIN_OBJECT at line 1 column 109 path $.Header.contactNumber
我发现了此,但是我想知道我是否具有非常复杂的json结构,那么我可能需要用这种方法来操纵许多json节点.
I have found this, But i was wondering if i have very complex json structure then i might need to manipulate many json nodes in this approach.
有人在这方面有更好的选择吗?
Do anyone has any better alternatives on this.
修改:1 我正在阅读如下查询和转换json
1 I am reading querying and converting json as below
Document MongoDocument = mycollection.find(searchCondition);
String resultJson = MongoDocument.toJson();
Gson gson = new Gson();
Model model= gson.fromJson(resultJson, ItemList.class);
推荐答案
看看:我有同样的问题. com.mongodb.util.JSON.serialize(document)的变通办法可以解决问题.
I had the same problem. The workaround with com.mongodb.util.JSON.serialize(document) does the trick.
这篇关于MongoDB"NumberLong/$ numberLong";转换回Java对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!