005.JSONObject,JSONArray,GSON和FastJSON
package com.json.enity; import java.util.ArrayList; import java.util.HashMap; public class Earthquake { public ArrayList<String> addresses; public ArrayList<Point> points; public ArrayList<ArrayList<String>> places; public ArrayList<String> getAddress() { return addresses; } public void setAddress(ArrayList<String> address) { this.addresses = address; } public ArrayList<Point> getPoints() { return points; } public void setPoints(ArrayList<Point> points) { this.points = points; } public ArrayList<ArrayList<String>> getPlaces() { return places; } public void setPlaces(ArrayList<ArrayList<String>> places) { this.places = places; } @Override public String toString() { return "Earthquake{" + "addresses=" + addresses + ", points=" + points + ", places=" + places + '}'; } }
package com.json.enity; import java.util.HashMap; public class Point { public double x; public double y; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + '}'; } }
package com.json.code; import com.json.enity.Earthquake; import com.json.enity.Point; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; public class Json_Jdk { public static String jsonString = "{\n" + " \"addresses\": [\n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\", \n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\"\n" + " ], \n" + " \"points\": [\n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }, \n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }\n" + " ], \n" + " \"places\": [\n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ], \n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ]\n" + " ]\n" + "}"; public static void main(String[] args) { /** * 1.导入包import org.json.JSONObject * 2.将Json转换为Java对象 */ Earthquake earthquake = new Earthquake(); //解析address属性并赋值 JSONObject jsonObject = new JSONObject(jsonString);//将Json转换为Java对象 JSONArray addressJSONArray = jsonObject.getJSONArray("addresses"); ArrayList<String> addressArrayList = new ArrayList<>(); for (int i = 0; i < addressJSONArray.length(); i++) { addressArrayList.add(addressJSONArray.getString(i)); } earthquake.setAddress(addressArrayList); //解析points属性并赋值 JSONArray points = jsonObject.getJSONArray("points"); ArrayList<Point> pointsArrayList = new ArrayList<>(); for (int i = 0; i < points.length(); i++) { Point point = new Point(); JSONObject jsonObject1 = points.getJSONObject(i); point.setX((Double) jsonObject1.get("x")); point.setX((Double) jsonObject1.get("y")); pointsArrayList.add(point); } earthquake.setPoints(pointsArrayList); //解析places属性并赋值 JSONArray placesJSONArray = jsonObject.getJSONArray("places"); ArrayList<ArrayList<String>> placesArrayListDouble = new ArrayList<>(); for (int i = 0; i < placesJSONArray.length(); i++) { ArrayList<String> innerArrayList = new ArrayList<>(); JSONArray jsonArray = placesJSONArray.getJSONArray(i); for (int j = 0; j < jsonArray.length(); j++) { String o = (String) jsonArray.get(j); innerArrayList.add(o); } placesArrayListDouble.add(innerArrayList); } earthquake.setPlaces(placesArrayListDouble); System.out.println(earthquake); } }
package com.json.code; import com.google.gson.Gson; import com.json.enity.Earthquake; public class Geson_Json { public static String jsonString="{\n" + " \"addresses\": [\n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\", \n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\"\n" + " ], \n" + " \"points\": [\n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }, \n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }\n" + " ], \n" + " \"places\": [\n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ], \n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ]\n" + " ]\n" + "}"; public static void main(String[] args) { Gson gson = new Gson(); Earthquake earthquake=gson.fromJson(jsonString,Earthquake.class); System.out.println(earthquake); } }
package com.json.code; import com.alibaba.fastjson.JSON; import com.json.enity.Earthquake; public class Fastjson_Json { public static String jsonString="{\n" + " \"addresses\": [\n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\", \n" + " \"广东省中山市火炬开发区在伟盛路附近距离顷九327米\"\n" + " ], \n" + " \"points\": [\n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }, \n" + " {\n" + " \"x\": 113.456021761023, \n" + " \"y\": 22.5737174302826\n" + " }\n" + " ], \n" + " \"places\": [\n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ], \n" + " [\n" + " \"测试区域\", \n" + " \"su\"\n" + " ]\n" + " ]\n" + "}"; public static void main(String[] args) { //Json转换为java对象 Earthquake earthquake = JSON.parseObject(jsonString, Earthquake.class); System.out.println(earthquake); /** * 1.java对象转换为Json * 2.fastjson要求JSON串中属性名一定要是小写 */ String stringJSON = JSON.toJSONString(earthquake); System.out.println(stringJSON); } }