【问题标题】:Can't parse Json String with Moshi无法使用 Moshi 解析 Json 字符串
【发布时间】:2019-04-24 03:40:36
【问题描述】:

我正在尝试使用 Moshi 解析 Json 响应,我遇到的问题是键的值是字符串上的 Json 包装:

{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},{\"name\": \"available\"}]"}

这是我的课

@Json(name = "mobile_accounts_account_showProperties")
private List<PropertyConfig> showProperties;

我尝试在解析之前使用replace("\"[", "[")replace("\\", "") 删除 (""),但这不是一个选项,因为这会删除我确实需要的一些其他引号。我尝试使用 JsonAdapter 但我无法做到这一点。 JsonAdapter 没有被调用。

public class PropertyConfigJsonAdapter {

public PropertyConfigJsonAdapter() {
}

@ToJson
String toJson(List<PropertyConfig> card) {
    return "";
}

@FromJson
List<PropertyConfig> fromJson(String object) {

    return new ArrayList<>();
}

我试试看 JsonAdapter 是否被调用,但它从不调用“fromJson”方法。以下是我如何称呼适配器:

MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())

【问题讨论】:

    标签: jsonparser moshi


    【解决方案1】:

    最简单的方法是将值作为字符串读取并从那里解码。您可以使用 JsonAdapter 使其对您的应用程序代码透明,例如下面的 ShowPropertiesContainer.ADAPTER

    public final class ShowPropertiesContainer {
      public final List<PropertyConfig> showProperties;
    
      public static final Object ADAPTER = new Object() {
        @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
            JsonAdapter<ShowPropertiesContainer> fooAdapter,
            JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
          ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
          return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
              showPropertiesContainer.mobile_accounts_account_showProperties),
              showPropertiesContainer.mobile_accounts_account_showProperties);
        }
      };
    
      final String mobile_accounts_account_showProperties;
    
      ShowPropertiesContainer(List<PropertyConfig> showProperties,
          String mobile_accounts_account_showProperties) {
        this.showProperties = showProperties;
        this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
      }
    }
    
    public final class PropertyConfig {
      public final String name;
    
      PropertyConfig(String name) {
        this.name = name;
      }
    }
    
    @Test public void showPropertiesContainer() throws IOException {
      Moshi moshi = new Moshi.Builder()
          .add(ShowPropertiesContainer.ADAPTER)
          .build();
      JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
      String encoded =
          "{\"mobile_accounts_account_showProperties\":\"[{\\\"name\\\": \\\"current\\\"},"
              + "{\\\"name\\\": \\\"available\\\"}]\"}";
      ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多