如何读取linkedtreemap [GSON]类型的json数据?

时间:2014-04-16 08:31:57

标签: java json

我正在尝试阅读以下json数据。如何使用LinkedTreeMap阅读?

{"msgType": "gameInit", "data": {
  "race": {
    "track": {
      "id": "indianapolis",
      "name": "Indianapolis",
      "pieces": [
        {
          "length": 100.0
        },
        {
          "length": 100.0,
          "switch": true
        },
        {
          "radius": 200,
          "angle": 22.5
        }
      ],
      "lanes": [
        {
          "distanceFromCenter": -20,
          "index": 0
        },
        {
          "distanceFromCenter": 0,
          "index": 1
        },
        {
          "distanceFromCenter": 20,
          "index": 2
        }
      ],
      "startingPoint": {
        "position": {
          "x": -340.0,
          "y": -96.0
        },
        "angle": 90.0
      }
    },
    "cars": [
      {
        "id": {
          "name": "Schumacher",
          "color": "red"
        },
        "dimensions": {
          "length": 40.0,
          "width": 20.0,
          "guideFlagPosition": 10.0
        }
      },
      {
        "id": {
          "name": "Rosberg",
          "color": "blue"
        },
        "dimensions": {
          "length": 40.0,
          "width": 20.0,
          "guideFlagPosition": 10.0
        }
      }
    ],
    "raceSession": {
      "laps": 3,
      "maxLapTimeMs": 30000,
      "quickRace": true
    }
  }
}}

4 个答案:

答案 0 :(得分:6)

我使用Gson解析你的JSON对象的工作方法很长。

您可以尝试如下:

 import com.google.gson.Gson;
        public class JsonParser {
            public static void main(String[] args){
                Gson gson = new Gson();
                String yourJson = "";
                MainObject object = gson.fromJson(yourJson, MainObject.class);
            }
        }

此处yourJson是您收到回复的JSON对象,在这里我只使用String来向您展示。

MainObject是解析JSON对象所需的POJO。

我已经展示了JSON所需的所有POJO。尝试使用它。

MainObject.java

public class MainObject {
    private String msgType;
    private Data data;

    public String getMsgType() {
        return msgType;
    }
    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }
    public Data getData() {
        return data;
    }
    public void setData(Data data) {
        this.data = data;
    }
}

Data.java

public class Data {
    private race race;
    public race getRace() {
        return race;
    }
    public void setRace(race race) {
        this.race = race;
    }
}

Track.java

public class Track{
    private String id;
    private String name;
    private List<Pieces> pieces;
    private List<Lanes> lanes;
    private startingPoint startingPoint;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Pieces> getPieces() {
        return pieces;
    }
    public void setPieces(List<Pieces> pieces) {
        this.pieces = pieces;
    }
    public List<Lanes> getLanes() {
        return lanes;
    }
    public void setLanes(List<Lanes> lanes) {
        this.lanes = lanes;
    }
    public startingPoint getStartingPoint() {
        return startingPoint;
    }
    public void setStartingPoint(startingPoint startingPoint) {
        this.startingPoint = startingPoint;
    }
}

Pieces.java

public class Pieces{
    private int length;
    private boolean switch;
    private int radius;
    private float angle;

    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    public int getRadius() {
        return radius;
    }
    public void setRadius(int radius) {
        this.radius = radius;
    }
    public float getAngle() {
        return angle;
    }
    public void setAngle(float angle) {
        this.angle = angle;
    }
    public boolean isSwitch() {
        return Switch;
    }
    public void setSwitch(boolean _switch) {
        Switch = _switch;
    }
}

Lanes.java

public class Lanes{
    private String distanceFromCenter;
    private int index;

    public String getDistanceFromCenter() {
        return distanceFromCenter;
    }
    public void setDistanceFromCenter(String distanceFromCenter) {
        this.distanceFromCenter = distanceFromCenter;
    }
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }
}

StartingPoint.java

public class StartingPoint{
    private String angle;
    private Position position;

    public String getAngle() {
        return angle;
    }
    public void setAngle(String angle) {
        this.angle = angle;
    }
    public Position getPosition() {
        return position;
    }
    public void setPosition(Position position) {
        this.position = position;
    }
}

Position.java

public class Position{
    private String x;
    private String y;
    public String getX() {
        return x;
    }
    public void setX(String x) {
        this.x = x;
    }
    public String getY() {
        return y;
    }
    public void setY(String y) {
        this.y = y;
    }
}

Cars.java

public class Cars{
    private Id id;
    private Dimensions dimensions;
    public id getId() {
        return id;
    }
    public void setId(id id) {
        this.id = id;
    }
    public Dimensions getDimensions() {
        return dimensions;
    }
    public void setDimensions(Dimensions dimensions) {
        this.dimensions = dimensions;
    }
} 

Id.java

public class Id{
    private String name;
    private String color;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }   
}

Dimensions.java

public class Dimensions{
    private int length;
    private int width;
    private int guideFlagPosition;
    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getGuideFlagPosition() {
        return guideFlagPosition;
    }
    public void setGuideFlagPosition(int guideFlagPosition) {
        this.guideFlagPosition = guideFlagPosition;
    }
}

RaceSession.java

public class RaceSession{
    private int lap;
    private String maxLapTimeMs;
    private boolean quickRace;
    public int getLap() {
        return lap;
    }
    public void setLap(int lap) {
        this.lap = lap;
    }
    public String getMaxLapTimeMs() {
        return maxLapTimeMs;
    }
    public void setMaxLapTimeMs(String maxLapTimeMs) {
        this.maxLapTimeMs = maxLapTimeMs;
    }
    public boolean isQuickRace() {
        return quickRace;
    }
    public void setQuickRace(boolean quickRace) {
        this.quickRace = quickRace;
    }
}

这就是全部。所有必需的POJO都在这里。

我已经使用过这种方法并且工作正常。

答案 1 :(得分:2)

来自the site

 public static void main(String[] args) {

  Gson gson = new Gson();

  try {

   System.out.println("Reading JSON from a file");
   System.out.println("----------------------------");

   BufferedReader br = new BufferedReader(
     new FileReader(args[0]));

    //convert the json string back to object
   MyBean countryObj = gson.fromJson(br, MyBean.class);
    // MyBean contains the data in the JSON and is a standard Java Bean   
 }
}

答案 2 :(得分:0)

BufferedReader in = new BufferedReader(input);

String inputLine;
String fullline = "";

while ((inputLine = in.readLine()) != null) {
    fullline = fullline.concat(inputLine);

}

JSONObject rootObject = new JSONObject(fullline);
JSONObject rows1 = rootObject.getJSONObject("data");
JSONObject race = rows1.getJSONObject("race");

JSONObject track = rows1.getJSONObject("track");
JSONArray pieces = rows1.getJSONArray("pieces");

答案 3 :(得分:0)

Hello这很简单,你可以使用JSON,不需要使用外部库Gson.it也可以增加你的应用程序大小。所以避免使用它。

// Try to parse JSON
    try {
        JSONObject jsonObjMain = new JSONObject(myjsonstring);
        JSONObject jsonObjectData=(JSONObject) jsonObjMain.get("data");
        JSONObject jsonObjectRace=(JSONObject) jsonObjectData.get("race");
        JSONObject jsonObjectTrack=(JSONObject) jsonObjectRace.get("track");
        JSONArray jsonArrayPieces=(JSONArray) jsonObjectTrack.get("pieces");
        JSONArray jsonArrayLanes=(JSONArray) jsonObjectTrack.get("lanes");
        JSONObject jsonObjectStartingPoint=(JSONObject) jsonObjectTrack.get("startingPoint");
        System.out.println("Starting Point :"+jsonObjectStartingPoint);
        JSONArray jsonArrayCars=(JSONArray) jsonObjectRace.get("cars");
        JSONObject jsonObjectRaceSession=(JSONObject) jsonObjectRace.get("raceSession");
        System.out.println("Race Session :"+jsonObjectRaceSession);
        for (int i = 0; i < jsonArrayCars.length(); i++) {
            JSONObject jsonObj = jsonArrayCars.getJSONObject(i);
            JSONObject jsonObjId = (JSONObject) jsonObj.get("id");
            System.out.println("id :"+jsonObjId);
            JSONObject jsonObjDimensions  = (JSONObject) jsonObj.get("dimensions");
            System.out.println("Dinmentions :"+jsonObjDimensions);
        }
        for (int i = 0; i < jsonArrayPieces.length(); i++) {
            JSONObject jsonObj = jsonArrayPieces.getJSONObject(i);
            System.out.println("Piece data :"+jsonObj);
        }
        for (int i = 0; i < jsonArrayLanes.length(); i++) {
            JSONObject jsonObj = jsonArrayLanes.getJSONObject(i);
            System.out.println("Lanes data :"+jsonObj);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }