gpt4 book ai didi

java - 如何使用 GSON 解析 JSON 响应 (Java/Android)

转载 作者:行者123 更新时间:2023-12-01 11:18:17 25 4
gpt4 key购买 nike

我只需要一个快速建议,因为我是 JSON 的初学者。

我从网络服务器收到以下响应,我将其存储在字符串中:

{  
"station62":[
{
"departureTime":1982,
"delay":"-1.0",
"line":"6",
"stationName":"randomname",
"direction":2
}
],
"station63":[
{
"departureTime":1234,
"delay":"-1.0",
"line":"87",
"stationName":"anotherrandomname",
"direction":2
}
],
"station64":[
{
"departureTime":4542,
"delay":"-1.0",
"line":"4",
"stationName":"yetanotherrandomname",
"direction":2
}
],
"station65":[
{
"departureTime":1232,
"delay":"-1.0",
"line":"23",
"stationName":"onemorerandomname",
"direction":2
}
]
}

(抱歉,我不知道缩进是如何工作的。)

响应较长,但在本例中已缩短。所以我需要的是解析每个“站”对象的信息。我不需要“station62”字符串,我只需要 java 对象中的“departureTime”、“delay”、“line”、“stationName”和“direction”。

我已经读过这篇文章,但我无法让它工作:https://stackoverflow.com/a/16378782

我是一个完全的初学者,所以任何帮助将非常感激。

编辑:这是我的代码:

我创建了一个包装类,就像上面的示例链接中一样。我对 map 类型进行了一些尝试,但到目前为止还没有运气。

public class ServerResponse
{

private Map<String, ArrayList<Station>> stationsInResponse = new HashMap<String, ArrayList<Station>>();

public Map<String, ArrayList<Station>> getStationsInResponse()
{
return stationsInResponse;
}

public void setStationsInResponse(Map<String, ArrayList<Station>> stationsInResponse)
{
this.stationsInResponse = stationsInResponse;
}
}

问题是,这张 map 没有被我下面显示的 gson.fromJSON(...) 调用填充。 map 大小始终为零。

站点类如下所示:

public class Station
{
String line;
String stationName;
String departureTime;
String direction;
String delay;

// getters and setters are there aswell
}

我想做的是

Gson gson = new Gson();
ServerResponse response = gson.fromJson(jsonString, ServerResponse.class);

其中“jsonString”包含字符串形式的 JSON 响应。

我希望代码显示了我需要做什么,它应该非常简单,但我在 JSON 方面还不够好。

编辑2

我需要我的 JSON 是这样的吗?

{"stationsInResponse": {
"station62": [{
"departureTime": 1922,
"delay": "-1.0",
"line": "8",
"stationName": "whateverrandomname",
"direction": 2
}],
"station67": [{
"departureTime": 1573,
"delay": "-1.0",
"line": "8",
"stationName": "rndmname",
"direction": 2
}],
"station157": [{
"departureTime": 1842,
"delay": "-2.0",
"line": "8",
"stationName": "randomname",
"direction": 2
}]
}}

最佳答案

这是工作代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

public class GSONTest {

public static void main(String[] args){

String gsonStr = "{\"stationsInResponse\": { \"station62\":[ { \"departureTime\":1982,\"delay\":\"-1.0\",\"line\":\"6\",\"stationName\":\"randomname\",\"direction\":2} ],\"station63\":[ { \"departureTime\":1981,\"delay\":\"-1.1\",\"line\":\"7\",\"stationName\":\"randomname2\",\"direction\":3} ]}}";

Gson gson = new Gson();
Response response = gson.fromJson(gsonStr, Response.class);

System.out.println("Map size:"+response.getStationsInResponse().size());

for (Iterator iterator = response.getStationsInResponse().keySet().iterator(); iterator.hasNext();) {

String key = (String) iterator.next();

ArrayList<Station> stationList = (ArrayList<Station>) response.getStationsInResponse().get(key);

for (Iterator iterator2 = stationList.iterator(); iterator2.hasNext();) {

Station station = (Station) iterator2.next();

System.out.println("Delay: "+station.getDelay());
System.out.println("DepartureTime: "+station.getDepartureTime());
System.out.println("Line: "+station.getLine());
System.out.println("StationName: "+station.getStationName());
}


}


}


}

class Response {
private Map<String, List<Station>> stationsInResponse;
//getters and setters

public Map<String, List<Station>> getStationsInResponse() {
return stationsInResponse;
}

public void setStationsInResponse(Map<String, List<Station>> stationsInResponse) {
this.stationsInResponse = stationsInResponse;
}
}

class Station {
private String departureTime;
public String getDepartureTime() {
return departureTime;
}
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}
public String getDelay() {
return delay;
}
public void setDelay(String delay) {
this.delay = delay;
}
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
private String delay;
private String line;
private String stationName;
private String direction;

}

控制台中的输出是这样的(因为我缩短了你的 json 字符串):

Map size:2
Delay: -1.0
DepartureTime: 1982
Line: 6
StationName: randomname
Delay: -1.1
DepartureTime: 1981
Line: 7
StationName: randomname2

关于java - 如何使用 GSON 解析 JSON 响应 (Java/Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536221/

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