使用JSON反序列化为Java对象吗? [英] Deserialization to a Java object using JSON?

查看:121
本文介绍了使用JSON反序列化为Java对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须接收以下JSON:

I must receive the following JSON:

{
   "method":"methodName1",
   "args":{
      "arg1Name":"arg1Value",
      "arg2Name":"arg2Value"
   }

}

这是另一个例子:

{
   "method":"methodName2",
   "args":{
      "arg1Name":"arg1Value",
      "arg2Name":"arg2Value"
      "arg3Name":"arg3Value"
      "arg4Name":"arg4Value"
   }

}

我需要解析这些JSON,以便使用指定的"args"作为参数来调用指定的方法.

I need to parse those JSONs in order to call the specified method with the specified "args" as parameters.

(对我而言)最简单的方法是使用JsonParser来获取JsonElement,然后是JsonObject,然后使用.get()提取每个值...类似这样:

The easy way (for me) would be to use a JsonParser to get a JsonElement, then a JsonObject and then extract each value using .get()... something like this:

JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(jsonString);
if (jsonElement.isJsonObject()) {
     JsonObject jsonObject = jsonElement.getAsJsonObject();
     String method = jsonObject.get("method").getAsString();
     JsonObject jsonArgs = jsonObject.getAsJsonObject("args");
     String arg1 = jsonArgs.get("arg1Name").getAsString();
     String arg2 = jsonArgs.get("arg2Name").getAsString();

}

然后我可以使用适当的参数(使用switch)调用指定的方法.

And then I could call the specified method with the appropriate arguments (using a switch).

我只是想知道是否会有更简单(或更漂亮)的方法来实现这一目标.

I was just wondering if there would be an easier (or prettier) way to achieve this.

我可以改用.fromJson()来检索对象,但是我不知道如何创建我的类来这样做(有一些args数组,并且并非所有方法都具有相同数量的args).

I could use .fromJson() to retrieve an object instead, but I don't know how I should create my class to do so (there's something like an array of args and not all methods have the same number of args).

我是JSON(和Java)的新手.

I'm new to JSON (and to Java as well).

我已经做到了:

import lotus.domino.*;
import com.google.gson.*;
import java.io.*;
import java.lang.reflect.Method;

public class JavaAgent extends AgentBase {
    public void NotesMain() {
        try {
            String jsonReceived = "{\"method\":\"methodName\",\"args\":{\"arg1Name\":\"arg1Value\",\"arg2Name\":\"arg2Value\"}}";
            Gson gson = new Gson();
            MethodCall mc = gson.fromJson(jsonReceived, MethodCall.class);

            JavaAgent ja = new JavaAgent();
            Method method = getClass().getMethod(mc.getMethod(), MethodCall.class);
            String s = (String)method.invoke(ja, mc);

            PrintWriter pw = getAgentOutput();
            pw.println(s);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public String methodName(MethodCall mc) {
        String s = mc.getArgs().get("arg1Name") + " " + mc.getArgs().get("arg2Name");
        return s;
    }
}

import java.util.*;
public class MethodCall {
    private String method;
    private Map<String, String> args;

    public String getMethod() {
        return method;
    }

    public Map<String, String> getArgs() {
        return args;
    }
}

这似乎可以工作...但是由于我是Java新手,所以我不确定这是否是正确的方法.你觉得呢?

It seems to work... but since I'm new to Java, I'm not sure that's the proper way to do it. What do you think?

推荐答案

Gson Map序列化程序是否足够聪明,可以将其反序列化?

Perhaps the Gson Map serializer is smart enough to deserialize that?

public class MethodCall {
    private String method;
    private Map<String, String> args;
}

(没有时间对其进行全面测试,但我希望它能起作用!:))

(Don't have the moment to test it thoroughly, but I hope it works! :) )

这篇关于使用JSON反序列化为Java对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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