最近突然對json比較敏感,項目裏不少地方需要自己拼接json或者對json進行解析處理,來記錄一下學習經歷,這篇說說直接訪問json進行解析的方式,後續更解析成Javabean的方式~
有三種解析方式:解析的方式無大差異只是方法名不一樣需要注意
1.jsonlib解析:比較麻煩,需要引入的jar包比較多:
- commons-beanutils-xxx.jar
- commons-collections-xxx.jar
- commons-lang-xxx.jar
- commons-logging-xxx.jar
- ezmorph-xxx.jar
- json-lib-xxx.jar
2.阿里巴巴的FastJson解析:這個比較好,功能和jsonlib差不多但是隻需要引一個jar包就好:fastjson-xxx.jar
3.谷歌的gson解析:gson-xxx.jar
1.jsonlib解析
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class textJsonLib {
public static void main(String[] args) {
//最簡單的json格式解析:
String hehe = "{'question':'123','answer':'456'}";
JSONObject jsonObject = JSONObject.fromObject(hehe);
System.out.println("question:" + jb.get("question") + "," + "answer:" + jb.get("answer"));
輸出格式:question:123,answer:456
System.out.println("====================");
//複雜一點的json格式解析,json裏帶數組的
String hehe = "{'type':'multy','question':'愛好','options':[{'value':'籃球'},{'value':'足球'}]}";
JSONObject jsonObject = JSONObject.fromObject(hehe);
String type = (String) jsonObject.get("type");
String question = (String) jsonObject.get("question");
System.out.println(type + "," + question);
//解析中間json數組
JSONArray items = jsonObject.getJSONArray("options");
JSONObject row = null;
for (int i = 0; i < items.size(); i++) {
row = items.getJSONObject(i);
System.out.println(row.get("value"));
}
System.out.println("====================");
String hehe = "{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球'},{'value':'足球'}]}}";
JSONObject jsonObject = JSONObject.fromObject(hehe);
String type = (String) jsonObject.get("type");
String question = (String) jsonObject.get("question");
System.out.println(type + "," + question);
JSONArray items = jsonObject.getJSONObject("options").getJSONArray("items");
JSONObject row = null;
for (int i = 0; i < items.size(); i++) {
row = items.getJSONObject(i);
System.out.println(row.get("value"));
}
System.out.println("====================");
String hehe = "{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','value':'足球'}]}}";
JSONObject jsonObject = JSONObject.fromObject(hehe);
String type = (String) jsonObject.get("type");
String question = (String) jsonObject.get("question");
System.out.println(type + "," + question);
JSONArray items = jsonObject.getJSONObject("options").getJSONArray("items");
JSONObject row = null;
for (int i = 0; i < items.size(); i++) {
row = items.getJSONObject(i);
System.out.println(row.get("value")); //這樣可以直接取出數組中的兩條數據
//也可以將items對應的數組取出來再進行遍歷 ["籃球","足球"]
JSONArray values = JSONArray.fromObject(row.get("value"));
System.out.println(values);
for (int k = 0; k < values.size(); k++) {
System.out.println(values.get(k)); //這樣也可以取出來對應的兩條數據
}
}
System.out.println("====================");
//再來個複雜點的,數組裏嵌套json的
String json = "{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}}";
JSONObject jsonObject = JSONObject.fromObject(json);
System.out.println("type:" + jsonObject.get("type") + "," + "question:" + jsonObject.get("question"));
JSONArray items = jsonObject.getJSONObject("options").getJSONArray("items");
JSONObject row = null;
for (int i = 0; i < items.size(); i++) {
row = items.getJSONObject(i);
System.out.println("value:" + row.get("value"));
JSONObject country = row.getJSONObject("country");
System.out.println("privince:" + country.get("privince") + "," + "city:" + country.get("city"));
}
/*
* 輸出
* type:multy,question:愛好
* value:籃球
* privince:liaoning,city:shenyang
*/
System.out.println("====================");
//解析json數組,說白了就是分成挨個json字符串挨個解析
String json = "[{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}},{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}}]";
JSONArray jsonArray = JSONArray.fromObject(json);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
System.out.println(jsonObject.get("type") + "," + jsonObject.get("question"));
JSONArray items = jsonObject.getJSONObject("options").getJSONArray("items");
JSONObject row = null;
for (int j = 0; j < items.size(); j++) {
row = items.getJSONObject(j);
System.out.println(row.get("value"));
JSONObject country = row.getJSONObject("country");
System.out.println(country.get("privince") + "," + country.get("city"));
}
}
}
}
以上全部親測有效~
2.fastjson解析:這裏只貼一個解析json數組的代碼了,基本上和jsonlib的一樣,只是方法名有點不同,注意一下就好
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class testFstJson {
public static void main(String[] args) {
String json = "[{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}},{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}}]";
JSONArray jsonArray = JSONArray.parseArray(json);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = JSONObject.parseObject(jsonArray.getString(i));
System.out.println(jsonObject.get("type") + "," + jsonObject.get("question"));
JSONArray items = jsonObject.getJSONObject("options").getJSONArray("items");
JSONObject row = null;
for (int j = 0; j < items.size(); j++) {
row = items.getJSONObject(j);
System.out.println(row.get("value"));
JSONObject country = row.getJSONObject("country");
System.out.println(country.get("privince") + "," + country.get("city"));
}
}
/*
* 輸出格式
* multy,愛好
* 籃球
* liaoning,shenyang
* multy,愛好
* 籃球
* liaoning,shenyang
*/
}
}
3.gson方式解析:親身嘗試之後感覺不如前兩個好,不推薦使用
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class testGsonJson {
public static void main(String[] args) {
//解析json數據
String json = "{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}}";
JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
//此處可以直接輸出,如果要取出值放到別出用的話一定要用getAsString()或者toString()方法轉換一下類型,否則報錯
String type = jsonObject.get("type").getAsString();
String question = jsonObject.get("question").getAsString();
System.out.println(type + "," + question);
JsonArray jsonArray = jsonObject.get("options").getAsJsonObject().get("items").getAsJsonArray();
JsonObject row = null;
for (int i = 0; i < jsonArray.size(); i++) {
row = (JsonObject) jsonArray.get(i);
System.out.println(row);
String value = row.get("value").getAsString();
System.out.println("value:" + value);
JsonObject country = row.get("country").getAsJsonObject();
System.out.println("privince:" + country.get("privince") + "," + "city:" + country.get("city"));
}
/*
* 控制檯輸出
* multy,愛好
* value:籃球
* privince:"liaoning",city:"shenyang"
*/
System.out.println("====================");
//解析json數組,此處有坑請注意
String json = "[{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}},{'type':'multy','question':'愛好','options':{'items':[{'value':'籃球','country':{'privince':'liaoning','city':'shenyang'}}]}}]";
JsonArray jsonArray = new JsonParser().parse(json).getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
/*
* 此處有坑:執行new JsonParser().parse(json數組)後
* 得到的是一個JsonElement對象無法進行後續操作(取值或循環遍歷)
* 需要執行getAsString()或toString()方法將其變成字符串
*/
JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonArray.get(i).toString());
System.out.println("type:" + jsonObject.get("type") + "," + "question:" + jsonObject.get("question"));
JsonArray items = jsonObject.get("options").getAsJsonObject().get("items").getAsJsonArray();
JsonObject row = null;
for (int j = 0; j < items.size(); j++) {
row = (JsonObject) items.get(j);
String value = row.get("value").getAsString();
System.out.println("value:" + value);
JsonObject country = row.get("country").getAsJsonObject();
System.out.println("privince:" + country.get("privince") + "," + "city:" + country.get("city"));
System.out.println("====================");
}
}
/*
* 控制檯輸出
* type:"multy",question:"愛好"
* value:籃球
* privince:"liaoning",city:"shenyang"
* ====================
* type:"multy",question:"愛好"
* value:籃球
* privince:"liaoning",city:"shenyang"
* ====================
*/
}
}
綜上是小編參考和自己摸索出來的結果,如有錯誤請指教~