在使用第三方的api文档时,会得到相应的JSON数据文件,那么我们怎样将JSON文件写入数据库从而测试数据呢?下面我将给大家做一个简单的展示。
JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。JSON是Douglas Crockford在2001年开始推广使用的数据格式,在2005年-2006年正式成为主流的数据格式,雅虎和谷歌就在那时候开始广泛地使用JSON格式。
1、JSON对象
以(key/value)键值对形式存在的无序的jsonObject对象,一个对象以“{”(左花括号)开始,“}”(右花括号)结束。每个“名称/值”之间“:”(冒号);“名称/值”与“名称/值”之间用逗号分隔。
例如:{“AddressEng”:“paris”,“AddressChn”:“巴黎”}
2、JSON数组
有序的value集合,这种形式被称为是jsonArray,数组是值(value)的有序集合。一个数组以“【”(左中括号开始),“】”(右中括号)结束,值之间使用“,”(逗号)分割。
例如:数组可以包含多个对象
{
“employees”:[{“firstName”:“John”,“lastName”:“Doe”},
{“firstName”:“David”,“lastName”:“James”},
{“firstName”:“curry”,“lastName”:“Stehp”}]
}
1、添加GSON依赖包
com.google.code.gson
gson
2.8.5
2、解析JSONObject
下面是一个jsonObject文件
{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "24",
"wind_direction": "西南风",
"wind_strength": "2级",
"humidity": "51%",
"time": "10:11"
},
"today": {
"temperature": "16℃~27℃",
"weather": "阴转多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期四",
"city": "滨州",
"date_y": "2015年06月04日",
"dressing_index": "舒适",
"dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "较适宜",
"travel_index": "",
"exercise_index": "较适宜",
"drying_index": ""
},
"future": [
{
"temperature": "16℃~27℃",
"weather": "阴转多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期四",
"date": "20150604"
},
{
"temperature": "20℃~32℃",
"weather": "多云转晴",
"weather_id": {
"fa": "01",
"fb": "00"
},
"wind": "西风3-4 级",
"week": "星期五",
"date": "20150605"
},
{
"temperature": "23℃~35℃",
"weather": "多云转阴",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "西南风3-4 级",
"week": "星期六",
"date": "20150606"
},
{
"temperature": "20℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风微风",
"week": "星期日",
"date": "20150607"
},
{
"temperature": "22℃~34℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期一",
"date": "20150608"
},
{
"temperature": "22℃~33℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "西南风3-4 级",
"week": "星期二",
"date": "20150609"
},
{
"temperature": "22℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "南风3-4 级",
"week": "星期三",
"date": "20150610"
}
]
},
"error_code": 0
}
解析过程:
package cn.edu.bzu.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class Read {
public static void main(String args[]){
JsonParser parse =new JsonParser(); //创建json解析器
try {
JsonObject json=(JsonObject) parse.parse(new FileReader("weather.json")); //创建jsonObject对象
System.out.println("resultcode:"+json.get("resultcode").getAsInt()); //将json数据转为为int型的数据
System.out.println("reason:"+json.get("reason").getAsString()); //将json数据转为为String型的数据
JsonObject result=json.get("result").getAsJsonObject();
JsonObject today=result.get("today").getAsJsonObject();
System.out.println("temperature:"+today.get("temperature").getAsString());
System.out.println("weather:"+today.get("weather").getAsString());
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
输出结果:
resultcode:200
reason:successed!
temperature:16~27℃
weather:阴转多云
3、解析JSONArray数据
下面是json文件
{
"cat":"it",
"language":[
{"id":1,"ide":"eclipse","name":Java},
{"id":2,"ide":"XCode","name":"Swift"},
{"id":3,"ide":"Visual Stdio","name":"C#"}
],
"pop":true
}
解析过程
package cn.edu.bzu.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class ReadJSON {
public static void main(String args[]){
try {
JsonParser parser=new JsonParser(); //创建JSON解析器
JsonObject object=(JsonObject) parser.parse(new FileReader("test.json")); //创建JsonObject对象
System.out.println("cat="+object.get("cat").getAsString()); //将json数据转为为String型的数据
System.out.println("pop="+object.get("pop").getAsBoolean()); //将json数据转为为boolean型的数据
JsonArray array=object.get("language").getAsJsonArray(); //得到为json的数组
for(int i=0;i
输出结果:
cat=it
pop=true
---------------
id=1
name=Java
ide=eclips
---------------
id=2
name=Swift
ide=XCode
---------------
id=3
name=C#
ide=Visual Stdio
ps:以上就是java解析json数据的方法,我们将需要的字段取出来之后写入实体类中,最后再调用mapper的insert方法就能将数据写入数据库。如下图所示:
最后通过Test类去调用该方法:
总结几点:解析json数据时,
1.导入GSON依赖包
2.需要进行创建Gson解析器
3.创建JSONObject对象
4.将json数据转为为相应的数据