如何获得JSON元素类型与GSON? [英] How to get JSON element type with Gson?

查看:115
本文介绍了如何获得JSON元素类型与GSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个JSON文件,文件内的各对象由不同类型的JSON元件构成。 (整数,字符串,数组,对象阵列等)

我的目标是列出所有元素的名称和相应的类型。我想知道我该怎么做,在GSON?这样做的目的是创建一个蜂巢模式

例如:

  {
  1号,
  TS:1386848002,
  cmpg:[
    {
      ID:476,
      MCP:0,
      交易: [ ],
      曲奇:UID:123
      出价: [
        {
          申奥:0,
          状态:ZB
          rmtchID:-1
        }
      ]
    }
  ]
}

输出:

 号INT,
TS字符串,
cmpg阵列<地图和LT;弦乐,对象>> //不知道如何跨preT这...


解决方案

我写了这个简单的类,展示了如何使用一些GSON类来得到你所需要的。

 包stackoverflow.questions.q19124387;进口的java.util.Map;导入com.google.gson *。公共类Q20624042 {   私人静态字符串printClass(JsonElement JE,串IDENT){
      StringBuilder的SB = NULL;
      如果(je.isJsonNull())
         返回空;      如果(je.isJsonPrimitive()){
         如果(je.getAsJsonPrimitive()。isBoolean())
            返回布尔;
         如果(je.getAsJsonPrimitive()。isString())
            返回字符串;
         如果(je.getAsJsonPrimitive()。ISNUMBER()){
            返回数;
         }
      }      如果(je.isJsonArray()){
         SB =新的StringBuilder(数组<);
         对于(JsonElement E:je.getAsJsonArray()){
            sb.append(printClass(即IDENT +));
         }
         sb.append(>中);
         返回sb.toString();
      }      如果(je.isJsonObject()){
         SB =新的StringBuilder(图< \\ n);
         对于(Map.Entry的<弦乐,JsonElement> E:je.getAsJsonObject()的entrySet()){
            sb.append(IDENT);
            。sb.append(e.getKey())附加(:);
            sb.append(printClass(e.getValue(),IDENT +));
            sb.append(\\ n);
         }
         sb.append(IDENT);
         sb.append(>中);
         返回sb.toString();
      }
      返回;   }   公共静态无效的主要(字串[] args){
      JSON字符串={+\\号\\:1,+\\TS \\:\\1386848002 \\,+ \"\\\"cmpg\\\":[{\\\"id\\\":476,\\\"mcp\\\":0.0000,\\\"deals\\\":[],\\\"cookie\\\":\\\"uid:123\\\",\\\"bid\\\":[{\\\"bId\\\":0,\\\"status\\\":\\\"ZB\\\",\\\"rmtchID\\\":-1}]}]}\";      JsonElement JE =新JsonParser()解析(JSON)。
      的System.out.println(printClass(JE,));   }}

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

 地图<
   号码:号码
   TS:字符串
   cmpg:数组<地图和LT;
          身份证号
          MCP:号码
          特价信息:数组<>
          饼干:字符串
          价格:数组<地图和LT;
                 出价:号码
                 状态:字符串
                 rmtchID:号码
                 >>
   >>
  >

JSON有一个递归特性,所以唯一的办法来处理这​​一类问题是写一个递归方法。我的压痕系统是相当幼稚的,我把缩进只对应展示给你的JSON,也许你甚至不需要说。请记住,在JSON 你没有整数和双打的区别,


  

JSON可以重新present了四个基本类型(字符串,数字,
  布尔和null)和两种结构化类型(对象和数组)


因此​​,如果你有改了一下我的方法是什么区别。

In a JSON file, each object inside the file is composed by different type of JSON elements. (integer, string, array, array of objects, etc.)

My target is to list all element name and corresponding type. May I know how can I do that in Gson? The purpose of this is for creating a Hive schema.

Example:

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

Output:

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

解决方案

I wrote this simple class that shows you how use some Gson classes to get what you need.

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,"   "));

   }

}

And this is the result with your JSON string:

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 has a recursive nature, so the only way to approach to this kind of problem is to write a recursive method. My indentation system is quite naive, I put indentation only to show the correspondence to your JSON, maybe you do not even need that. Keep in mind that in JSON you do not have difference between integer and doubles,

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays)

so if you what that distinction you have change a bit my method.

.

这篇关于如何获得JSON元素类型与GSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆