【问题标题】:Java parse JSON path value to variableJava 将 JSON 路径值解析为变量
【发布时间】:2021-02-11 16:06:14
【问题描述】:

我有问题。我有以下代码将 JSON 字符串解析为对象:

public AgentStrategy parseJsonToObject(String jsonString) {
    Gson gson = new Gson();
    AgentStrategy agent = gson.fromJson(jsonString, AgentStrategy.class);    
    return agent;
}

但现在我有一个 JSON 值不等于属性名称。这是新的 JSON:

{
   "Market": "USDT",
   "Coin":"BTC",
   "ModuleEnabled":{
      "Patterns":{
         "Buy":"true",
         "Sell":"true"
      },
      "EMA":{
         "Buy":"true",
         "Sell":"false"
      }
   }
}

这就是类的样子:

public class AgentStrategy {

    public String Market;    
    public String Coin;

    public boolean ModuleEnabledBuyEMA;
    public boolean ModuleEnabledSellEMA;
    public boolean ModuleEnabledBuyPatterns;
    public boolean ModuleEnabledSellPatterns;

    public AgentStrategy parseJsonToObject(String jsonString) {
        Gson gson = new Gson();
        AgentStrategy agent = gson.fromJson(jsonString, AgentStrategy.class);

        return agent;
    }
}

现在如何匹配 JSON 和类如下:

"ModuleEnabled" -> "EMA" -> "Buy" = ModuleEnabledBuyEMA
"ModuleEnabled" -> "EMA" -> "Sell" = ModuleEnabledSellEMA
"ModuleEnabled" -> "Patterns" -> "Buy" = ModuleEnabledBuyPatterns
"ModuleEnabled" -> "Patterns" -> "Sell" = ModuleEnabledSellPatterns

【问题讨论】:

  • 我还没有解决这个问题!

标签: java json


【解决方案1】:

您可以编写自定义反序列化器,在其中使用默认 Gson 反序列化并另外手动解析“有问题的”字段:

public class AgentStrategyDeserializer implements JsonDeserializer<AgentStrategy> {

    @Override
    public AgentStrategy deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

        Gson gson = new Gson();
        AgentStrategy agent = gson.fromJson(jsonElement, AgentStrategy.class);
        // At this point only Market and Coin attributes are set. Since the booleans can not be parsed they are initialized to false

        JsonObject moduleEnabledJsonObject = jsonElement.getAsJsonObject().get("ModuleEnabled").getAsJsonObject();
        boolean moduleEnabledBuyPatterns = moduleEnabledJsonObject.get("Patterns").getAsJsonObject().get("Buy").getAsBoolean();
        boolean moduleEnabledSellPatterns = moduleEnabledJsonObject.get("Patterns").getAsJsonObject().get("Sell").getAsBoolean();
        boolean moduleEnabledBuyEMA = moduleEnabledJsonObject.get("EMA").getAsJsonObject().get("Buy").getAsBoolean();
        boolean moduleEnabledSellEMA = moduleEnabledJsonObject.get("EMA").getAsJsonObject().get("Sell").getAsBoolean();

        agent.setModuleEnabledBuyEMA(moduleEnabledBuyEMA);
        agent.setModuleEnabledSellEMA(moduleEnabledSellEMA);
        agent.setModuleEnabledBuyPatterns(moduleEnabledBuyPatterns);
        agent.setModuleEnabledSellPatterns(moduleEnabledSellPatterns);

        return agent;
    }
}

这就是你使用反序列化器的方式:

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(AgentStrategy.class, new AgentStrategyDeserializer());
    Gson gson = gsonBuilder.create();
    AgentStrategy agentStrategy = gson.fromJson(jsonString, AgentStrategy.class);

【讨论】:

  • 我得到错误:Class 'AgentStrategyDeserializer' must either be declared abstract or implement abstract method 'deserialize(JsonParser, DeserializationContext)' in 'JsonDeserializer',但是当我输入那个方法时,我没有jsonElement
  • 你在实现 com.google.gson.JsonDeserializer 吗?它的方法签名是deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
  • 是的,我实现了这一点,但后来我在这一行得到了那个错误:public class AgentStrategyDeserializer implements JsonDeserializer&lt;AgentStrategy&gt; { 它给了我:Interface expected here
  • 也许你正在混合 Gson 和 Jackson。我在您的错误中看到的方法 deserialize(JsonParser, DeserializationContext) 来自 Jackson 反序列化接口。删除所有 jackson 依赖项并添加 import com.google.gson.*
  • 是的,解决了它
【解决方案2】:

第一个选择是编写自己的反序列化器。看: How do I write a custom JSON deserializer for Gson?

另一种解决方案是创建一个与您的 json 匹配的类,而不是创建一个方法(from(JsonMatchingClass)) 将其转换为 AgentStrategy

【讨论】:

  • 但我也有正确解析的默认属性。难道没有办法将这些元素单独解析为属性吗?
猜你喜欢
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多