我正在尝试学习JSON解析,并且提出了一个关于如何解析JSON的快速问题,其中"类别"名称可以根据外部API(特别是我玩弄的视频游戏API)的请求进行更改。
作为随机示例,使用" laofuthetiger"发送API请求因为URI返回如下所示的JSON:
let sprite = SKSpriteNode(imageNamed:"fullcolor")
let overlay = SKSpriteNode(imageNamed:"overlay")
overlay.color = .Red
overlay.zPosition = 1
overlay.colorBlendFactor = 1.0
sprite.addChild(overlay)
将URI更改为" sloverlord"得出这个:
{ "laofuthetiger": {
"id": 51044840,
"name": "Lao Fu the Tiger",
"profileIconId": 664,
"revisionDate": 1484537981000,
"summonerLevel": 30
}}
根据我对使用GSON的JSON解析的基本理解,我能够通过使用如下所示的类从第一个JSON示例中收集数据:
{ "sloverlord": {
"id": 39943538,
"name": "sloverlord",
"profileIconId": 712,
"revisionDate": 1484537981000,
"summonerLevel": 30
}}
其中SummonerDto包含单个元素id,name等。但是,我不知道如何处理实际的"类别" (或者它被称为)在API调用之间进行更改。使用此解决方案,使用laofuthetiger的呼叫可以正常工作,但是由于显而易见的原因,sloverlord会返回内部错误。
编辑:有关更多信息,URI看起来像这样:public class Player{
private SummonerDto laofuthetiger;
...
其中" laofuthetiger"可以是任何球员的名字。
答案 0 :(得分:1)
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper om = new ObjectMapper();
JsonNode tree = om.readTree(jsonString);
JsonNode dtoNode = tree.get(0);
SummonerDto dto = om.readValue(om.treeAsTokens(dtoNode), SummonerDto.class);
import java.io.IOException;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Test {
private static final String JSON = "{ \"laofuthetiger\": {" + "\"id\": 51044840,"
+ "\"name\": \"Lao Fu the Tiger\"," + "\"profileIconId\": 664," + "\"revisionDate\": 1484537981000, "
+ "\"summonerLevel\": 30" + "}}";
public static void main(String[] args) throws IOException {
JsonParser parser = new JsonParser();
Map.Entry<String, JsonElement> e = ((JsonObject) parser.parse(JSON)).entrySet().iterator().next();
Gson g = new Gson();
SummonerDto dto = g.fromJson(e.getValue(), SummonerDto.class);
System.out.println(dto);
}
class SummonerDto {
int id;
String name;
int profileIconId;
long revisionDate;
int summonerLevel;
@Override
public String toString() {
return "SummonerDto [id=" + id + ", name=" + name + ", profileIconId=" + profileIconId + ", revisionDate="
+ revisionDate + ", summonerLevel=" + summonerLevel + "]";
}
}
<强>更新强>
添加了GSON的完整代码
SummonerDto [id=51044840, name=Lao Fu the Tiger, profileIconId=664, revisionDate=1484537981000, summonerLevel=30]
<强>输出强>
=\s*(\d)+;
答案 1 :(得分:0)
有一个选项:
你总是知道你的播放器名称吗?
当你可以使用API时,就像这样:
mPlayer = "laofuthetiger";
Repository mRepo = new Repo();
mRepo.requestPlayerByName(player)
[when you get your response]
private void getMyResponseFromAPI(Json jsonResponse){
JsonObject jSummoner jsonResponse.get(mPlayer).getAsJsonObject()
SummonerDto summoner = new Gson().fromJson(jPlayer, Summoner.class)
Player player = new Player(summoner)
}
通过这种方式,您将始终得到您的回复,与您的玩家(或召唤师)的名称无关。
为什么会有效?
您的API响应中唯一更改的部分是召唤器名称...响应的其余部分始终遵循相同的模式。因此,您可以使用正确的名称获得Json对象,您将始终拥有:
{
"id": 51044840,
"name": "Lao Fu the Tiger",
"profileIconId": 664,
"revisionDate": 1484537981000,
"summonerLevel": 30
}
这真的是要与Gson(或杰克逊)解析。