How does java parse the json returned by the background with unknown key and value

Problem description:
The background returns a json object, the key and value are dynamic and the field is unknown! As shown below, the keys and values ​​in the labels object are dynamically changed.

{
    bads: 0,
    average: "8.3",
    totalEvaluates: 6,
    goodRate: "50%",
    servedCompanies: "5",
    goods: 3,
    mediums: 3,
    labels: {
        安全专业性强: 6,
        建议可行: 4,
        对企业有帮助: 3,
        服务态度好: 1
    }
}

Solution:
Just write other known fields directly into the properties of the entity class, and that dynamic json object needs to be JsonElementparsed. JsonElement can parse various json formats. So the above json can be written to the following entity class:

  public class CreditBean{
        /**
         * bads : 0
         * average : 8.0
         * totalEvaluates : 2
         * goodRate : 50%
         * servedCompanies :
         * goods : 1
         * mediums : 1
         * labels :
         */

        private int bads;
        private String average;
        private String totalEvaluates;
        private String goodRate;
        private String servedCompanies;
        private int goods;
        private int mediums;
        private JsonElement labels;
 }

The requested data can be done like this:

 JsonElement labels = credit.getLabels();//获取动态json元素类
 Iterator<String> it = json.keys();//使用迭代器
     while (it.hasNext()) {
         String key = it.next();//获取key
         int value = json.getInt(key);//获取value
         Log.e("key-value","key="+key+" value="+value);
     }

Introduction to JsonElement:
com.google.gson.JsonElement is an abstract class that represents an element of a json string. This element can be JsonObject , JsonArray , JsonPrimitive JsonNull etc.
JsonElement provides a series of methods to determine the current JsonElement type, such as:

  /**
   * provides check for verifying if this element is an array or not.
   *
   * @return true if this element is of type {@link JsonArray}, false otherwise.
   */
  public boolean isJsonArray() {
    return this instanceof JsonArray;
  }

  /**
   * provides check for verifying if this element is a Json object or not.
   *
   * @return true if this element is of type {@link JsonObject}, false otherwise.
   */
  public boolean isJsonObject() {
    return this instanceof JsonObject;
  }

JsonElement also provides a series of methods to get the corresponding type of JsonElement, such as:

 /**
   * convenience method to get this element as a {@link JsonObject}. If the element is of some
   * other type, a {@link IllegalStateException} will result. Hence it is best to use this method
   * after ensuring that this element is of the desired type by calling {@link #isJsonObject()}
   * first.
   *
   * @return get this element as a {@link JsonObject}.
   * @throws IllegalStateException if the element is of another type.
   */
  public JsonObject getAsJsonObject() {
    if (isJsonObject()) {
      return (JsonObject) this;
    }
    throw new IllegalStateException("Not a JSON Object: " + this);
  }

  /**
   * convenience method to get this element as a {@link JsonArray}. If the element is of some
   * other type, a {@link IllegalStateException} will result. Hence it is best to use this method
   * after ensuring that this element is of the desired type by calling {@link #isJsonArray()}
   * first.
   *
   * @return get this element as a {@link JsonArray}.
   * @throws IllegalStateException if the element is of another type.
   */
  public JsonArray getAsJsonArray() {
    if (isJsonArray()) {
      return (JsonArray) this;
    }
    throw new IllegalStateException("This is not a JSON Array.");
  }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511584&siteId=291194637