gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:08 39 4
gpt4 key购买 nike

我正在尝试读取以下 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
}
}
}}

最佳答案

我使用 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。尝试使用它。

主对象.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;
}
}

数据.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;
}
}

片断.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;
}
}

车道.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;
}
}

起点.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;
}
}

位置.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;
}
}

汽车.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;
}
}

维度.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 都在这里。

我已经使用过这种方法并且效果很好。

关于java - 如何读取 linkedtreemap [GSON] 类型的 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23104009/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com