【问题标题】:How to get JSON element type with Gson?如何使用 Gson 获取 JSON 元素类型?
【发布时间】:2014-01-04 14:50:22
【问题描述】:

在 JSON 文件中,文件中的每个对象都由不同类型的 JSON 元素组成。 (整数、字符串、数组、对象数组等)

我的目标是列出所有元素名称和相应的类型。我可以知道如何在 Gson 中做到这一点吗?这样做的目的是创建 Hive 架构。

例子:

{
  "number": 1, 
  "ts": "1386848002", 
  "cmpg": [
    {
      "id": 476, 
      "mcp": 0, 
      "deals": [ ], 
      "cookie": "uid:123", 
      "bid": [
        {
          "bId": 0, 
          "status": "ZB", 
          "rmtchID": -1
        }
      ]
    }
  ]
}

输出:

number int,
ts String,
cmpg array<map<String, Object>> // not sure how to interpret this...

【问题讨论】:

    标签: java arrays json hive gson


    【解决方案1】:

    我编写了这个简单的类,向您展示了如何使用一些 Gson 类来获得所需的内容。

    package stackoverflow.questions.q19124387;
    
    import java.util.Map;
    
    import com.google.gson.*;
    
    public class Q20624042 {
    
       private static String printClass(JsonElement je, String ident) {
          StringBuilder sb = null;
          if (je.isJsonNull())
             return "null";
    
          if (je.isJsonPrimitive()) {
             if (je.getAsJsonPrimitive().isBoolean())
                return "Boolean";
             if (je.getAsJsonPrimitive().isString())
                return "String";
             if (je.getAsJsonPrimitive().isNumber()){
                return "Number";
             }
          }
    
          if (je.isJsonArray()) {
             sb = new StringBuilder("array<");
             for (JsonElement e : je.getAsJsonArray()) {
                sb.append(printClass(e, ident+ "    "));
             }
             sb.append(">");
             return sb.toString();
          }
    
          if (je.isJsonObject()) {
             sb = new StringBuilder("map<\n");
             for (Map.Entry<String, JsonElement> e : je.getAsJsonObject().entrySet()) {
                sb.append(ident);
                sb.append(e.getKey()).append(":");
                sb.append(printClass(e.getValue(), ident+"   "));
                sb.append("\n");
             }
             sb.append(ident);
             sb.append(">");
             return sb.toString();
          }
          return "";
    
       }
    
       public static void main(String[] args) {
          String json = "{" + "\"number\":1," + "\"ts\":\"1386848002\"," + "\"cmpg\":[{\"id\":476,\"mcp\":0.0000,\"deals\":[],\"cookie\":\"uid:123\",\"bid\":[{\"bId\":0,\"status\":\"ZB\",\"rmtchID\":-1}]}]}";
    
          JsonElement je = new JsonParser().parse(json);
          System.out.println(printClass(je,"   "));
    
       }
    
    }
    

    这是您的 JSON 字符串的结果:

    map<
       number:Number
       ts:String
       cmpg:array<map<
              id:Number
              mcp:Number
              deals:array<>
              cookie:String
              bid:array<map<
                     bId:Number
                     status:String
                     rmtchID:Number
                     >>
       >>
      >
    

    JSON 具有递归性质,因此解决此类问题的唯一方法是编写递归方法。我的缩进系统非常幼稚,我放缩进只是为了显示与您的 JSON 的对应关系,也许您甚至不需要它。记住in JSON你没有整数和双精度的区别,

    JSON 可以表示四种基本类型(字符串、数字、 booleans 和 null)和两种结构化类型(对象和数组)

    所以如果你有什么区别,你会改变我的方法。

    .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2012-01-13
      • 2011-09-14
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多