Home > Java > javaTutorial > body text

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

黄舟
Release: 2018-05-12 16:01:33
Original
2559 people have browsed it

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"
 }
]
Copy after login

The method to read it as List 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();
  }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template