【问题标题】:How to read the value from json using JsonReader without knowing the json key如何在不知道 json 键的情况下使用 JsonReader 从 json 读取值
【发布时间】: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


    【解决方案1】:

    你可以这样做:

    @Override
    public MagazineReader read(JsonReader in) throws IOException {
    
      final MagazineReader magazineReader = new MagazineReader();
      final Map<String, Object> users = new HashMap<>();
    
      in.beginObject();
      while (in.hasNext()) {
        switch (in.nextName()) {
          case "id":
            magazineReader.setID(in.nextInt());
            break;
          case "tag":
            magazineReader.setTag(in.nextString());
            break;
          case "users":
            in.beginObject();
            while(in.hasNext()) {
              String key = in.nextName();
              JsonToken type = in.peek();
              if (type == JsonToken.NUMBER) {
                users.put(key, in.nextInt());
              } else if (type == JsonToken.STRING) {
                users.put(key, in.nextString());
              } else {
                System.err.println("Unhandled type: " + type);
              }
            }
            in.endObject();
            break;
        }
        in.endObject();
      }
    }
    

    我使用Map 来存储键值对,您可以使用任何类型的对象来执行此操作。另外,我只添加了数字和字符串值的处理程序。

    重要的部分是当您到达users 键时开始一个新对象,然后遍历该对象的所有属性。您如何处理对象的条目取决于您要执行的操作。

    【讨论】:

    • 谢谢。这就是我想要的。你的代码和解释让我更好理解。
    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 2019-02-24
    • 2021-11-09
    • 2022-08-18
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多