如何在没有 JsonArray 的情况下解析 JsonObject?
How to parse JsonObject without JsonArray?
我有一个 json 这样的:
{"status": "ok","data": {
"0": {
"id": "1901",
"price": "0",
"userBought": "0",
"leagueName": "Germany League",
"teamOne": "Grossaspach",
"teamTwo": "Offenbacher",
"date": "05.11.2021 - 21.00",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.19"
},
"1": {
"id": "1900",
"price": "0",
"userBought": "0",
"leagueName": "France League",
"teamOne": "FC Villefranche-Beaujolai",
"teamTwo": "US Avranches",
"date": "05.11.2021 - 21.00",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.25"
},
"2": {
"id": "1899",
"price": "0",
"userBought": "0",
"leagueName": "Germany League",
"teamOne": "Holstein Kiel",
"teamTwo": "Dynamo Dresden",
"date": "05.11.2021 - 20.30",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.20"
}}}
但是我无法使用 volley 从“数据”标签获取字符串对象,因为没有任何 json 数组到 foreach 标签。
我累了,我搜索了这么多例子。我无法从 stacoverflow 找到任何解决方案。
谁能帮帮我?
我不熟悉 Volley,但一个直接的方法是将你的 JSON 字符串去隐藏成一个 Map
与大多数 JSON 库(例如,Jackson ),那么就可以得到字段data
的内容,遍历如下:
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> resultMap = objectMapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {});
((Map<String, Object>) resultMap.get("data")).entrySet().stream()
.map(Map.Entry::getValue)
.forEach(System.out::println);
控制台输出:
{id=1901, price=0, userBought=0, leagueName=Germany League, teamOne=Grossaspach, teamTwo=Offenbacher, date=05.11.2021 - 21.00, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.19}
{id=1900, price=0, userBought=0, leagueName=France League, teamOne=FC Villefranche-Beaujolai, teamTwo=US Avranches, date=05.11.2021 - 21.00, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.25}
{id=1899, price=0, userBought=0, leagueName=Germany League, teamOne=Holstein Kiel, teamTwo=Dynamo Dresden, date=05.11.2021 - 20.30, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.20}
您也可以使用 Jayway JsonPath:
获得相同的结果
Map<String, Object> resultMap = JsonPath.parse(jsonStr).read("$.data");
resultMap.entrySet().stream()
.map(Map.Entry::getValue)
.forEach(System.out::println);
我有一个 json 这样的:
{"status": "ok","data": {
"0": {
"id": "1901",
"price": "0",
"userBought": "0",
"leagueName": "Germany League",
"teamOne": "Grossaspach",
"teamTwo": "Offenbacher",
"date": "05.11.2021 - 21.00",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.19"
},
"1": {
"id": "1900",
"price": "0",
"userBought": "0",
"leagueName": "France League",
"teamOne": "FC Villefranche-Beaujolai",
"teamTwo": "US Avranches",
"date": "05.11.2021 - 21.00",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.25"
},
"2": {
"id": "1899",
"price": "0",
"userBought": "0",
"leagueName": "Germany League",
"teamOne": "Holstein Kiel",
"teamTwo": "Dynamo Dresden",
"date": "05.11.2021 - 20.30",
"result": "0",
"teamOneScore": "0",
"teamTwoScore": "0",
"info": "+1.5 Goal Over",
"ratio": "1.20"
}}}
但是我无法使用 volley 从“数据”标签获取字符串对象,因为没有任何 json 数组到 foreach 标签。
我累了,我搜索了这么多例子。我无法从 stacoverflow 找到任何解决方案。 谁能帮帮我?
我不熟悉 Volley,但一个直接的方法是将你的 JSON 字符串去隐藏成一个 Map
与大多数 JSON 库(例如,Jackson ),那么就可以得到字段data
的内容,遍历如下:
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> resultMap = objectMapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {});
((Map<String, Object>) resultMap.get("data")).entrySet().stream()
.map(Map.Entry::getValue)
.forEach(System.out::println);
控制台输出:
{id=1901, price=0, userBought=0, leagueName=Germany League, teamOne=Grossaspach, teamTwo=Offenbacher, date=05.11.2021 - 21.00, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.19}
{id=1900, price=0, userBought=0, leagueName=France League, teamOne=FC Villefranche-Beaujolai, teamTwo=US Avranches, date=05.11.2021 - 21.00, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.25}
{id=1899, price=0, userBought=0, leagueName=Germany League, teamOne=Holstein Kiel, teamTwo=Dynamo Dresden, date=05.11.2021 - 20.30, result=0, teamOneScore=0, teamTwoScore=0, info=+1.5 Goal Over, ratio=1.20}
您也可以使用 Jayway JsonPath:
获得相同的结果Map<String, Object> resultMap = JsonPath.parse(jsonStr).read("$.data");
resultMap.entrySet().stream()
.map(Map.Entry::getValue)
.forEach(System.out::println);