java读取json文件_java读取json文件与其他文件

一、读取json文件

直接读取文件,并转化为map

ObjectMapper objectMapper = new ObjectMapper();

try {

Map map = objectMapper.readValue(new File(filePath), Map.class);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

二、读取普通文件

使用FileInputStream效率最高!

下面是每次读取1024个字节

public static String readFile(String filePath) {

StringBuffer stringBuffer = new StringBuffer("");

byte[] buffer = new byte[1024];

int count = 0;

File file = new File(filePath);

try {

InputStream inputStream = new FileInputStream(file);

while (-1 != (count = inputStream.read(buffer))) {

stringBuffer.append(new String(buffer, 0, count));

}

inputStream.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return stringBuffer.toString();

}

下面是读取所有字节

public static String readFile(String filePath) {

String encoding = "UTF-8";

File file = new File(filePath);

Long filelength = file.length();

byte[] filecontent = new byte[filelength.intValue()];

try {

FileInputStream in = new FileInputStream(file);

in.read(filecontent);

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

try {

return new String(filecontent, encoding);

} catch (UnsupportedEncodingException e) {

System.err.println("The OS does not support " + encoding);

e.printStackTrace();

return null;

}

————————————————

版权声明:本文为CSDN博主「前方一片光明」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用许多库来解析JSON文件,其中最常用的是Jackson和Gson。以下是使用Gson库读取和处理JSON文件的简单示例: 1. 导入Gson库 ```java import com.google.gson.*; ``` 2. 读取JSON文件 ```java // 从文件读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); ``` 3. 处理JSON数据 ```java // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject中获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } ``` 完整示例代码: ```java import com.google.gson.*; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class JsonExample { public static void main(String[] args) throws Exception { // 从文件读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject中获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } // 输出结果 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); System.out.println("State: " + state); System.out.println("Hobbies: " + hobbies); } } ``` 其中,假设JSON文件中的内容如下: ```json { "name": "Alice", "age": 25, "address": { "city": "New York", "state": "NY" }, "hobbies": [ "reading", "painting", "hiking" ] } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值