【发布时间】:2020-08-24 18:53:11
【问题描述】:
我正在尝试使用 TypedArray 来解析 json。
我的 json 将如下:
{
"id":"112233",
"tag":"From server",
"users":{
"vijay":1,
"dhas":2,
"vijaydhas":3
}
}
这里用户对象中的键是动态的。我将在运行时从服务器接收。只有那个时候我不知道钥匙(vijay,dhas,vijaydhas)。
要解析 id 和 tag,我将执行以下代码。
@Override
public MagazineReader read (JsonReader in) throws IOException {
final MagazineReader magazineReader = new MagazineReader();
in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "id":
magazineReader.setID(in.nextInt());
break;
case "tag":
magazineReader.setTag(in.nextString());
break;
in.beginArray();
/*
For User how to read the json???
*/
}
in.endObject();
}
现在我想在不知道密钥的情况下读取和解析用户 JsonArray 及其对象。我知道如何在不知道密钥的情况下解析 JSONObject。
JSONObject users= obj.getJSONObject("users");
Iterator iteratorObj = detailList.keys();
while (iteratorObj.hasNext())
{
String jsonKey = (String)iteratorObj.next();
property.put(jsonKey,usersList.get(jsonKey));
}
但在 JsonReader 中,我不知道如何在不知道密钥的情况下读取 json 值。请帮助我。 [1]:https://javacreed.com/gson-typeadapter-example
【问题讨论】:
标签: android json jsonreader