Home  >  Article  >  Java  >  About how Java reads json files in web projects into map collections

About how Java reads json files in web projects into map collections

黄舟
黄舟Original
2018-05-12 16:01:332544browse

This article mainly introduces you to the relevant information about using java to read json files in web projects into map collections. The article introduces you to you in great detail through sample code, which has certain reference for your study or work. Value, friends in need come and take a look below.

Preface

This article mainly introduces the relevant content about Java reading json files in web projects into map collections, and shares them for your reference. Learning, I won’t say much more below, let’s take a look at the detailed introduction.

Instance introduction

Assume that there is a json file in the current project web directory (/resource/test.json) as follows:

[
 {
 "path": "content_111",
 "title": "文章1",
 "imgUrl": "../../../libs/img/pptau/pf.jpg"
 },
 {
 "path": "content_222",
 "title": "文章2",
 "imgUrl": "../../../libs/img/pptau/pf.jpg"
 }
]

The method to read it as List54e47cee50a55a19f1b926d0f6809e99 in Java is:

String dir = request.getSession().getServletContext()
.getRealPath("/resource/test.json");

  try {
   File file = new File(dir);
   if (!file.exists()) {
    file.createNewFile();
   }
   String str= FileUtils.readFileToString(file, "UTF-8");
   List<Map> maps= (List)JSONArray.fromObject(str);
  } catch (IOException e) {
   e.printStackTrace();
  }

The above is the detailed content of About how Java reads json files in web projects into map collections. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn