I am trying to read the below json data. How to read using 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
}
}
}}
I've little long but working approach to parse your JSON Object using Gson.
You can try it as following:
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);
}
}
Here yourJson
is your JSON object in which your response is received, here I've used String just to show you.
And MainObject
is the POJO for required to parse your JSON object.
I've shown all POJOs for that are required for your JSON. Try to use it.
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;
}
}
That's all. All required POJOs are here.
I've used this approach and it's working fine.
From 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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With