分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

解析Json文件

发布时间:2023-09-06 01:38责任编辑:苏小强关键词:暂无标签

一:

 ???/** ????* 把json文件读取到内存中 ????* ?????* @throws IOException ????*/ ???public String getFile(String filePath) throws IOException { ???????StringBuffer jsonFile = new StringBuffer(); ???????Reader iStream = new FileReader(filePath); ???????BufferedReader br = new BufferedReader(iStream); ???????String line = ""; ???????while ((line = br.readLine()) != null) { ???????????jsonFile.append(line); ???????} ???????return jsonFile.toString(); ???}
package com.json;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/** * 使用json-lib构造和解析Json数据 * ?* @author Alexia * @date 2013/5/23 * */public class JsonTest { ???/** ????* 构造Json数据 ????* ?????* @return ????*/ ???public static String BuildJson() { ???????// JSON格式数据解析对象 ???????JSONObject jo = new JSONObject(); ???????// 下面构造两个map、一个list和一个Employee对象 ???????Map<String, String> map1 = new HashMap<String, String>(); ???????map1.put("name", "Alexia"); ???????map1.put("sex", "female"); ???????map1.put("age", "23"); ???????Map<String, String> map2 = new HashMap<String, String>(); ???????map2.put("name", "Edward"); ???????map2.put("sex", "male"); ???????map2.put("age", "24"); ???????List<Map> list = new ArrayList<Map>(); ???????list.add(map1); ???????list.add(map2); ???????Employee employee = new Employee(); ???????employee.setName("wjl"); ???????employee.setSex("female"); ???????employee.setAge(24); ???????// 将Map转换为JSONArray数据 ???????JSONArray ja1 = JSONArray.fromObject(map1); ???????// 将List转换为JSONArray数据 ???????JSONArray ja2 = JSONArray.fromObject(list); ???????// 将Bean转换为JSONArray数据 ???????JSONArray ja3 = JSONArray.fromObject(employee); ???????System.out.println("JSONArray对象数据格式:"); ???????System.out.println(ja1.toString()); ???????System.out.println(ja2.toString()); ???????System.out.println(ja3.toString()); ???????// 构造Json数据,包括一个map和一个Employee对象 ???????jo.put("map", ja1); ???????jo.put("employee", ja2); ???????System.out.println("\n最终构造的JSON数据格式:"); ???????System.out.println(jo.toString()); ???????return jo.toString(); ???} ???/** ????* 解析Json数据 ????* ?????* @param jsonString Json数据字符串 ????*/ ???public static void ParseJson(String jsonString) { ???????// 以employee为例解析,map类似 ???????JSONObject jb = JSONObject.fromObject(jsonString); ???????JSONArray ja = jb.getJSONArray("employee"); ???????List<Employee> empList = new ArrayList<Employee>(); ???????// 循环添加Employee对象(可能有多个) ???????for (int i = 0; i < ja.size(); i++) { ???????????Employee employee = new Employee(); ???????????employee.setName(ja.getJSONObject(i).getString("name")); ???????????employee.setSex(ja.getJSONObject(i).getString("sex")); ???????????employee.setAge(ja.getJSONObject(i).getInt("age")); ???????????empList.add(employee); ???????} ???????System.out.println("\n将Json数据转换为Employee对象:"); ???????for (int i = 0; i < empList.size(); i++) { ???????????Employee emp = empList.get(i); ???????????System.out.println("name: " + emp.getName() + " sex: " ???????????????????+ emp.getSex() + " age: " + emp.getAge()); ???????} ???} ???/** ????* @param args ????*/ ???public static void main(String[] args) { ???????// TODO Auto-generated method stub ???????ParseJson(BuildJson()); ???}}

解析Json文件

原文地址:https://www.cnblogs.com/HHR-SUN/p/8337967.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved