简体   繁体   English

在Java中创建JSON对象的方法

[英]Ways of creating JSON Objects in Java

The following is the JSON body I want to use in my iOS (Swift) and Android (Java) applications. 以下是我想在我的iOS(Swift)和Android(Java)应用程序中使用的JSON主体。

{
    "type" : "select",
    "args" : {
        "table"  : "todo",
        "columns": ["id", "title","completed"],
        "where"  : {"user_id": 1}
    }
}

In Swift, it is pretty straightforward and easy to convert the above into a Dictionary : 在Swift中,将上面的内容转换为字典非常简单易行:

let params: [String: Any] = [
  "type" : "select",
  "args" : [
    "table"     : "todo",
    "columns"   : ["id","title","completed"],
    "where"     : ["user_id" : 1]
  ]
]

In Java, I am using GSON to do the above, but I feel that my solution is ugly and too long 在Java中,我使用GSON来完成上述操作,但我觉得我的解决方案很丑陋而且太长

public class SelectQuery {

    @SerializedName("type")
    String type = "select";

    @SerializedName("args")
    Args args;

    public SelectTodoQuery(=) {
        args = new Args();
        args.where = new Where();
        args.where.userId = 1;
    }

    class Args {

        @SerializedName("table")
        String table = "todo";

        @SerializedName("columns")
        String[] columns = {
                "id","title","completed"
        };

        @SerializedName("where")
        Where where;

    }

    class Where {
        @SerializedName("user_id")
        Integer userId;
    }

}

Is there a better way to do this in Java and also how will one go about representing this JSON in Java natively, without the use of GSON? 有没有更好的方法在Java中执行此操作,以及如何在不使用GSON的情况下本地使用Java本地表示此JSON?

UPDATE UPDATE

I am not asking for a list of libraries which help me do the above, I already know of them and clearly am using one. 我没有要求提供一个帮助我完成上述工作的图书馆清单,我已经知道它们并且显然正在使用它们。 I do not need to know about their performance either. 我也不需要了解他们的表现。 I am asking for a better implementation (if it exists) and if Java does not provide such a feature, that can be an accepted answer too. 我要求更好的实现(如果它存在),如果Java没有提供这样的功能,那也可以是一个公认的答案。 Also, an example for doing the same natively in Java. 此外,还有一个在Java中本地执行相同操作的示例。

Well, there are several libs available to serialize and de-serialize object to and from json . 好吧,有几个库可用于对json进行序列化和反序列化对象。

GSON is the simplest one, you can convert your POJO (plan old java object) to JSON without annotating them. GSON是最简单的一个,您可以将POJO (计划旧的Java对象)转换为JSON而无需注释它们。

LoganSquare is the best library for this purpose, it requires you to annotate your fields but the performance is pretty high. LoganSquare是用于此目的的最佳库,它需要您注释您的字段,但性能非常高。

Github page actually tells about benchmark study of LoganSquare vs other options. Github页面实际上讲述了LoganSquare与其他选项的基准研究。

在此输入图像描述

There are a few others like Moshi and Jackson but considering Platform's limitations I have found LoganSquare the best. 还有其他一些像MoshiJackson,但考虑到Platform的限制,我发现LoganSquare是最好的。

As far as Native capability is concerned, Java so far does not provide any such utility and if someone is not interested in Third partylibs , the project I have mentioned above are all open source objects, one can fork them and implement their own version based on one's usecases. 就Native功能而言,Java到目前为止还没有提供任何这样的实用程序,如果有人对Third partylibs不感兴趣,我上面提到的项目都是开源对象,可以分叉它们并实现自己的版本基于一个人的用例。

With Gson you can try something like this, i am not sure about the type you want to read here: 有了Gson你可以尝试这样的东西,我不确定你想要在这里阅读的类型:

final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.enableComplexMapKeySerialization().create();
final Type type = new TypeToken<Map<String, Args>>(){}.getType();

// deserialize from a string (read your file to string maybe)
HashMap<String, Args> aMap = gson.fromJson(aJSONString, type);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM