json&xml学习笔记--Java读取本地json文件

写在前面

嗯,之前没具体学习过JSON相关知识,最近整理文档,顺便做个总结。

本篇文章实现了Java读取本地json文件功能——使用commons.io和fastjson实现。

所需jar包:

commons-io-2.6.jar;

fastjson-1.2.28.jar;

json文件:

books.xml,位于src根目录下:

{
  "message": "这是一个中文测试",
  "info": {
    "fileType": "json",
    "fileUsage": "我在哪里?我要做什么?"
  },
  "books": [{
    "title": "Everyday Italian",
    "author": "Giada De Laurentiis",
    "year": "2005",
    "price": 30.00
  	},
    {
      "title": "Harry Potter",
      "author": "J K. Rowling",
      "year": "2005",
      "price": 29.99
    },
    {
      "title": "Learning JSON",
      "author": "Erik T. Ray",
      "year": "2005",
      "price": 39.95
    }
  ]
}

Java代码:

package books;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class GetJson {

	public static void main(String[] args) {
		try {
//			获取JSON文件内容,转化为字符串类型
			String jsonStr = FileUtils.readFileToString(new File("src/books.json"), "UTF-8");
//			将字符串转化为json
			JSONObject jsonObject = JSON.parseObject(jsonStr);

//			获取单个属性值
			String message = (String) jsonObject.get("message");
			System.out.println("***message***");
			System.out.println("message= " + message);
			
//			获取对象值
			JSONObject info = jsonObject.getJSONObject("info");
			String fileType = info.getString("fileType");
			String fileUsage = info.getString("fileUsage");
			System.out.println("***info***");
			System.out.println("    fileType=" + fileType);
			System.out.println("    fileUsage=" + fileUsage);
			
//			获取数组值
			JSONArray jsonArray = jsonObject.getJSONArray("books");
			System.out.println("***books***");

			for(int i = 0; i < jsonArray.size(); i++) {
				System.out.println("***book***");
				JSONObject jObject = (JSONObject) jsonArray.get(i);
				String title = jObject.getString("title");
				String author = jObject.getString("author");
				String year = jObject.getString("year");
				double price = jObject.getDoubleValue("price");
				
				System.out.println("    title=" + title);
				System.out.println("    author=" + author);
				System.out.println("    year=" + year);
				System.out.println("    price=" + price);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

输出结果:

***message***
message= 这是一个中文测试
***info***
    fileType=json
    fileUsage=我在哪里?我要做什么?
***books***
***book***
    title=Everyday Italian
    author=Giada De Laurentiis
    year=2005
    price=30.00
***book***
    title=Harry Potter
    author=J K. Rowling
    year=2005
    price=29.99
***book***
    title=Learning JSON
    author=Erik T. Ray
    year=2005
    price=39.95

转载于:https://my.oschina.net/u/2427564/blog/3055980

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值