Python 官方文档:入门教程 => 点击学习
Java读取项目JSON文件并转为jsON对象 1、创建json文件(demo.json) { "button": [ { "type": "click", "
{
"button": [
{
"type": "click",
"name": "今日歌曲",
"key": "V1001_TODAY_MUSIC"
},
{
"name": "菜单",
"sub_button": [
{
"type": "view",
"name": "搜索",
"url": "Http://www.soso.com/"
},
{
"type": "miniprogram",
"name": "wxa",
"url": "http://mp.weixin.qq.com",
"appid": "wx286b93c14bbf93aa",
"pagepath": "pages/lunar/index"
},
{
"type": "click",
"name": "赞一下我们",
"key": "V1001_GoOD"
}
]
}
]
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
package com.jeff.demo;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSONObject;
public class FileDemo3 {
public static JSONObject fileToJson(String fileName) {
JSONObject json = null;
try (
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
) {
json = JSONObject.parseObject(IOUtils.toString(is, "utf-8"));
} catch (Exception e) {
System.out.println(fileName + "文件读取异常" + e);
}
return json;
}
public static void main(String[] args) {
String fileName = "doc/demo.json";
JSONObject json = FileDemo3.fileToJson(fileName);
System.out.println(json);
}
}
String jsonFilePath = "C:/a.json";
File file = new File(jsonFilePath );
String input = FileUtils.readFileToString(file,"UTF-8");
JSONObject obj = new JSONObject(input);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Java读取项目json文件并转为JSON对象的操作
本文链接: https://www.lsjlt.com/news/132334.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0