Search code examples
androidjsonjsonreader

How to read the value from json using JsonReader without knowing the json key


I am trying to use TypedArray to parse json.

My json will be the following:

    {
      "id":"112233",
      "tag":"From server",
      "users":{
      "vijay":1,
      "dhas":2,
      "vijaydhas":3
      }
    }

Here the in the users object key will be dynamic. I will receive from the server at run time. On that time only I Don't know the key(vijay, dhas, vijaydhas).

To parse id and tag I will do the following code.

           @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();
            }

Now I want to read and parse the users JsonArray and its object without knowing the key. I know how to parse the JSONObject without knowing the key.

JSONObject users= obj.getJSONObject("users");
            Iterator iteratorObj = detailList.keys();
            while (iteratorObj.hasNext())
            {
                String jsonKey = (String)iteratorObj.next();
                property.put(jsonKey,usersList.get(jsonKey));
            }

But in JsonReader I don't know how to read the json value without knowing key. Please help me on this. [1]: https://javacreed.com/gson-typeadapter-example


Solution

  • You can do something like this:

    @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();
      }
    }
    

    I've used a Map to store the key value pairs, you can use any kind of object to do that. Also, I've only added handlers for number and string values.

    The important part is to begin a new object when you get to the users key and then go through all that object's properties. How you handle the object's entries depends on what are you trying to do.