当前位置: 首页 > news >正文

Java googlecode JSON 解析示例

为什么80%的码农都做不了架构师?>>>   hot3.png

1.pom.xml配置

<dependency>
	<groupId>com.googlecode.json-simple</groupId>
	<artifactId>json-simple</artifactId>
	<version>1.1</version>
</dependency>

2.准备JSON数据文件jsonTestFile.json

{
  "id": 1,
  "firstname": "Katerina",
  "languages": [
    {
      "lang": "en",
      "knowledge": "proficient"
    },
    {
      "lang": "fr",
      "knowledge": "advanced"
    }
  ],
  "job": {
    "site": "www.javacodegeeks.com",
    "name": "Java Code Geeks"
  }
}

3.Java 解析类

package com.javacodegeeks.javabasics.jsonparsertest;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonParseTest {

	private static final String filePath = "C:\\Users\\katerina\\Desktop\\jsonTestFile.json";
	
	public static void main(String[] args) {

		try {
			// read the json file
			FileReader reader = new FileReader(filePath);

			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

			// get a String from the JSON object
			String firstName = (String) jsonObject.get("firstname");
			System.out.println("The first name is: " + firstName);

			// get a number from the JSON object
			long id =  (long) jsonObject.get("id");
			System.out.println("The id is: " + id);

			// get an array from the JSON object
			JSONArray lang= (JSONArray) jsonObject.get("languages");
			
			// take the elements of the json array
			for(int i=0; i<lang.size(); i++){
				System.out.println("The " + i + " element of the array: "+lang.get(i));
			}
			Iterator i = lang.iterator();

			// take each value from the json array separately
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				System.out.println("language "+ innerObj.get("lang") + 
						" with level " + innerObj.get("knowledge"));
			}
			// handle a structure into the json object
			JSONObject structure = (JSONObject) jsonObject.get("job");
			System.out.println("Into job structure, name: " + structure.get("name"));

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ParseException ex) {
			ex.printStackTrace();
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		}

	}

}


转载于:https://my.oschina.net/boonya/blog/835073

相关文章:

  • background-clip
  • Docker的常用命令-02
  • 【MongoDB】从入门到精通mongdb系列学习宝典,想学mongodb小伙伴请进来
  • SAS描述统计量
  • 老毛桃PE修改方法(屏蔽更改主页,屏蔽加装的绿色浏览器)
  • 2017年2月12日 WER学习总结
  • InfluxDB和MySQL的读写对比测试
  • 制作 OpenStack Linux 镜像 - 每天5分钟玩转 OpenStack(151)
  • HBase入门基础教程 HBase之单机模式与伪分布式模式安装
  • linux 创建sudo账号.md
  • Git基础之(十一)——远程仓库——从远程库克隆
  • 素数筛
  • ActiveMq持久化数据
  • ERROR! The server quit without updating PID file (/usr/local/var/mysql/bogon.pid).
  • 前端知识小计
  • 【comparator, comparable】小总结
  • Codepen 每日精选(2018-3-25)
  • cookie和session
  • css选择器
  • Redis在Web项目中的应用与实践
  • Vue ES6 Jade Scss Webpack Gulp
  • Vue2.x学习三:事件处理生命周期钩子
  • 对象管理器(defineProperty)学习笔记
  • 将回调地狱按在地上摩擦的Promise
  • 悄悄地说一个bug
  • 区块链分支循环
  • 全栈开发——Linux
  • 深入浏览器事件循环的本质
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 写给高年级小学生看的《Bash 指南》
  • 一个完整Java Web项目背后的密码
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • ​用户画像从0到100的构建思路
  • #pragma once
  • ${ }的特别功能
  • (1)(1.13) SiK无线电高级配置(五)
  • (C#)获取字符编码的类
  • (python)数据结构---字典
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (论文阅读40-45)图像描述1
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转) 深度模型优化性能 调参
  • (转)socket Aio demo
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • .NET开发者必备的11款免费工具
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • .NET是什么
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • @ModelAttribute 注解
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [ Linux ] Linux信号概述 信号的产生
  • [04]Web前端进阶—JS伪数组