简体   繁体   English

使用Jackson库从JSON文件创建JAVA Map

[英]create JAVA Map out of JSON file using Jackson libraries

I am using CouchbaseLite to build embedded database of NoSQL type. 我正在使用CouchbaseLite来构建NoSQL类型的嵌入式数据库。 Downloaded libraries for 1.3 version couchbase-lite-java-1.3.1-community.zip and added them to my Java project in eclipse. 下载了1.3版本couchbase-lite-java-1.3.1-community.zip库,并将它们添加到eclipse中的Java项目中。

Following are the tasks I want to achieve? 以下是我想要实现的任务?

  1. Read .json file (let us assume the file path J:/temp/sample.json ) 读取.json文件(让我们假设文件路径为J:/temp/sample.json
  2. Convert it to Map 将其转换为地图
  3. Insert into CouchBase database. 插入CouchBase数据库。

Following is the code that I tried: 以下是我尝试过的代码:

    CouchDBManager dbManager = new CouchDBManager();
    Database myDB = dbManager.createDataBase("atempt1");
    // first step
    File f = new File("J:/temp/sample.json");
    // code to read the content of a file
    // second step
    ObjectNode objectNode1 = mapper.createObjectNode();
    // add code to covert the json content read from the file to Map
    // third step
    Document doc = myDB.createDocument();
    doc.putProperties(map); // putProperties expectes Map object

So, kindly help me in reading the json file and convert it to Map using Jackson libraries so that I can create and insert the document into CouchbaseLite DB. 所以,请帮助我阅读json文件并使用Jackson库将其转换为Map以便我可以创建文档并将其插入CouchbaseLite DB。

Note: Jackson libraries are part of CouchBaseLite that I downloaded, so I want a solution around it. 注意: Jackson库是我下载的CouchBaseLite一部分,所以我想要一个解决方案。 I don't want to use custom processing of JSON file and convert it to Map, which is error prone and performance issue. don't want to use custom processing JSON文件的don't want to use custom processing并将其转换为Map,这容易出错并且性能问题。

Please have a look at Jackson data bind docs int the below link: 请查看以下链接中的Jackson数据绑定文档:

map = mapper.readValue(new File("J:/temp/sample.json"), HashMap.class);

returns a Map. 返回一张地图。

Reference: 参考:

  1. http://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/ObjectMapper.html#readValue(java.io.File,%20com.fasterxml.jackson.databind.JavaType) http://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/ObjectMapper.html#readValue(java.io.File,%20com.fasterxml.jackson.databind.JavaType)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM